No Spam Results in Google Search

No more spam results in your google search!

目前为 2022-06-02 提交的版本。查看 最新版本

// ==UserScript==
// @name         No Spam Results in Google Search
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  No more spam results in your google search!
// @author       CY Fung
// @match        https://www.google.com/search?*
// @match        https://www.google.*.*/search?*
// @match        http://*/*
// @match        https://*/*
// @icon         https://gf.qytechs.cn/rails/active_storage/representations/redirect/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBeGswQVE9PSIsImV4cCI6bnVsbCwicHVyIjoiYmxvYl9pZCJ9fQ==--d840d20219ec465d39691a24251f7affa09443a5/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCem9MWm05eWJXRjBTU0lJY0c1bkJqb0dSVlE2RkhKbGMybDZaVjkwYjE5c2FXMXBkRnNIYVFISWFRSEkiLCJleHAiOm51bGwsInB1ciI6InZhcmlhdGlvbiJ9fQ==--e4f27e4605e5535222e2c2f9dcbe36f4bd1deb29/nospam.png
// @grant       GM.setValue
// @grant       GM.getValue
// @grant       GM.deleteValue
// @grant       GM_getTab
// @grant       GM_saveTab
// @grant       GM_registerMenuCommand
// @license MIT
// @noframes

// ==/UserScript==

'use strict';

const cssTxt = `
a[href^="http"].gs-spam-link-disable, a[href^="http"].gs-spam-link-disable>h3 {
    text-decoration: initial;
    color: #d88a8a;
    background: #605766;
    cursor: no-drop;
}
`;

(function () {
    'use strict';

    const [window, document, hostname, location, history] = new Function('return [window, document, location.hostname, location, history];')(); // real window & document object
    let isGoogleSearchPage = hostname.includes('.google')?/\w+\.(google|googleusercontent)\.com(\.\w{0,2}){0,2}/.test(hostname):false;

    function purify(url){
        let nt=null;
        url = `${url}`;
        try{
            nt=new URL(url);
        }catch(e){
            return url;
        }
        return `${nt.origin}${nt.pathname}`;
    }
    function stringify(obj){
        let res = JSON.stringify(obj);
        res=res.replace(/\,\{\"/g,',\n{');
        return res;
    }
    function elementCount(document){
        if(!document) return null;
        try{
            return document.all.length;
        }catch(e){
            return document.getElementsByTagName("*").length;
        }
    }
    function checkSpam(tabObject){
        if( tabObject.hitAt>1 && Date.now()- tabObject.hitAt<30*1000){
            let filteredTimeline = tabObject.timeline.filter(entry=>entry.timeAt>=tabObject.hitAt);
            if(filteredTimeline.length<10 && filteredTimeline.length>=1 && filteredTimeline[0].type=='click'){
                let completeOK=filteredTimeline.filter(entry=>entry.type=='complete').every(entry=>entry.elementCount<20);
                let renderOK= true;
                filteredTimeline.reduce((a,b)=>{
                    if(a && b && a.type=='start' && b.type=='complete' && a.shortHref === b.shortHref){
                        if(b.timeAt-a.timeAt>5000){
                            renderOK = false;
                        }
                    }
                    return b;
                },null);

                //alert([8989, filteredTimeline.length, completeOK, renderOK])
                if(completeOK && renderOK){

                    let histCount = filteredTimeline.filter(entry=>entry.type=='start').length
                    let jsHistCount = filteredTimeline.map(entry=>entry.histCount)
                    let jsHistCountInOrder = true;
                    jsHistCount.reduce((a,b)=>{
                        if(a>-1 && b>-1 && a<=b){
                        }else{
                            jsHistCountInOrder=false;
                        }
                        return b;
                    },0);
                    if(histCount>=2 && jsHistCountInOrder){

                        // in case redirect on the same domain
                        // it is allowed
                        let domains = {}
                        for(const entry of filteredTimeline){
                            let domain = new URL(entry.pageHref).hostname
                            if(entry.type!=='click') domains[domain]=(domains[domain]||0)+1
                        }
                        if(Object.keys(domains).length===1) return;

                        return {filteredTimeline, jsHistCount};
                    }

                }
            }
        }
    }

    GM_getTab(tabObject=>{

        if(!tabObject.timeline)tabObject.timeline=[];

        if(isGoogleSearchPage){
            //alert(`${tabObject.hitAt} - ${stringify(tabObject.timeline)}`);
            if(tabObject.hitAt<0){
                history.pushState({},null);
            }
            tabObject.timeline.length=0;
            tabObject.hitAt=0;
        }else if(!tabObject.hitAt){
            return;
        }else if(Date.now()-tabObject.hitAt>30*1000){
            tabObject.timeline.length=0;
            tabObject.hitAt=0;
            GM_saveTab(tabObject);
            return;
        }

        // either isGoogleSearchPage or hitTime < 30s

        tabObject.timeline.push({type:'start', shortHref: purify(location.href), pageHref:location.href, timeAt:Date.now() , histCount: history.length});
        //alert(stringify(tabObject))

        let resCheckSpam = checkSpam(tabObject);
        if(resCheckSpam){
            let {filteredTimeline, jsHistCount}=resCheckSpam;
            tabObject.hitAt=-1;
            GM_saveTab(tabObject);

            ;(async ()=>{
                try{
                    let spamDomains = (await GM.getValue('spamDomains', "")).split(',');

                    // Note awaiting the set -- required so the next get sees this set.
                    await GM.setValue('spamDomains', spamDomains+','+ new URL(filteredTimeline[0].shortHref).origin);

                }catch(e){}
            })();

            alert(`spam website "${filteredTimeline[0].shortHref}" detected \n go back ${purify(filteredTimeline[0].pageHref)}`);
            //location.href=filteredTimeline[0].pageHref;
            history.go(-(jsHistCount[jsHistCount.length-1] - jsHistCount[0]));
        }else{
            GM_saveTab(tabObject);
        }



        function addStyle (styleText) {
            const styleNode = document.createElement('style');
            styleNode.type = 'text/css';
            styleNode.textContent = styleText;
            document.documentElement.appendChild(styleNode);
            return styleNode;
        }


        function onReady(){

            let emc = elementCount(document);
            if(emc<40){ // including html, body, script, div, ... //7 16 33
                emc-=document.querySelectorAll('script, style, meta, title, head, link, noscript').length; //remove count for non-visual elements //6 12
            }
            tabObject.timeline.push({type:'complete', shortHref: purify(location.href), pageHref:location.href, timeAt:Date.now(), elementCount: emc , histCount: history.length });
            //alert(stringify(tabObject))
            GM_saveTab(tabObject);

            let hElems = null
            if(isGoogleSearchPage){
                hElems = [...document.querySelectorAll('a[href^="http"]')].filter(elm=>!/^https?\:\/\/\w+\.(google|googleusercontent)\.com(\.\w{0,2}){0,2}\//.test(elm.href))
                for(const hElem of hElems){
                    hElem.addEventListener('click',function(){
                        tabObject.hitAt = Date.now();
                        tabObject.timeline.push({type:'click', shortHref:purify(hElem.href), pageHref:location.href, timeAt:tabObject.hitAt , histCount: history.length});
                        //alert(stringify(tabObject))
                        GM_saveTab(tabObject);
                    })
                }

                ;(async () => {

                    let spamDomains = (await GM.getValue('spamDomains', "")).split(',');
                    console.log(spamDomains)
                    let spamLinks = hElems.filter( elm=> spamDomains.includes(new URL(elm.href).origin) )

                    if(spamLinks.length>0){

                        let noClickFunc=function(evt){if(evt){evt.returnValue = false; evt.preventDefault();} return false;}
                        for(const spamLink of spamLinks) {
                            spamLink.classList.add('gs-spam-link-disable');
                            spamLink.onclick=noClickFunc
                        }
                        addStyle (cssTxt);

                    }

                })();

                async function clearSpamDomainList(){
                    await GM.deleteValue('spamDomains');
                    for(const spamLink of hElems) {
                        spamLink.classList.remove('gs-spam-link-disable');
                        spamLink.onclick=null
                    }
                }
                GM_registerMenuCommand('Clear Spam Domain List', clearSpamDomainList);

            }else{

                function fx(){
                    document.removeEventListener('mousedown',fx,true);
                    document.removeEventListener('keydown',fx,true);
                    tabObject.timeline.length=0;
                    tabObject.hitAt=0;
                    GM_saveTab(tabObject);
                }

                document.addEventListener('mousedown',fx,true);
                document.addEventListener('keydown',fx,true);
            }

        }

        ;(function uiMain(){
            if(document.documentElement == null) return window.requestAnimationFrame(uiMain)
            if (document.readyState !== 'loading') {
                onReady();
            } else {
                document.addEventListener('DOMContentLoaded', onReady);
            }
        })();

    })
})();

QingJ © 2025

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