// ==UserScript== // @name 修改跳转方式-强制打开新标签页 // @namespace open_new // @version 2.1 // @description 拦截页面跳转,强制在新标签页打开所有链接 | 更多实用脚本 & 加入交流群👉关注公众号:【极客脚本库】 // @author 大角牛软件/u2222223(极客脚本库) // @match *://*/* // @grant GM_registerMenuCommand // @grant GM_addStyle // @grant GM_setValue // @grant GM_getValue // @run-at document-start // ==/UserScript== (function () { 'use strict'; // ------------------------------------------------------------------------- // 🎨 UI & 交互逻辑:菜单与弹窗 // ------------------------------------------------------------------------- function registerMenu() { if (typeof GM_registerMenuCommand !== 'undefined') { GM_registerMenuCommand("💬 反馈BUG & 加入油猴爱好者群", showContactModal); } } function checkFirstRun() { if (!GM_getValue('has_shown_modal', false)) { // 延迟执行,确保页面加载完成 window.addEventListener('load', () => { setTimeout(() => showContactModal(true), 2000); // 延迟2秒显示,避免干扰 }); GM_setValue('has_shown_modal', true); } } function showContactModal(isFirstRun = false) { // 移除已存在的模态框 const existing = document.getElementById('geek-script-modal'); if (existing) existing.remove(); // 创建容器 const modal = document.createElement('div'); modal.id = 'geek-script-modal'; // 内联样式(确保独立性) const css = ` #geek-script-modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); z-index: 2147483647; display: flex; justify-content: center; align-items: center; opacity: 0; transition: opacity 0.3s ease; backdrop-filter: blur(2px); } #geek-script-modal.show { opacity: 1; } .geek-modal-content { background: #ffffff; padding: 24px; border-radius: 16px; box-shadow: 0 10px 40px rgba(0,0,0,0.25); width: 380px; max-width: 90vw; text-align: center; position: relative; transform: scale(0.9); transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; border: 1px solid rgba(0,0,0,0.05); } #geek-script-modal.show .geek-modal-content { transform: scale(1); } .geek-modal-close { position: absolute; top: 10px; right: 14px; width: 24px; height: 24px; line-height: 24px; cursor: pointer; color: #aaa; font-size: 24px; font-weight: 300; transition: color 0.2s; } .geek-modal-close:hover { color: #333; } .geek-modal-title { font-size: 18px; font-weight: 700; color: #2c3e50; margin: 5px 0 15px; } .geek-modal-subtitle { font-size: 14px; color: #27ae60; margin-bottom: 15px; font-weight: 500; } .geek-modal-img { width: 220px; height: 220px; border-radius: 12px; box-shadow: 0 4px 12px rgba(0,0,0,0.08); margin-bottom: 15px; display: block; margin-left: auto; margin-right: auto; } .geek-modal-tip-box { background: #f8f9fa; border-radius: 8px; padding: 10px; margin-bottom: 20px; border: 1px dashed #dee2e6; } .geek-modal-tip-text { font-size: 13px; color: #555; line-height: 1.5; } .geek-modal-highlight { color: #e67e22; font-weight: bold; background: rgba(230, 126, 34, 0.1); padding: 0 4px; border-radius: 4px; } .geek-modal-btn { width: 100%; padding: 10px 0; background: linear-gradient(135deg, #007bff, #0056b3); color: white; border: none; border-radius: 8px; font-size: 15px; font-weight: 500; cursor: pointer; transition: transform 0.1s, box-shadow 0.2s; box-shadow: 0 2px 5px rgba(0, 123, 255, 0.3); margin-top: 10px; } .geek-modal-btn:hover { box-shadow: 0 4px 8px rgba(0, 123, 255, 0.4); } .geek-modal-btn:active { transform: translateY(1px); } .geek-modal-btn-secondary { width: 100%; padding: 10px 0; background: transparent; color: #95a5a6; border: none; font-size: 13px; cursor: pointer; margin-top: 5px; transition: color 0.2s; } .geek-modal-btn-secondary:hover { color: #7f8c8d; text-decoration: underline; } .geek-modal-promotion { font-size: 14px; color: #e67e22; font-weight: bold; margin: 15px 0 10px; padding: 10px; background: #fff8e1; border-radius: 8px; border: 1px solid #ffe0b2; } .geek-modal-notice-highlight { font-size: 13px; color: #2c3e50; font-weight: 600; margin-top: 10px; padding: 8px; background: #f1f2f6; border-radius: 6px; border-left: 4px solid #3498db; text-align: left; } `; const style = document.createElement('style'); style.textContent = css; modal.appendChild(style); // 构建HTML内容 let titleHtml = '
加入油猴爱好者群 / 反馈BUG
'; let subTitleHtml = ''; let btnClass = 'geek-modal-btn'; let tipBoxHtml = ''; if (isFirstRun) { titleHtml = '
🚀 脚本运行成功
'; subTitleHtml = '
本弹窗仅首次显示,后续不再打扰
'; btnClass = 'geek-modal-btn-secondary'; // 首次运行:整合后的提示信息(黄色强调背景) tipBoxHtml = `
🔥 关注公众号【极客脚本库】
回复 交流群 加入油猴爱好者群 & 获取更多脚本
💡 遇到问题?请点击浏览器的 油猴/脚本猫 图标,在菜单中选择“反馈BUG & 加入油猴爱好者群”
`; } else { // 菜单触发:标准提示 tipBoxHtml = `
关注公众号【极客脚本库】
回复 交流群 加入油猴爱好者群,在群内反馈BUG
`; } const content = document.createElement('div'); content.className = 'geek-modal-content'; content.innerHTML = `
×
${titleHtml} ${subTitleHtml}
极客脚本库
${tipBoxHtml} `; modal.appendChild(content); // 图片加载失败处理 const img = content.querySelector('.geek-modal-img'); const errDiv = content.querySelector('.geek-modal-img-error'); img.onerror = () => { img.style.display = 'none'; errDiv.style.display = 'block'; }; document.body.appendChild(modal); // 绑定事件 const close = () => { modal.classList.remove('show'); setTimeout(() => modal.remove(), 300); }; modal.querySelector('.geek-modal-close').onclick = close; // 兼容两种按钮样式 const confirmBtn = modal.querySelector('.geek-modal-btn') || modal.querySelector('.geek-modal-btn-secondary'); if (confirmBtn) confirmBtn.onclick = close; modal.onclick = (e) => { if (e.target === modal) close(); }; // 动画入场 requestAnimationFrame(() => modal.classList.add('show')); } // 立即注册菜单 registerMenu(); // 检查首次运行 checkFirstRun(); // ------------------------------------------------------------------------- // 🚀 核心逻辑:链接跳转控制 // ------------------------------------------------------------------------- // 核心函数:处理链接跳转,强制新标签打开 function openInNewTab(event) { // 获取目标链接 let targetUrl = ''; const targetElement = event.target.closest('a'); // 找到最接近的a标签 // 处理a标签链接 if (targetElement && targetElement.href) { targetUrl = targetElement.href; } // 过滤无效链接: // 1. 空链接 // 2. javascript: 伪协议(通常用于脚本触发) // 3. 锚点链接(#开头) // 4. 当前页面链接(避免刷新) if (!targetUrl || targetUrl.trim() === '' || targetUrl.toLowerCase().startsWith('javascript:') || targetUrl.includes('#') || // 包含锚点的通常也是页内跳转或JS逻辑 targetUrl === window.location.href) { return; // 直接返回,不阻止默认行为,让网页原有逻辑生效 } // 只有确定是有效跳转链接时,才阻止默认行为并新开标签 event.preventDefault(); event.stopPropagation(); // 新标签页打开链接 window.open(targetUrl, '_blank'); } // 监听页面所有a标签的点击事件 document.addEventListener('click', function (event) { const target = event.target.closest('a'); if (target && !event.ctrlKey && !event.metaKey && !event.shiftKey) { // 仅在未按住Ctrl/Command/Shift键时生效(避免重复新开标签) openInNewTab(event); } }, true); // 使用捕获阶段,确保优先拦截 // 可选:处理表单提交(如需强制表单提交也新开标签,可启用) // document.addEventListener('submit', function(event) { // const form = event.target; // if (form && form.action) { // event.preventDefault(); // window.open(form.action + '?' + new URLSearchParams(new FormData(form)), '_blank'); // } // }, true); // 控制台输出引流信息(不打扰,懂技术的用户能看到) console.log('✅ 强制新标签页脚本已运行'); console.log('📌 更多黑科技脚本 & 加入交流群 → 公众号:极客脚本库(回复“交流群”)'); })();