// ==UserScript== // @name 强制所有链接在当前标签打开 // @namespace https://greasyfork.org/zh-CN/scripts/446917-%E5%BC%BA%E5%88%B6%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%9C%A8%E5%BD%93%E5%89%8D%E6%A0%87%E7%AD%BE%E6%89%93%E5%BC%80 // @version 0.25 // @license MIT // @description 强制所有链接在当前标签打开。 // @grant unsafeWindow // @author meteora // @include *://* // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @downloadURL none // ==/UserScript== (function () { "use strict"; //排除iframe if (window.self !== window.top) { return; } // 白名单管理 let enable = localStorage.getItem("8y8Av7o3Rp") === "enable"; if (!enable) { GM_registerMenuCommand("开启", () => { localStorage.setItem("8y8Av7o3Rp", "enable"); unsafeWindow.location.reload(); }); return; } else { GM_registerMenuCommand("关闭", () => { localStorage.setItem("8y8Av7o3Rp", "disable"); unsafeWindow.location.reload(); }); } document.head.appendChild(document.createElement("base")).target = "_self"; const listener = function (e) { let dom = e.target; if (dom.nodeName === "A") { dom.target = "_self"; return; } //循环迭代获取父节点 for (let i = 0; i < 5; i++) { dom = dom.parentNode; //如果是a标签 if (dom.nodeName === "A") { dom.target = "_self"; return; } } }; document.body.addEventListener("click", listener, true); //对于调用window.open跳转的 const open = unsafeWindow.open; const newOpen = function ( url = false, target = "_self", windowFeatures = false, ) { if (url && windowFeatures) { open(url, "_self", windowFeatures); } else if (url) { open(url, "_self"); } else { open(); } }; Object.defineProperty(unsafeWindow, "open", { value: newOpen, }); })();