[英]Mutation Observer not firing
Hi I'm building a chrome extension. 嗨,我正在建立一个chrome扩展程序。 I have a javascript file in which I want to detect changes to a div on facebook.com so I attach it to a mutation observer.
我有一个JavaScript文件,我想在facebook.com上检测对div的更改,因此将其附加到突变观察器。 The thing is now if I navigate to another page on Facebook say by clicking the home button the mutation observer stops working even though a div by the same id is present in this new page.
现在的问题是,如果我导航到Facebook上的另一个页面,则说单击主页按钮,即使该新页面中存在具有相同ID的div,变异观察者也将停止工作。 It only starts working once I refresh the page.
只有刷新页面后它才能开始工作。 Any ideas on how I can fix this?
关于如何解决此问题的任何想法?
Also all my code is wrapped in a document.ready jquery function so I would assume that would automatically fire when I navigate to a new facebook page by clicking the home button but that doesn't happen. 另外,我所有的代码都包装在document.ready jquery函数中,因此我假设当我单击主页按钮导航到新的Facebook页面时,该代码会自动触发,但不会发生。 It only fires when I either explicitly type the url or refresh the page after navigating.
仅当我在导航后显式键入URL或刷新页面时才会触发。
The code is like this: 代码是这样的:
$ (document).ready( function() {
var observeDOM = (function(){
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver,
eventListenerSupported = window.addEventListener;
return function(obj, callback){
if( MutationObserver ){
// define a new observer
var obs = new MutationObserver(function(mutations, observer){
if( mutations[0].addedNodes.length || mutations[0].removedNodes.length )
callback();
});
// have the observer observe foo for changes in children
obs.observe( obj, { childList:true, subtree:true });
}
else if( eventListenerSupported ){
obj.addEventListener('DOMNodeInserted', callback, false);
obj.addEventListener('DOMNodeRemoved', callback, false);
}
}
})();
if (document.getElementById('contentArea') !== null) {
console.log("working");
observeDOM( document.getElementById('contentArea') ,function(){
console.log("DOM CHANGED");
});
}
});
UPDATE: Seems like the problem is with how facebook loads pages (see: How facebook load pages? ) 更新:似乎问题在于facebook如何加载页面(请参阅: facebook如何加载页面? )
Any way to detect these individual changes happening via AJAX? 有什么方法可以检测通过AJAX发生的这些单独的变化?
The thing is now if I navigate to another page on Facebook say by clicking the home button the mutation observer stops working even though a div by the same id is present in this new page.
现在的问题是,如果我导航到Facebook上的另一个页面,则说单击主页按钮,即使该新页面中存在具有相同ID的div,变异观察者也将停止工作。
Right, because even if it has the same id
, it's a different element . 是的,因为即使
id
相同,它也是一个不同的元素 。
If you want to watch that new element for mutation events, you have to set up a new mutation observer that observes the new element, probably by responding to Chrome's "page loaded" event. 如果您想监视该新元素的突变事件,则必须设置一个新的突变观察器来观察该新元素,这可能是通过响应Chrome的“页面加载”事件来进行的。 If your script endures between page loads, you'll also want to be sure to release the previous observer.
如果您的脚本在两次页面加载之间可以忍受,那么您还需要确保释放先前的观察者。
so I would assume that would automatically fire when I navigate to a new facebook page by clicking the home button but that doesn't happen
所以我认为当我通过单击主屏幕按钮导航到新的Facebook页面时会自动触发,但不会发生
I wouldn't assume that, I would assume it would run on the page it's injected on, and not any other page. 我不会假设它会在它注入的页面上运行,而不是在其他任何页面上运行。 This question and its answers address ensuring an extension is run when a page is done loading.
这个问题及其答案可确保在页面加载完成后运行扩展。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.