GreasyFork Search

To search scripts using Google Search

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         GreasyFork Search
// @namespace    http://tampermonkey.net/
// @version      0.4.2
// @description  To search scripts using Google Search
// @author       CY Fung
// @match        https://greasyfork.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=greasyfork.org
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
    'use strict';

    let input = document.querySelector('form input[name="q"]');
    if (!(input instanceof HTMLInputElement)) return;
    let form = input.closest('form');
    if (!(form instanceof HTMLFormElement)) return;


    let locales = [...document.querySelectorAll('select#language-selector-locale > option')].map(x => x.value)

    document.head.appendChild(document.createElement('style')).textContent = `


    @keyframes rs1tmAnimation {
        0% {
            background-position-x: 3px;
        }
        100% {
            background-position-x: 4px;
        }
    }

    form.rs1tm{
        position: fixed;
        top:-300px;
        left:-300px;
        width: 1px;
        height: 1px;
        contain: strict;
        display: flex;
        overflow: hidden;
        animation: rs1tmAnimation 1ms linear 1ms 1 normal forwards;
    }

    `
    document.addEventListener('animationstart', (evt) => {

        if (evt.animationName === 'rs1tmAnimation') {
            const target = evt.target;
            target && target.parentNode && target.remove();
        }

    }, true);

    form.addEventListener('submit', function (evt) {

        let form = evt.target;
        if (!(form instanceof HTMLFormElement)) return;
        let input = form.querySelector('input[name="q"]');
        if (!(input instanceof HTMLInputElement)) return;

        if (form.classList.contains('rs1tm')) return;

        let value = input.value;
        const lang = document.documentElement.lang || '';

        let useLang = false;


        let u = 0;
        let isGoogleSearch = false;

        let sites = [];

        const split = value.split(/\s+/);
        let forceLang = 'all';
        let reformedSplit = [];
        for (const s of split) {

            if (!isGoogleSearch && /^[a-z][a-z0-9_-]{2,}(\.[a-z][a-z0-9_-]{2,})*(\.[a-z-]{2,4})+$/.test(s)) {
                sites.push(s);
            } else if (s === 'js') {
                forceLang = 'js'; reformedSplit.push(s);
            } else if (s === 'css') {
                forceLang = 'css'; reformedSplit.push(s);
            } else if (s === '"js"') {
                reformedSplit.push('js');
            } else if (s === '"css"') {
                reformedSplit.push('css');
            } else if (u === 0 && s === 'g') {
                isGoogleSearch = true;
            } else if (locales.indexOf(s) >= 0 || s === lang) {
                useLang = s;
            } else {
                reformedSplit.push(s);
            }
            u++;
        }

        value = reformedSplit.join(' ')

        let onlySite = '';

            if (sites.length === 1 && sites[0]) {
                onlySite = sites[0];
            }


        if (isGoogleSearch && value) {
            let q = value.replace('g ', '');

            let m = "-inurl%3A%22%2Fusers%2F%22+-inurl%3A%22%2Fdiscussions%22-inurl%3A%22%2Fstats%22+-inurl%3A%22%2Ffeedback%22+-inurl%3A%22%2Fcode%22+-inurl%3A%22q%3D%22+-inurl%3A%22%2Fby-site%2F%22+inurl%3A%22%2Fscripts%2F%22+site%3Agreasyfork.org";



            let lr = useLang ? `&lr=lang_${useLang}` : '';
            evt.preventDefault();
            evt.stopImmediatePropagation();
            evt.stopPropagation();
            location.href = `https://www.google.com/search?q=${encodeURIComponent(q)}+${m}${lr}`

        } else if (value ||  onlySite) {



            let newForm = document.createElement('form');
            newForm.className = 'rs1tm';
            const copyAttr = (x) => {
                let t = form.getAttribute(x);
                if (typeof t === 'string') newForm.setAttribute(x, t);
            }
            copyAttr('action');
            copyAttr('accept-charset');
            copyAttr('method');
            newForm.innerHTML = `<input name="q" type="hidden" value="" /><input name="site" type="hidden" /><input name="language" type="hidden" value="all" />`


            const nq = newForm.querySelector('input[name="q"]');
            const language = newForm.querySelector('input[name="language"]');
            const site = newForm.querySelector('input[name="site"]');

            value = value.replace(/\s+/g, ' ');
            site.value == onlySite;

            if (form.getAttribute('action') === `/${lang}/scripts` && useLang && useLang !== lang) {
                form.setAttribute('action', `/${useLang}/scripts`)
            }

            if (site.value === '') site.remove();

            nq.value = value;

            language.value = forceLang;

            if (language.value === '') language.remove();

            evt.preventDefault();
            evt.stopImmediatePropagation();
            evt.stopPropagation();

            form.parentNode.insertBefore(newForm, form);
            newForm.submit();


        } else {
            evt.preventDefault();
            evt.stopImmediatePropagation();
            evt.stopPropagation();
        }

    })

    // Your code here...
})();