Này người xa lạ!

Còn chờ gì nữa mà không mau đăng nhập hoặc đăng ký để cùng tham gia thảo luận với cộng đồng!

百度下面有这么一段代码,如何破?会导致MutationObserver(chrome)出问题

đã sửa March 2017 trong Phát triển Chrome

try {
window.MutationObserver = window.WebKitMutationObserver = window.MozMutationObserver = null
} catch (e) {}

https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_66bebcd.js

Bình luận

  • function restoreGlobalAPI(name) {
        if (window[name])
            return;
        var iframe = document.createElement('iframe');
        iframe.width = iframe.height = 0;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
        window[name] = iframe.contentWindow[name];
        iframe.remove();
    }
    
    restoreGlobalAPI('MutationObserver');
    
  • đã sửa March 2017 Chrome

    thanks a lot

  • đã sửa March 2017 Chrome

    I found this code can't use in
    https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=grease

    function restoreGlobalAPI(name) { //处理百度封MutationObserver的问题
        if (window[name])
            return;
        var iframe = document.createElement('iframe');
        iframe.width = iframe.height = 0;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
         window[name] = iframe.contentWindow[name];
        iframe.remove();
    }
    restoreGlobalAPI('MutationObserver');
    var watch = document.querySelector("title");
    var observer = new MutationObserver(function(mutations) {
        console.log("标题发生了变化", document.title);
    
    });
    observer.observe(watch, {
        childList: true,
        subtree: true,
        characterData: true
    });
    
  • This page doesn't kill native MutatioObserver when I open it so that code is not needed.

  • đã sửa March 2017 Chrome

    This page doesn't kill native MutatioObserver when I open it so that code is not needed.

    maybe the network issue,your place hadn't load that script

    I use this code in other page's console, window.MutationObserver = null;
    and use restoreGlobalAPI,but when I edit the title,the code has no effect.

    window.MutationObserver  = null;
    function restoreGlobalAPI(name) { //处理百度封MutationObserver的问题
        if (window[name])
            return;
        var iframe = document.createElement('iframe');
        iframe.width = iframe.height = 0;
        iframe.style.display = 'none';
        document.body.appendChild(iframe);
         window[name] = iframe.contentWindow[name];
        iframe.remove();
    }
    restoreGlobalAPI('MutationObserver');
    var watch = document.querySelector("title");
    var observer = new MutationObserver(function(mutations) {
        console.log("标题发生了变化", document.title);
    });
    observer.observe(watch, {
        childList: true,
        subtree: true,
        characterData: true
    });
    
  • đã sửa March 2017 Chrome

    only with

    var watch = document.querySelector("title");
    var observer = new MutationObserver(function(mutations) {
        console.log("标题发生了变化", document.title);
    });
    observer.observe(watch, {
        childList: true,
        subtree: true,
        characterData: true
    });
    

    .

  • Well, apparently the restored MutationObserver belongs to a different execution context so it can't see anything in the document.

    Do you use // @grant none in your script? If you do, remove it: Tampermonkey/Greasemonkey preserve the context in a sandbox, including the native MutationObserver.

  • Well, apparently the restored MutationObserver belongs to a different execution context so it can't see anything in the document.

    Do you use // @grant none in your script? If you do, remove it: Tampermonkey/Greasemonkey preserve the context in a sandbox, including the native MutationObserver.

    I use

    // @grant       GM_getValue
    // @grant       GM_setValue
    // @grant       GM_addStyle
    

    Well .....
    I think In Tampermonkey ,it preserve the context in a sandbox after the script run
    ,so I need use // @run-at document-start
    and the code is work

  • Indeed, document-start runs before the site scripts.

  • This solution may be better:

    Object.defineProperty(window, 'MutationObserver', {
      value: window.MutationObserver,
      writable: false,
      configurable: false,
      enumerable: false
    });
    
Đăng nhập hoặc Đăng ký để gửi bình luận.