本帖最后由 sexcat 于 2024-2-27 16:24 编辑
一个知乎网站自动关闭登录窗的方案
有时候搜索东西搜到知乎了,
点进去没登陆就一个大大的登录框。
我又不想登陆,真烦就写了一个
[JavaScript] 纯文本查看 复制代码
// ==UserScript==
// @name
知乎自动关闭登录窗
// @namespace
// @version
1.1
// @description 尝试精确地关闭知乎登录窗口
// @author
icescat
// @match
*://*.zhihu.com/*
// @grant
none
// @run-at
document-body
// ==/UserScript==
(function() {
'use strict';
// 直接尝试点击关闭按钮
const tryClickCloseButton = () => {
const closeButton = document.querySelector('.Modal-closeButton');
if (closeButton) {
closeButton.click();
}
};
// 使用MutationObserver实时关闭后续出现的登录窗
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.addedNodes.length) {
tryClickCloseButton(); // 如果出现新元素,尝试点击关闭按钮
}
});
});
const config = {
childList: true,
subtree: true
};
// 在文档加载后立即执行关闭操作,并开始监控DOM变化
document.addEventListener('DOMContentLoaded', () => {
tryClickCloseButton();
observer.observe(document.body, config);
});
})();