Discussions » Development

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

§
Posted: 2017-03-14
Edited: 2017-03-14

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

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

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

wOxxOmMod
§
Posted: 2017-03-14
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');
§
Posted: 2017-03-14
Edited: 2017-03-24

thanks a lot

§
Posted: 2017-03-23
Edited: 2017-03-24

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
});
wOxxOmMod
§
Posted: 2017-03-24

https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&tn=baidu&wd=grease

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

§
Posted: 2017-03-24
Edited: 2017-03-24

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
});
§
Posted: 2017-03-24
Edited: 2017-03-24

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
});

.

wOxxOmMod
§
Posted: 2017-03-24

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.

§
Posted: 2017-03-25

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

wOxxOmMod
§
Posted: 2017-03-25

Indeed, document-start runs before the site scripts.

Deleted user 20822
§
Posted: 2017-03-25

This solution may be better:

Object.defineProperty(window, 'MutationObserver', {
  value: window.MutationObserver,
  writable: false,
  configurable: false,
  enumerable: false
});

Post reply

Sign in to post a reply.