Greasy Fork

Greasy Fork is available in English.

阻止网站监听返回事件

某些中国特色垃圾网站会通过监听返回事件来吸引用户流量到自己首页(比如 `*.news.sina.cn`),这个脚本则会替换掉原先网页定义的返回事件。

目前为 2019-02-18 提交的版本,查看 最新版本

// ==UserScript==
// @name         阻止网站监听返回事件
// @version      0.1.2
// @author       Anonymous
// @description  某些中国特色垃圾网站会通过监听返回事件来吸引用户流量到自己首页(比如 `*.news.sina.cn`),这个脚本则会替换掉原先网页定义的返回事件。
// @namespace    https://misakamikoto.example.org/
// @match        https://*.sina.cn/*
// @match        https://*.sina.com/*
// @match        https://*.sina.com.cn/*
// @match        http://*.sina.cn/*
// @match        http://*.sina.com/*
// @match        http://*.sina.com.cn/*
// @grant         none
// @require      https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js
// @run-at       document-start
// ==/UserScript==

(function() {
    var pushState = window.history.pushState;
    var replaceState = window.history.replaceState;
    HISTORY_OBJECTS = [pushState, replaceState, window.history.length];
    RUN_TIMES = 0;
    replaceFunctions();
    //hisPush = hisReplace = function() { throw new Error('新浪💀🐎'); };
    sinaMaMa = function() {
        if (RUN_TIMES >= 50) {
            window.clearInterval(SINA_SIMA);
        }
        RUN_TIMES++;
        var isTypeError = false;
        try {
            window.history.pushState('新浪的🐎💀了吗'); // 针对💀🐎新浪把 Object 直接替换掉的方法
        } catch (TypeError) {
            isTypeError = true;
        }
        if (!isTypeError) {
            replaceFunctions();
            console.log('新浪🐎🐎美丽');
            window.clearInterval(SINA_SIMA);
        }
    };
    SINA_SIMA = setInterval(sinaMaMa, 50);
    window.addEventListener("popstate", function (event) {
        var backLength = window.history.length - HISTORY_OBJECTS[2];
        if (backLength > 0) {
            var state = {
                title: "title",
                url: "#"
            };
            HISTORY_OBJECTS[0](state, "title", "#");
            window.history.back(0 - backLength - 1);
        }
    }, false);

    function replaceFunctions() {
        window.history.pushState = hisPush = function(...args) {
            var obj = HISTORY_OBJECTS[0];
            if (args.length <= 1) {
                throw new TypeError();
            }
            console.log(args);
            args_length = args.length;
            var result_msg = '';
            for (var i = 0; i < args_length; i++) {
                var now_str = args[i];
                if (now_str == null || now_str == '') { now_str = null }
                result_msg += '[' + i + '] ' + now_str + '\n';
            }
            /*swal({
            title: "该网站疑似恶意修改返回记录",
            text: window.location.host + " 正在尝试加入或修改如下数据:\n" + result_msg + "已被拦截。"
        });*/
        };
        window.history.replaceState = hisReplace = function(...args) {
            var obj = HISTORY_OBJECTS[1];
            if (args.length <= 1) {
                throw new TypeError();
            }
            console.log(args);
            args_length = args.length;
            var result_msg = '';
            for (var i = 0; i < args_length; i++) {
                var now_str = args[i];
                if (now_str == null || now_str == '') { now_str = null }
                result_msg += '[' + i + '] ' + now_str + '\n';
            }
            swal({
                title: "该网站疑似恶意修改返回记录",
                text: window.location.host + " 正在尝试加入或修改如下数据:\n" + result_msg + "已被拦截。"
            });
        };
    }
})();