No Spam Results in Google Search

No more spam results in your google search!

目前為 2022-05-01 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         No Spam Results in Google Search
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant       GM.setValue
// @grant       GM.getValue
// @grant       GM_getTab
// @grant       GM_saveTab
// @license MIT
// @noframes

// ==/UserScript==

'use strict';

;!(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;
        }

    }

    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;

        }


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

        if( tabObject.hitAt>1 && Date.now()- tabObject.hitAt<30*1000){

            //            elementCount<20

            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){
                        tabObject.hitAt=-1;
                        GM_saveTab(tabObject);
                        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]));

                    }

                }





            }






        }





        function onReady(){

            let emc = elementCount(document)

            if(emc<40){

                emc-=document.querySelectorAll('script, style, meta, title, head, link, noscript').length //remove count for non-visual elements

            }

            tabObject.timeline.push({type:'complete', shortHref: purify(location.href), pageHref:location.href, timeAt:Date.now(), elementCount: emc , histCount: history.length });
            //7 16 33
            //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);


                    })

                }

            }





        }




        ;(function uiMain(){


            if(document.documentElement == null) return window.requestAnimationFrame(uiMain)



            if (document.readyState !== 'loading') {
                onReady();
            } else {
                document.addEventListener('DOMContentLoaded', onReady);
            }



        })();




    })
})();