1

I'm building a Chrome extension that hides notifications around your browser. I want document title '(1) Facebook' to go to simply 'Facebook', always. In my Chrome extension content script, I have:

if (document.title.includes("Facebook")) {
    document.title = "Facebook";
}

This doesn't work because document.title is being dynamically set by the page:

  • when document title first loads (which is also when my content script runs), it shows 'Facebook'
  • Facebook page continues to load and document title is set to '(1) Facebook'
  • at point that new notification is received (e.g. you're on the page and receive a message) document title is again set - to '(X) Facebook' where X is the new total number of notifications

How can I use my content script to override dynamic updating of the document title in the most efficient way, and set my document title to just 'Facebook' without it ever changing? I could do a setInterval with the above function but it seems a bit heavy. I'm hoping to figure out a method that works for other sites as well (Gmail, Twitter, etc.).

Xan
  • 74,770
  • 16
  • 179
  • 206
lou1989
  • 337
  • 2
  • 8
  • [This answer of mine](http://stackoverflow.com/a/22989490/934239) isn't a duplicate but contains most of what's needed to make it work. – Xan Oct 05 '16 at 17:02
  • Actually now that I re-read the question, it IS a duplicate. If you run into specific implementation questions, feel free to make a new question. – Xan Oct 05 '16 at 17:11
  • You can freeze the title in a [DOM-injected script](https://stackoverflow.com/q/9515704) via `Object.defineProperty(document, 'title', {value: document.title})`. The only problem left is to detect user-invoked (intended) page navigation which is shown in the duplicate post. – wOxxOm Oct 05 '16 at 17:29
  • Thanks a lot @Xan, greatly appreciate it! Apologies for not finding your question when I searched for answers before writing this one, I had a good look around but missed it. Look forward to implementing. And wOxxOm, thanks too, I will try that as well. – lou1989 Oct 05 '16 at 23:23

0 Answers0