Search code examples
javascriptfirefoxeventsfirefox-addondom-events

How to capture change on page title from a Firefox extension


Is there a way to capture when a website title changes from a Firefox extension?


Solution

  • I don't know, if it works from a firefox extension, but as it works in a document, I think it works from a extension too.

    You have to work with Mutation-Events, especially DOMSubtreeModified. This fires on every change on the target.

    A little example-script, put it somewhere after the <title/>

    <script type="text/javascript">
    <!--
    (function()
      {
        var _this={
                    target:document.getElementsByTagName('TITLE')[0],
                    oldValue:document.title
                  };
        _this.onChange=function()
                      {
                        if(_this.oldValue!==document.title)
                        {
                          _this.oldValue=document.title;
                          alert('somebody changed the title');
                        }
                     };
        _this.delay=function()
                    {
                      setTimeout(_this.onChange,1);
                    };
        _this.target.addEventListener('DOMSubtreeModified',_this.delay,false)
      })()
    //-->
    </script>