Search code examples
javascriptdomoperauserscripts

What Can I Use Instead of DOMSubtreeModified?


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.


Solution

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