// ==UserScript== // @name 知乎增强:自动关闭登录弹窗与外链跳转 // @namespace http://tampermonkey.net/ // @version 1.2 // @description 自动关闭知乎登录弹窗,处理外链安全提示,并恢复页面滚动。 // @author Assistant // @match https://*.zhihu.com/* // @match http://*.zhihu.com/* // @icon https://static.zhihu.com/heifetz/favicon.ico // @grant none // @run-at document-end // @license MIT // ==/UserScript== (function() { 'use strict'; // 配置区域:可根据需要调整 const config = { // 重试次数 maxRetries: 10, // 重试间隔(毫秒) retryInterval: 500, // 启用实验性功能(如点击背景关闭评论弹窗) experimentalFunction: false }; let retryCount = 0; let checkInterval; /** * 主关闭函数 */ function mainCloser() { closeLoginModal(); restoreScroll(); autoClickContinueVisit(); // 如果达到最大重试次数,停止检查 if (retryCount >= config.maxRetries) { clearInterval(checkInterval); console.log('知乎增强脚本:已达到最大重试次数,停止检查。'); return; } retryCount++; } /** * 关闭登录弹窗 */ function closeLoginModal() { // 多种选择器策略,提高兼容性[1,2,3](@ref) const selectors = [ '.Modal-closeButton', // 主关闭按钮[1](@ref) 'button.Button.Modal-closeButton.Button--plain', // 带更多样式的关闭按钮[2](@ref) 'div.Modal-wrapper', // 直接操作弹窗容器[5](@ref) ]; for (let selector of selectors) { const element = document.querySelector(selector); if (element) { try { if (selector === 'div.Modal-wrapper') { // 直接移除弹窗容器[5](@ref) element.remove(); console.log('知乎增强脚本:通过移除弹窗容器方式关闭登录弹窗。'); } else { // 模拟点击关闭按钮[1,2](@ref) element.click(); console.log('知乎增强脚本:通过点击关闭按钮方式关闭登录弹窗。'); } // 成功找到并处理一个元素后即可返回 return; } catch (error) { console.warn('知乎增强脚本:处理元素时发生错误:', error); } } } } /** * 恢复页面滚动(关闭弹窗后页面可能被锁定) */ function restoreScroll() { if (document.documentElement.style.overflow === 'hidden') { document.documentElement.style.overflow = 'auto'; document.body.style.overflow = 'auto'; } } /** * 自动点击“继续访问”按钮(用于外链安全跳转提示) */ function autoClickContinueVisit() { const continueButton = document.querySelector('a.button'); if (continueButton && continueButton.innerText === "继续访问") { continueButton.click(); console.log('知乎增强脚本:已自动点击“继续访问”按钮。'); } } /** * 实验性功能:点击背景关闭评论等弹窗 */ function initExperimentalFunction() { if (!config.experimentalFunction) return; document.addEventListener('click', function(event) { const backdropSelectors = ['.Modal-backdrop', '.Modal-wrapper']; for (let selector of backdropSelectors) { if (event.target.matches(selector)) { const closeBtn = event.target.querySelector('.Modal-closeButton'); if (closeBtn) closeBtn.click(); } } }); } // 初始执行一次 mainCloser(); initExperimentalFunction(); // 使用间隔定期检查(应对动态加载的弹窗) checkInterval = setInterval(mainCloser, config.retryInterval); // 使用 MutationObserver 监控 DOM 变化(更高效地应对动态内容)[1,2](@ref) const observer = new MutationObserver(function(mutations) { // 检查是否有新增的节点包含弹窗相关元素 let shouldCheck = mutations.some(mutation => { return Array.from(mutation.addedNodes).some(node => { return node.nodeType === 1 && ( node.querySelector?.('.Modal-closeButton') || node.querySelector?.('a.button') ); }); }); if (shouldCheck) { mainCloser(); } }); observer.observe(document.body, { childList: true, subtree: true }); // 页面完全加载后再执行一次,确保处理所有弹窗 window.addEventListener('load', function() { setTimeout(mainCloser, 1000); }); console.log('知乎增强脚本已加载!'); })();