Greasy Fork is available in English.

Discussions » Demandes de création de scripts

在使用谷歌搜索的时候,希望在搜索结果页右边空白处新增一个宅栏,里面显示同样关键词在另一个搜索引擎里的搜所结果

§
Posté le: 11/03/2023

在使用谷歌搜索的时候,希望在搜索结果页右边空白处新增一个宅栏,里面显示同样关键词在另一个搜索引擎里的搜所结果

例如,当我用谷歌搜索“python”这个关键词时,如附件图片红框位置,会显示比如 百度 搜索的结果。当一个关键词在我的主搜索引擎(谷歌搜索)里没有合适结果时,可以快速看到另一个搜索引擎的结果。

§
Posté le: 11/03/2023

我自己尝试用 ChatGPT 写了一个,但是由于我不太懂代码,不知道要怎么把样式和体验修改的更好,希望有人能优化它


// ==UserScript==
// @name Google search results with Bing window
// @namespace http://tampermonkey.net/
// @version 1
// @description Show Bing search results in a narrow window on Google search results page
// @author Your Name
// @match https://www.google.com/*
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// ==/UserScript==

(function() {
'use strict';

// Get search query from Google search input field
const query = document.querySelector('input[name="q"]').value;

// Create container for Bing search results
const bingContainer = document.createElement('div');
bingContainer.id = 'bing-container';
bingContainer.style.cssText = `
position: fixed;
top: 60px;
right: 0;
bottom: 0;
width: 350px;
background-color: #1e1e1e;
z-index: 99999;
overflow: auto;
color: #fff;
border: 1px solid #333;
border-radius: 8px;
`;

// Add title to Bing container
const bingTitle = document.createElement('h2');
bingTitle.textContent = 'Bing 搜索结果';
bingTitle.style.cssText = `
margin: 0;
padding: 10px;
font-size: 18px;
font-weight: bold;
background-color: #222;
color: white;
text-align: center;
`;
bingContainer.insertBefore(bingTitle, bingContainer.firstChild);

// Add container to page
document.body.appendChild(bingContainer);

// Fetch Bing search results using GM_xmlhttpRequest
GM_xmlhttpRequest({
method: 'GET',
url: `https://www.bing.com/search?q=${query}`,
onload: function(response) {
// Extract search results from response
const parser = new DOMParser();
const doc = parser.parseFromString(response.responseText, 'text/html');
const searchResults = doc.querySelectorAll('#b_results .b_algo');

// Add search results to Bing container
searchResults.forEach((result) => {
const resultContainer = document.createElement('div');
resultContainer.className = 'result-container';
resultContainer.innerHTML = result.outerHTML;
bingContainer.appendChild(resultContainer);
});

// Add some styles to search results
GM_addStyle(`
ul, ol,li {
list-style-type: none;
}
#bing-container .result-container {
margin: 10px;
padding: 10px;
border-radius: 8px;
background-color: #333;
}
#bing-container .result-container h2 {
margin: 10px 0 5px;
color: white;
}
#bing-container .result-container .b_caption p {
margin: 0;
color: #bbb;
}
`);

}
});
})();

Poster une réponse

Connectez-vous pour poster une réponse.