Обсуждения » Разработка

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

§
Создано: 14.03.2017
Отредактировано: 14.03.2017

百度下面有这么一段代码,如何破?会导致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/all_async_search_66bebcd.js

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');
§
Создано: 14.03.2017
Отредактировано: 24.03.2017

thanks a lot

§
Создано: 23.03.2017
Отредактировано: 24.03.2017

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

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.

§
Создано: 24.03.2017
Отредактировано: 24.03.2017

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
});
§
Создано: 24.03.2017
Отредактировано: 24.03.2017

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.

Deleted user 20822
§
Создано: 25.03.2017

This solution may be better:

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

Ответить

Войдите, чтобы ответить.