0

I'd like to write a script that injects in the compose-page of gmail.

So far I've got this url-pattern: ["*://mail.google.com/*"]. The script successfully gets injected when loading gmail, but after that it doesn't get re-injected when clicking links like inbox or compose. I was hoping ["*://mail.google.com/*compose"] would do it easily, but no.

Is this pattern perhaps as far as I can get, and I'll then have to create a listener that checks when that part of the page is reloaded?

What is the most straightforward way to determine when the compose-page is loaded?

Rob W
  • 341,306
  • 83
  • 791
  • 678
Clox
  • 1,923
  • 5
  • 28
  • 43

1 Answers1

2

Content script only run when a page is truly loaded.
If you want to run code for a specific hash, inject the script at the URL without the hash, and use the hashchange to detect changes.

function checkHash() {
    if (/^#(compose|drafts)\b/.test(location.hash)) {
        // Do whatever you want
    }
}
window.addEventListener('hashchange', checkHash);
checkHash();

Instead of monitoring the location hash, it might be more effective to use setInterval to continuously check whether the desired element exists in the DOM. This is particularly useful when you want to read or write the value of a specific DOM node.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • Thanks! This answers my question and it works like a charm. Though I don't get why setInterval would be more suited for this? Isn't that exactly the kind of sloppy programming that generally always should be avoided? Anyway, after getting past this problem I ran into a new one. I had a working script which I ran through a bookmarklet, and for making it into an extensionI thought it would just be a matter of writing the code that would run this script, but apparently not. New question: http://stackoverflow.com/questions/13669671/accessing-editing-the-content-of-an-iframe-from-a-chrome-extension – Clox Dec 02 '12 at 13:31
  • @Clox You need to find out when the node exists. You could either use [DOM mutation observers](https://developer.mozilla.org/en-US/docs/DOM/MutationObserver) (or worse: DOM mutation events), but support is not optimal yet (though Chrome supports it, and you're writing a Chrome extension). `setInterval` is simple and universally supported. When implemented correctly, there's not a problem with it. – Rob W Dec 02 '12 at 13:46