问题描述
我正在尝试在Facebook页面的顶部写一个覆盖的JavaScript脚本。它使用DOMContentLoaded检测何时加载内容,然后添加一些额外的东西。但是,因为Facebook在实际上重新加载页面,当到一个新的页面(它只是使用AJAX),DOMContentLoaded处理程序不运行再次,即使有新的东西来看看。
I am trying to write a JavaScript script that is "overlayed" on top of a Facebook page. It uses DOMContentLoaded to detect when content is loaded, then adds some extra stuff in. However, because Facebook doesn't actually "reload" the page when going to a new page (it just uses AJAX), the DOMContentLoaded handler doesn't run again, even though there is new stuff to look through.
无论如何,为了检测变化,我想到了使用onhashchange,因为Facebook用来改变页面的哈希,但是Firefox 4(我需要支持Firefox 3和更高版本,但没有其他浏览器),Facebook不再更改哈希,在Firefox 3.6之前没有onhashchange。
Anyway, to detect the change, I thought about using onhashchange since Facebook used to change the page's hash, but in Firefox 4 (I need to support Firefox 3 and later with this, but no other browsers), Facebook doesn't change the hash anymore and in pre-Firefox 3.6 there is no onhashchange.
我以为使用DOMNodeInserted,但这真的会减慢页面? (我真的不能在这个脚本中有任何减速。)
I thought about using DOMNodeInserted, but would that really slow down the page? (I really can't have any slowdowns in this script.)
推荐答案
对于轻量级页面,它通常没有明显的效果。然而,在庞大的页面(我在gmail上尝试过),这使得真的很慢,我甚至不能顺利地撰写消息。那个事件被添加到一个非常简单的跨度元素,只有一个链接。像DOMNodeInserted和DOMSubTreeModified这样的事件是真正的显示屏。
For lightweight pages it generally doesn't have noticable effect. However, on bulky pages (I tried this on gmail) it makes that really really slow that I cannot even compose a message smoothly. And that event was added to a very simple span element which just had a single link in that. The events like DOMNodeInserted and DOMSubTreeModified are real show stoppers.
更新:对于所有尝试找到答案的人,请注意,这些方法DOMNodeInserted(或DOMSubtreeModified)确实具有性能问题,因此根据新的ECMA规范一个更快的监听器:MutationObserver做同样的事情(和更多)。
UPDATE: For all those trying to find an answer to this, note that these methods DOMNodeInserted (or DOMSubtreeModified) really had performance problems, so according to new ECMA specs it is a much faster listener : MutationObserver for doing the same thing (and more).
这篇关于DOMNodeInserted或hashchange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!