您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
综合处理链接新窗口打开及搜索框回车提交
当前为
// ==UserScript== // @name 当前页面打开链接 // @namespace https://greasyfork.org/zh-CN/scripts/497533 // @version 1.3 // @description 综合处理链接新窗口打开及搜索框回车提交 // @author YourName // @match *://*/* // @grant none // @run-at document-end // ==/UserScript== (function() { 'use strict'; // 通用属性清理函数(2025优化版) const cleanAttributes = (element) => { element.removeAttribute('target'); element.removeAttribute('onclick'); element.removeAttribute('onmousedown'); }; // 初始清理所有链接(逆向遍历提升性能) document.querySelectorAll('a').forEach(link => cleanAttributes(link)); // 动态元素处理器(支持Shadow DOM) const handleDynamicElements = (nodes) => { nodes.forEach(node => { if (node.nodeType === 1) { // 处理链接元素 if (node.matches('a')) cleanAttributes(node); // 处理表单元素(2025新增) if (node.matches('input[type="text"], input[type="search"], textarea')) { node.addEventListener('keydown', function(e) { if (e.key === 'Enter') { const form = node.closest('form'); if (form) { form.target = '_self'; form.removeAttribute('target'); } } }); } } }); }; // MutationObserver配置(增强版) const observer = new MutationObserver(mutations => { mutations.forEach(({ addedNodes }) => handleDynamicElements(addedNodes)); }); observer.observe(document, { childList: true, subtree: true, attributeFilter: ['target', 'onclick'] }); // 全局点击拦截(2025升级版) document.addEventListener('click', function(e) { const target = e.composedPath()[0]; if (target.matches('a')) { cleanAttributes(target); } }, true); // 键盘事件拦截(2025新增功能) document.addEventListener('keydown', function(e) { if (e.key === 'Enter' && !e.ctrlKey && !e.metaKey) { const activeElement = document.activeElement; if (activeElement.matches('input[type="text"], input[type="search"], textarea')) { // 实时处理form属性 const form = activeElement.closest('form'); if (form) { form.target = '_self'; form.removeAttribute('target'); } // 防御性处理异步属性添加 requestAnimationFrame(() => { if (form && form.getAttribute('target') === '_blank') { form.removeAttribute('target'); } }); } } }, true); // 表单提交拦截(新增防御层) document.addEventListener('submit', function(e) { e.target.target = '_self'; }, true); })();