知乎免登录

去除知乎烦人的登录提示,安卓端免app浏览

< Feedback on 知乎免登录

Review: OK - script works, but has bugs

§
Posted: 2021-06-21

0.8.3版本会出现如下错误

'observe' on 'MutationObserver': parameter 1 is not of type 'Node'

我稍微修改了一下代码,shamelessly copy from StackOverflow

// ==UserScript==
// @name         知乎免登录
// @namespace    https://www.zhihu.com/
// @version      0.8.3
// @description  去除知乎烦人的登录提示,安卓端免app浏览
// @author       System
// @match        *://*.zhihu.com/*
// @run-at         document-start
// @connect        api.zhihu.com
// @grant          GM_xmlhttpRequest
// ==/UserScript==
(function() {
    "use strict";
    const isMobile = /(Android|iPhone)/i.test(navigator.userAgent);

    function addObserverIfDesiredNodeAvailable() {
        var body = document.body;
        if (!body) {
            //The node we need does not exist yet.
            //Wait 500ms and try again
            window.setTimeout(addObserverIfDesiredNodeAvailable, 500);
            return;
        }
        var config = {
            childList: true
        };
        composeObserver.observe(body, config);
    }
    //登录弹窗屏蔽功能
    {
        let flag = false,
            timer = null;
        var composeObserver = new MutationObserver((events, observer) => events.forEach((e) => e.addedNodes.forEach((target) => {
            if (!flag &&
                target.getElementsByClassName &&
                target.getElementsByClassName("signFlowModal").length !== 0
            ) {
                target.style.display = 'none';
                setTimeout(
                    () => {
                        document.querySelector('html').style.overflow = 'auto';
                        target.getElementsByClassName("Modal-backdrop")[0].click();
                        target.remove();
                    },
                    0
                );
            }
            flag = false;
        })));
        addObserverIfDesiredNodeAvailable();
        document.addEventListener('click', () => {
            timer && clearTimeout(timer);
            [flag, timer] = [true, null];
            timer = setTimeout(() => flag = false, 1000);
        })
    } //END

  //....后面省略

SystemAuthor
§
Posted: 2021-07-06

嗯,我看了,是加载的时候body节点为null,不知道是知乎为了防脚本做的还是浏览器的新机制

Post reply

Sign in to post a reply.