Site search on Google

Adds a button to search results under specific websites with Google

目前為 2023-04-12 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Site  search on Google
// @version      1.0.1.3
// @license      MIT
// @description  Adds a button to search results under specific websites  with Google
// @author       IcyBearLovesSleep
// @namespace     https://github.com/IcyBearLovesSleep/reddit-search-on-google-2
// @match              https://*.google.com/search*
// @match              https://*.google.ad/search*
// @match              https://*.google.ae/search*
// @match              https://*.google.com.af/search*
// @match              https://*.google.com.ag/search*
// @match              https://*.google.com.ai/search*
// @match              https://*.google.al/search*
// @match              https://*.google.am/search*
// @match              https://*.google.co.ao/search*
// @match              https://*.google.com.ar/search*
// @match              https://*.google.as/search*
// @match              https://*.google.at/search*
// @match              https://*.google.com.au/search*
// @match              https://*.google.az/search*
// @match              https://*.google.ba/search*
// @match              https://*.google.com.bd/search*
// @match              https://*.google.be/search*
// @match              https://*.google.bf/search*
// @match              https://*.google.bg/search*
// @match              https://*.google.com.bh/search*
// @match              https://*.google.bi/search*
// @match              https://*.google.bj/search*
// @match              https://*.google.com.bn/search*
// @match              https://*.google.com.bo/search*
// @match              https://*.google.com.br/search*
// @match              https://*.google.bs/search*
// @match              https://*.google.bt/search*
// @match              https://*.google.co.bw/search*
// @match              https://*.google.by/search*
// @match              https://*.google.com.bz/search*
// @match              https://*.google.ca/search*
// @match              https://*.google.cd/search*
// @match              https://*.google.cf/search*
// @match              https://*.google.cg/search*
// @match              https://*.google.ch/search*
// @match              https://*.google.ci/search*
// @match              https://*.google.co.ck/search*
// @match              https://*.google.cl/search*
// @match              https://*.google.cm/search*
// @match              https://*.google.cn/search*
// @match              https://*.google.com.co/search*
// @match              https://*.google.co.cr/search*
// @match              https://*.google.com.cu/search*
// @match              https://*.google.cv/search*
// @match              https://*.google.com.cy/search*
// @match              https://*.google.cz/search*
// @match              https://*.google.de/search*
// @match              https://*.google.dj/search*
// @match              https://*.google.dk/search*
// @match              https://*.google.dm/search*
// @match              https://*.google.com.do/search*
// @match              https://*.google.dz/search*
// @match              https://*.google.com.ec/search*
// @match              https://*.google.ee/search*
// @match              https://*.google.com.eg/search*
// @match              https://*.google.es/search*
// @match              https://*.google.com.et/search*
// @match              https://*.google.fi/search*
// @match              https://*.google.com.fj/search*
// @match              https://*.google.fm/search*
// @match              https://*.google.fr/search*
// @match              https://*.google.ga/search*
// @match              https://*.google.ge/search*
// @match              https://*.google.gg/search*
// @match              https://*.google.com.gh/search*
// @match              https://*.google.com.gi/search*
// @match              https://*.google.gl/search*
// @match              https://*.google.gm/search*
// @match              https://*.google.gr/search*
// @match              https://*.google.com.gt/search*
// @match              https://*.google.gy/search*
// @match              https://*.google.com.hk/search*
// @match              https://*.google.hn/search*
// @match              https://*.google.hr/search*
// @match              https://*.google.ht/search*
// @match              https://*.google.hu/search*
// @match              https://*.google.co.id/search*
// @match              https://*.google.ie/search*
// @match              https://*.google.co.il/search*
// @match              https://*.google.im/search*
// @match              https://*.google.co.in/search*
// @match              https://*.google.iq/search*
// @match              https://*.google.is/search*
// @match              https://*.google.it/search*
// @match              https://*.google.je/search*
// @match              https://*.google.com.jm/search*
// @match              https://*.google.jo/search*
// @match              https://*.google.co.jp/search*
// @match              https://*.google.co.ke/search*
// @match              https://*.google.com.kh/search*
// @match              https://*.google.ki/search*
// @match              https://*.google.kg/search*
// @match              https://*.google.co.kr/search*
// @match              https://*.google.com.kw/search*
// @match              https://*.google.kz/search*
// @match              https://*.google.la/search*
// @match              https://*.google.com.lb/search*
// @match              https://*.google.li/search*
// @match              https://*.google.lk/search*
// @match              https://*.google.co.ls/search*
// @match              https://*.google.lt/search*
// @match              https://*.google.lu/search*
// @match              https://*.google.lv/search*
// @match              https://*.google.com.ly/search*
// @match              https://*.google.co.ma/search*
// @match              https://*.google.md/search*
// @match              https://*.google.me/search*
// @match              https://*.google.mg/search*
// @match              https://*.google.mk/search*
// @match              https://*.google.ml/search*
// @match              https://*.google.com.mm/search*
// @match              https://*.google.mn/search*
// @match              https://*.google.ms/search*
// @match              https://*.google.com.mt/search*
// @match              https://*.google.mu/search*
// @match              https://*.google.mv/search*
// @match              https://*.google.mw/search*
// @match              https://*.google.com.mx/search*
// @match              https://*.google.com.my/search*
// @match              https://*.google.co.mz/search*
// @match              https://*.google.com.na/search*
// @match              https://*.google.com.ng/search*
// @match              https://*.google.com.ni/search*
// @match              https://*.google.ne/search*
// @match              https://*.google.nl/search*
// @match              https://*.google.no/search*
// @match              https://*.google.com.np/search*
// @match              https://*.google.nr/search*
// @match              https://*.google.nu/search*
// @match              https://*.google.co.nz/search*
// @match              https://*.google.com.om/search*
// @match              https://*.google.com.pa/search*
// @match              https://*.google.com.pe/search*
// @match              https://*.google.com.pg/search*
// @match              https://*.google.com.ph/search*
// @match              https://*.google.com.pk/search*
// @match              https://*.google.pl/search*
// @match              https://*.google.pn/search*
// @match              https://*.google.com.pr/search*
// @match              https://*.google.ps/search*
// @match              https://*.google.pt/search*
// @match              https://*.google.com.py/search*
// @match              https://*.google.com.qa/search*
// @match              https://*.google.ro/search*
// @match              https://*.google.ru/search*
// @match              https://*.google.rw/search*
// @match              https://*.google.com.sa/search*
// @match              https://*.google.com.sb/search*
// @match              https://*.google.sc/search*
// @match              https://*.google.se/search*
// @match              https://*.google.com.sg/search*
// @match              https://*.google.sh/search*
// @match              https://*.google.si/search*
// @match              https://*.google.sk/search*
// @match              https://*.google.com.sl/search*
// @match              https://*.google.sn/search*
// @match              https://*.google.so/search*
// @match              https://*.google.sm/search*
// @match              https://*.google.sr/search*
// @match              https://*.google.st/search*
// @match              https://*.google.com.sv/search*
// @match              https://*.google.td/search*
// @match              https://*.google.tg/search*
// @match              https://*.google.co.th/search*
// @match              https://*.google.com.tj/search*
// @match              https://*.google.tl/search*
// @match              https://*.google.tm/search*
// @match              https://*.google.tn/search*
// @match              https://*.google.to/search*
// @match              https://*.google.com.tr/search*
// @match              https://*.google.tt/search*
// @match              https://*.google.com.tw/search*
// @match              https://*.google.co.tz/search*
// @match              https://*.google.com.ua/search*
// @match              https://*.google.co.ug/search*
// @match              https://*.google.co.uk/search*
// @match              https://*.google.com.uy/search*
// @match              https://*.google.co.uz/search*
// @match              https://*.google.com.vc/search*
// @match              https://*.google.co.ve/search*
// @match              https://*.google.vg/search*
// @match              https://*.google.co.vi/search*
// @match              https://*.google.com.vn/search*
// @match              https://*.google.vu/search*
// @match              https://*.google.ws/search*
// @match              https://*.google.rs/search*
// @match              https://*.google.co.za/search*
// @match              https://*.google.co.zm/search*
// @match              https://*.google.co.zw/search*
// @match              https://*.google.cat/search*
// @run-at       document-end
// ==/UserScript==

// put in desire site searches
const siteData = [
    {
        site:"reddit.com",
        name:"Reddit"
    },
    {
        site:"youtube.com",
        name:"Youtube"
    },
    {
        site:"wikipedia.com",
        name:"Wikipedia"
    }
]

// Change this to false if you don't want an icon
const useIcon = true;
// Change this to true if you want to add the button to the right of the 'Tools' button
const appendRight = false;

const queryRegex = /q=[^&]+/g;
const siteRegex = /\+site(?:%3A|\:).+\.[^&+]+/g;
let redditIcon = '<svg foscusable="false" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16"><path d="M0 7.59c.1-.26.15-.54.28-.77.57-1 1.83-1.29 2.79-.67.09.06.15.06.24 0A8.94 8.94 0 017.5 5c.1 0 .15-.04.19-.14l1.17-3.32.05-.15 1.23.3c.56.12 1.12.25 1.68.4.1.02.15 0 .2-.09a1.66 1.66 0 013.06 1.02c-.06.8-.72 1.46-1.5 1.52-.85.06-1.6-.47-1.77-1.28-.04-.17-.1-.23-.27-.27L9.54 2.5l-.88 2.48c.37.05.72.08 1.07.14 1.05.17 2.04.5 2.96 1.04.09.05.14.05.23 0a1.99 1.99 0 012.34 3.2c-.04.04-.08.11-.08.17a3.67 3.67 0 01-.9 2.73 6.36 6.36 0 01-2.9 1.92c-2.47.86-4.93.8-7.35-.22a5.89 5.89 0 01-2.48-1.9A3.56 3.56 0 01.8 9.5c0-.04 0-.1-.03-.11a2.1 2.1 0 01-.76-1.32v-.01-.47zm7.72 6.22c1.5 0 2.67-.21 3.73-.66a5.2 5.2 0 002.15-1.54c.83-1.06.85-2.3.06-3.38-.4-.56-.93-.97-1.52-1.3a8.15 8.15 0 00-3.44-.97 9.04 9.04 0 00-4.17.63c-.83.35-1.58.82-2.15 1.54a2.66 2.66 0 00-.08 3.38c.41.57.95.99 1.55 1.32 1.29.7 2.68.97 3.87.98zm5.7-11.63a.7.7 0 00-.7.7c0 .39.32.7.7.71a.7.7 0 00.72-.72.7.7 0 00-.72-.7zm1.44 6.2a1 1 0 00-.05-1.22c-.29-.34-.79-.48-1.14-.3.49.43.9.92 1.19 1.53zM1.12 8.38c.3-.6.7-1.09 1.19-1.52-.35-.16-.81-.03-1.1.29-.3.33-.34.83-.09 1.23z"/><path d="M7.98 13.1a4.77 4.77 0 01-1.99-.45 3.58 3.58 0 01-.65-.45c-.24-.2-.24-.5-.04-.7.19-.2.47-.19.7.01.33.3.73.44 1.16.52.76.14 1.52.11 2.24-.17.24-.1.45-.24.66-.38.22-.16.5-.16.68.03.18.2.17.5-.04.68-.43.39-.95.62-1.5.73-.4.08-.81.12-1.22.18zM11.67 9.04a1.3 1.3 0 01-2.6 0 1.3 1.3 0 012.6 0zM4.46 9.04c0-.73.6-1.3 1.32-1.3a1.3 1.3 0 11-1.32 1.3z"/></svg>';
const isImageSearch = /[?&]tbm=isch/.test(location.search);

if (typeof trustedTypes !== 'undefined') {
    const policy = trustedTypes.createPolicy('html', { createHTML: input => input });
    redditIcon = policy.createHTML(redditIcon);
}

(function () {
    const elements = document.getElementsByClassName("MUFPAc");

    if (elements.length > 0) {
        for (let i = 0; i < siteData.length; i++) {
            let Url = '+site%3A'+siteData[i].site;

            // Creating the element
            let el = document.createElement('div');
            el.className = 'hdtb-mitem';
            const link = document.createElement('a');
            if(siteData[i].name=="Reddit"){
                // Adding the svg icon
                if (useIcon) {
                    const span = document.createElement('span');
                    span.className = isImageSearch ? 'm3kSL' : 'bmaJhd iJddsb';
                    span.style.cssText = 'height:16px;width:16px';
                    span.innerHTML = redditIcon;
                    link.appendChild(span);
                }
            }

            // Hyperlink to add 'site:reddit.com' to the query
            link.appendChild(document.createTextNode(siteData[i].name));
            link.href = window.location.href.replace(queryRegex, (match) => {
                // Replaces the existing `site` flags
                return match.search(siteRegex) >= 0 ? match.replace(siteRegex, Url) : match + Url;
            });
            if (isImageSearch) {
                link.classList.add('NZmxZe');
                el = link;
            } else {
                el.appendChild(link);
            }

            // Inserting the element into Google search
            if (appendRight) {
                const toolsBtn = document.querySelector(isImageSearch ? '.ssfWCe' : '.t2vtad');
                toolsBtn.parentNode.insertBefore(el, toolsBtn.nextSibling);
            } else {
                const menuBar = document.querySelector(isImageSearch ? '.T47uwc' : '.MUFPAc');
                if (isImageSearch) {
                    menuBar.insertBefore(el, menuBar.children[menuBar.childElementCount - 1]);
                } else {
                    menuBar.appendChild(el);
                }
            }
        }
    }
    const elements2 = document.getElementsByClassName("MUFPAc");

    if (elements2.length > 0) {
        for (let i = 0; i < siteData.length; i++) {
            let Url = '+site%3A'+siteData[i].site;

            window.addEventListener("load", function () {
                const menu = document.querySelector('.nfdoRb')
                // creates button
                let button = document.createElement('a')
                button.className = 'zItAnd FOU1zf'
                button.href = window.location.href.replace(queryRegex, (match) => {
                    // Replaces the existing `site` flags
                    return match.search(siteRegex) >= 0 ? match.replace(siteRegex, Url) : match + Url;
                });

                button.innerText = siteData[i].name
                document.querySelector('.nfdoRb').append(button)
            }, false)
        }
    }
})();