// ==UserScript== // @name 强制页面在新标签页打开 // @namespace http://tampermonkey.net/ // @version 0.2 // @description 为了避免误触需要在油猴菜单里输入生效的dom元素的选择器,如果想要整个网页都生效,只需填入 body 即可 // @author meteora // @match http://*/* // @license MIT // @match https://*/* // @grant GM_registerMenuCommand // @grant GM_unregisterMenuCommand // @grant unsafeWindow // @downloadURL none // ==/UserScript== ;(function () { "use strict" //排除iframe if (window.self !== window.top) { return } let domListText = localStorage.getItem("domListText") ? localStorage.getItem("domListText") : "" let domList = [] function hookATag() { // 获取页面上的所有链接元素 for (let domListElement of domList) { let links = domListElement.getElementsByTagName("a") for (let i = 0; i < links.length; i++) { // 遍历每个链接元素并添加目标属性 links[i].setAttribute("target", "_blank") //给标签添加点击事件,点击后标红 links[i].addEventListener("click", function () { this.style.color = "darkred" }) } } } function hookWindowOpen() { // 保存原始的 window.open 方法的引用 let originalOpen = unsafeWindow.open // 重写 window.open 方法 unsafeWindow.open = function (url, target, features) { // 在新标签页中打开链接 originalOpen.call(this, url, "_blank", features) } } //监听dom节点变化以应对异步刷新的场景,一旦dom节点发生变化则重新执行hookPage function hookPageWhenDomChange() { let MutationObserver = window.MutationObserver || window.WebKitMutationObserver let observer = new MutationObserver(function (mutations) { mutations.forEach(function (mutation) { hookATag() }) }) observer.observe(document.body, { childList: true, // 观察目标子节点的变化,是否有添加或者删除 subtree: true, // 观察后代节点,默认为 false attributes: false, // 观察属性变动 }) } //显示文本输入框浮窗,用于接收用户输入的需要生效的dom选择器 function showInputTextarea() { const dom = `