我可以使用什么来代替 DOMSubtreeModified?

发布于 2024-12-15 21:00:09 字数 241 浏览 2 评论 0原文

我正在为 Opera 编写用户脚本,而 Opera 不支持 DOMSubtree修改, 修改子树有其他方法吗?

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

谢谢。

I'm working on a user script for opera, and opera doesn't support DOMSubtreeModified,
Is there any alternative method for subtree modified?

NOTE: I'm looking a solution only in javascript. (not jquery or any javascript library)

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从此见与不见 2024-12-22 21:00:09

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

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

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

Target the nodes you really care about, then use an interval timer to check for changes.

The key is choosing the nodes of interest, for example given:

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

Maybe you want the "author" nodes.
You would scan for these using querySelectorAll("#content p.author") inside a timer.

Something like this pseudo code:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文