百度搜索跳转到google

百度搜索跳转到google,快捷跳转

// ==UserScript==
// @name         百度搜索跳转到google
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  百度搜索跳转到google,快捷跳转
// @author       You
// @match        *://www.baidu.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=baidu.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    
    // 使用常量存储样式
    const BUTTON_STYLES = {
        width: '100px',
        height: '40px',
        backgroundColor: '#546FE9',
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        borderRadius: '5px',
        marginLeft: '10px',
        position: 'absolute',
        top: '0px',
        right: '-120px',
        color: '#fff',
        cursor: 'pointer',
        fontSize: '14px',
       
    };

    const DROPDOWN_STYLES = {
        position: 'absolute',
        top: '100%',
        left: '0',
        backgroundColor: '#fff',
        boxShadow: '0 2px 5px rgba(0,0,0,0.2)',
        display: 'none',
        width: '100%',
        zIndex: '1000'
    };

    const DROPDOWN_ITEM_STYLES = {
        padding: '8px 12px',
        cursor: 'pointer',
        color: '#333',
        backgroundColor: '#fff',
        hover: {
            backgroundColor: '#f0f0f0'
        }
    };

    // 创建按钮
    function createGoogleButton() {
        const buttonContainer = document.createElement('div');
        Object.assign(buttonContainer.style, BUTTON_STYLES);
        
        buttonContainer.innerHTML = `
            <span style="flex: 1; text-align: left; padding-left: 15px;" class="google-text">Google</span>
            <div style="width: 1px; height: 20px; background-color: rgba(255,255,255,0.3);"></div>
            <span style="padding: 0 8px; cursor: pointer; transform: scaleY(0.6);" class="arrow">▼</span>
        `;
        
        const dropdown = document.createElement('div');
        Object.assign(dropdown.style, DROPDOWN_STYLES);
        
        const sites = [
            { name: 'Bilibili', url: 'https://search.bilibili.com/all?keyword=' },
            { name: 'GitHub', url: 'https://github.com/search?q=' }
        ];
        
        sites.forEach(site => {
            const item = document.createElement('div');
            Object.assign(item.style, DROPDOWN_ITEM_STYLES);
            item.textContent = site.name;
            item.addEventListener('mouseover', () => {
                item.style.backgroundColor = DROPDOWN_ITEM_STYLES.hover.backgroundColor;
            });
            item.addEventListener('mouseout', () => {
                item.style.backgroundColor = '#fff';
            });
            item.addEventListener('click', (e) => {
                e.stopPropagation();
                const searchText = document.querySelector('#kw')?.value || '';
                window.open(`${site.url}${encodeURIComponent(searchText)}`);
            });
            dropdown.appendChild(item);
        });
        
        buttonContainer.appendChild(dropdown);
        
        const arrow = buttonContainer.querySelector('.arrow');
        const googleText = buttonContainer.querySelector('.google-text');
        
        arrow.addEventListener('mouseenter', () => {
            dropdown.style.display = 'block';
        });
        
        googleText.addEventListener('mouseenter', () => {
            dropdown.style.display = 'none';
        });
        
        buttonContainer.addEventListener('mouseleave', () => {
            dropdown.style.display = 'none';
        });
        
        buttonContainer.addEventListener('click', (e) => {
            if (!e.target.classList.contains('arrow')) {
                const searchText = document.querySelector('#kw')?.value || '';
                window.open(`https://www.google.com/search?q=${encodeURIComponent(searchText)}`);
            }
        });
        
        return buttonContainer;
    }

    // 初始化
    function init() {
        const searchButton = document.querySelector('#su');
        if (!searchButton) return;

        const parent = searchButton.parentElement;
        parent.style.position = 'relative';

        const googleButton = createGoogleButton();
        const searchInput = document.querySelector('#kw');

        googleButton.addEventListener('click', () => {
            const searchText = searchInput?.value || '';
            window.open(`https://www.google.com/search?q=${encodeURIComponent(searchText)}`);
        });

        parent.appendChild(googleButton);
    }

    // 等待DOM加载完成后执行
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址