我可以使用什么而不是DOMSubtreeModified?

时间:2011-11-17 12:04:51

标签: javascript dom opera userscripts

我正在为opera的用户脚本工作,而opera不支持DOMSubtreeModified, 是否有任何替代子树修改方法?

注意:我只在javascript中寻找解决方案。 (不是jquery或任何javascript库)

感谢。

1 个答案:

答案 0 :(得分:8)

定位您真正关心的节点,然后使用间隔计时器检查更改。

关键是选择感兴趣的节点,例如:

<div id="content"><p class="author"> ... </p> 
... 
</div>

也许你想要“作者”节点 您可以使用计时器内的querySelectorAll("#content p.author")扫描这些内容。

像这样的代码:

setInterval (MyNodeCheckFunc, 500);

function MyNodeCheckFunc () 
{
    var newNodeList     = [];
    this.oldNodeList    = this.oldNodeList  ||  [];

    var targetNodes     = document.querySelectorAll ('VALID CSS-STYLE SELECTOR');
    if (targetNodes  &&  targetNodes.length > 0) 
    {
        /*--- Found target node(s).  Go through each and act if they
            are new.
        */
        for (var J = targetNodes.length - 1;  J >= 0;  --J) 
        {
            var targNode = targetNodes[J];

            newNodeList.push (targNode);

            if (! targNode.weHaveProcessed) 
            {
                targNode.weHaveProcessed    = true;

                //// DO STUFF FOR NEW NODE.
            }
        }
    }

    /*/// HERE, COMPARE newNodeList TO oldNodeList.
        1)  If an item is in oldNodeList, but not in newNodeList, 
            do any DELETED NODE action.

        2)  If an item is in a different order from oldNodeList 
            to newNodeList, do any MOVED NODE action.
    */
    this.oldNodeList = newNodeList;
}