4

I'm working on a chrome extension, and I set window.title in the onload handler. It seems, though, that the page I'm modifying sets the document title dynamically as well. There's a huge collection of scripts being linked. Is there any way for me to prevent anyone else from modifying document.title or any of its variants, without knowing where the modification is coming from? Alternatively, is there a quick way for me to see where the change is coming from?

Moshe
  • 57,511
  • 78
  • 272
  • 425

3 Answers3

5

I had same problem, some external scripts are changed my page title by document.title = "..."

I've made own solution for it:

try {
    window.originalTitle = document.title; // save for future
    Object.defineProperty(document, 'title', {
        get: function() {return originalTitle},
        set: function() {}
    });
} catch (e) {}
mixalbl4
  • 3,507
  • 1
  • 30
  • 44
1

See the answer to how to listen for changes to the title element?. Notably:

function titleModified() {
    window.alert("Title modifed");
}

window.onload = function() {
    var titleEl = document.getElementsByTagName("title")[0];
    var docEl = document.documentElement;

    if (docEl && docEl.addEventListener) {
        docEl.addEventListener("DOMSubtreeModified", function(evt) {
            var t = evt.target;
            if (t === titleEl || (t.parentNode && t.parentNode === titleEl)) {
                titleModified();
            }
        }, false);
    } else {
        document.onpropertychange = function() {
            if (window.event.propertyName == "title") {
                titleModified();
            }
        };
    }
};
Community
  • 1
  • 1
ryan
  • 6,541
  • 5
  • 43
  • 68
0

This SO answer suggest a technique for how to listen for changes to the document title.

Perhaps you could use that technique to create a callback which changes the title back to whatever you want it to be, as soon as some other script tries to change it.

Community
  • 1
  • 1
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103