Google Search Result Toggle Chinese/English

Toggle the result language for Google searches

当前为 2019-08-03 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Google Search Result Toggle Chinese/English
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Toggle the result language for Google searches
// @author       tgxhx
// @match        https://www.google.com/search*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const $ = document.querySelector;
    const settingsBtn = $('#ab_options');
    const settingsBtnParent = settingsBtn.parentElement;
    const a = document.createElement('a');
    const query = new URLSearchParams(decodeURIComponent(location.search));
    const isChinese = (query.get('hl') || '').toLowerCase() === 'zh-cn';
    if (isChinese) {
        query.delete('hl');
        query.delete('lr');
        query.delete('tbs');
        a.textContent = 'English';
    } else {
        query.set('hl', 'zh-cn');
        query.set('lr', 'lang_zh-CN|lang_zh-TW');
        query.set('tbs', 'lr:lang_1zh-CN|lang_1zh-TW');
        a.textContent = 'Chinese';
    }

    const href = `${location.origin}${location.pathname}?${query.toString()}`;
    a.setAttribute('href', href);
    a.classList.add('hdtb-tl');
    a.style.cssText = 'color: #5f6368;text-decoration: none;'
    settingsBtnParent.insertBefore(a, settingsBtn)
})();