GoogleMore

谷歌搜索快速跳转搜索其他网站

目前為 2023-07-05 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         GoogleMore
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  谷歌搜索快速跳转搜索其他网站
// @author       marumaru
// @license MIT
// @match        https://www.google.com/search?q*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==


(function () {
    'use strict';

    setTimeout(() => {

        var targetElement;
        var elements = document.querySelectorAll('.O3S9Rb');

        targetElement = elements[0]
        var parentElement = targetElement.parentNode.parentNode;



        var keyword = ''
        const defaultConfig = [
            { name: '百度', url: 'https://www.baidu.com/s?wd=' },
            { name: '豆瓣', url: 'https://www.douban.com/search?source=suggest&q=' },
            { name: '知乎', url: 'https://www.zhihu.com/search?type=content&q=' },
            { name: '必应', url: 'https://www.bing.com/search?cc=sg&q=' },
            { name: 'V2EX', url: 'https://www.google.com/search?q=site:v2ex.com/t%20' },
        ];
        // 获取用户自定义的 data 数组,如果没有则使用默认的数组
        const customCofig=GM_getValue('data')||defaultConfig
        console.log('GM_getValue',GM_getValue('data'))
      var data =customCofig
        //  var data =defaultConfig;
        // 添加一个编辑按钮
               const editButton=createLinkElement({url:'www.baidu.com',name:'编辑'},'button')
parentElement.append(editButton)



     function createLinkElement(item, elementType = 'a') {
            var newElement = document.createElement(elementType);
            newElement.className = 'zItAnd FOU1zf';
            newElement.href = item.url;
            newElement.target = '_blank';

            var newDiv = document.createElement('div');
            newDiv.className = 'O3S9Rb';
            newDiv.textContent = item.name;

            newElement.appendChild(newDiv);

            return newElement;
        }



        for (var i = 0; i < data.length; i++) {
            var item = data[i];

            const newElement=createLinkElement(item)

            newElement.addEventListener('mouseenter', function (event) {
                var inputElement = document.querySelector('#APjFqb');
                keyword = inputElement.value
                console.log(keyword)
                // 修改链接的href属性,使其包含输入框中的关键词


                var url = event.target.href;
                var lastEqualSign = url.lastIndexOf('=');
                var result = url.substring(0, lastEqualSign + 1); // +1 是为了保留等号

                event.target.href = result + keyword
                console.log(event.target.href)
            });

            parentElement.insertBefore(newElement, targetElement.parentNode);
        }



//编辑按钮功能

           //       GM_setValue('data', newData);
       //     GM_setValue('data', []);
const styles = {
    dialog: "z-index:100;display: none; position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); border: 1px solid black; padding: 20px; background-color: white; box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);",
    button: "margin-right: 10px;  background-color: white; border: 1px soid black; border-radius: 5px; cursor: pointer; transition: background-color 0.2s;",
    input: "margin-right: 10px;",

    configContainer: "margin-bottom: 10px;"
};



// 创建dialog div
let dialog = document.createElement("div");
dialog.id = "dialog";
dialog.style.cssText = styles.dialog;
document.body.appendChild(dialog);


// 创建关闭button
let closeButton = document.createElement("button");
closeButton.innerText = "保存";
 closeButton.style.cssText = styles.button;
closeButton.addEventListener('click', function () {
    dialog.style.display = 'none'; // 隐藏对话框
     GM_setValue('data', customCofig);
    location.reload();

});

// 创建"还原"按钮
let restoreButton = document.createElement("button");
        restoreButton.style.cssText = styles.button;
restoreButton.innerText = "还原";
// 添加"还原"按钮点击事件
restoreButton.addEventListener('click', function () {
       dialog.style.display = 'none'; // 隐藏对话框
     GM_setValue('data', defaultConfig);
    location.reload();

});
     const btnBox=document.createElement('div')
     btnBox.style.marginBottom='10px'
      btnBox.appendChild(closeButton);

 btnBox.appendChild(restoreButton);

// 创建"新增"按钮
let addButton = document.createElement("button");
addButton.innerText = "新增";
addButton.style.cssText = styles.button;
btnBox.appendChild(addButton);
// 添加"新增"按钮点击事件
addButton.addEventListener('click', function () {
    let configContainer = document.createElement("div");
    configContainer.style.cssText = styles.configContainer;

    let nameInput = document.createElement("input");
    nameInput.style.cssText = styles.input;
    configContainer.appendChild(nameInput);

    let urlInput = document.createElement("input");
    urlInput.style.cssText = styles.input;
    configContainer.appendChild(urlInput);

    let updateButton = document.createElement("button");
    updateButton.innerText = "保存";
updateButton.style.cssText =styles.button;
    updateButton.addEventListener('click', function () {
        // 将新的配置项添加到customCofig中
        customCofig.push({ name: nameInput.value, url: urlInput.value });
        dialog.style.display = 'none'; // 隐藏对话框
        editButton.click(); // 重新打开对话框以刷新内容
    });
    configContainer.appendChild(updateButton);

    dialog.appendChild(configContainer);
});

dialog.appendChild(btnBox);



// 添加点击事件
editButton.addEventListener('click', function () {
    dialog.style.display = 'block'; // 显示对话框

    // 清空对话框,但保留关闭按钮
    while (dialog.children.length > 1) {
        dialog.removeChild(dialog.lastChild);
    }

    // 根据customCofig生成编辑区域
    customCofig.forEach((config, index) => {
        let configContainer = document.createElement("div");
        configContainer.style.cssText = styles.configContainer;

        let nameInput = document.createElement("input");
        nameInput.value = config.name;
        nameInput.style.cssText = styles.input;
        configContainer.appendChild(nameInput);

        let urlInput = document.createElement("input");
        urlInput.value = config.url;
        urlInput.style.cssText = styles.input;
        configContainer.appendChild(urlInput);

        let updateButton = document.createElement("button");
        updateButton.innerText = "更新";
        updateButton.style.cssText = styles.button;
        updateButton.addEventListener('click', function () {
            config.name = nameInput.value;
            config.url = urlInput.value;
        });
        configContainer.appendChild(updateButton);

        let deleteButton = document.createElement("button");
        deleteButton.innerText = "删除";
        deleteButton.style.cssText = styles.button;
        deleteButton.addEventListener('click', function () {
            customCofig.splice(index, 1);
            dialog.style.display = 'none'; // 隐藏对话框
            editButton.click(); // 重新打开对话框以刷新内容
        });
        configContainer.appendChild(deleteButton);

        dialog.appendChild(configContainer);
    });
});

    }, 200)



})();