// ==UserScript== // @name 阻止网站监听返回事件 // @version 0.1.1 // @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 // @run-at document-start // @downloadURL none // ==/UserScript== (function() { HISTORY_OBJECTS = [window.history.pushState, window.history.replaceState, window.history.length]; RUN_TIMES = 0; sinaMaMa = function() { RUN_TIMES++; if (RUN_TIMES >= 50) { window.clearInterval(SINA_SIMA); } var isTypeError = false; try { window.history.pushState('新浪的🐎💀了吗'); // 针对💀🐎新浪把 Object 直接替换掉的方法 } catch (TypeError) { isTypeError = true; } if (!isTypeError) { hisPush = hisReplace = window.history.pushState = window.history.replaceState = function() { throw new Error('新浪💀🐎'); }; console.log('新浪🐎🐎美丽'); window.clearInterval(SINA_SIMA); } }; SINA_SIMA = setInterval(sinaMaMa, 50); window.addEventListener("popstate", function (event) { alert('a'); console.log(event); alert(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); })();