I’m writing a reasonably complex browser extension for Chrome, Firefox & Safari.
In Chrome & Firefox, I can specify which content script are injected into which sites based on URL matching.
Safari doesn’t appear to offer any such (fine grained) functionality – they only allow one white/blacklist for all sites.
Is there a way to do this? If not, how can I conditionally load a Javascript file? (bearing in mind I can’t add them to the DOM).
I’ve tried yepnope, but it appends to the DOM.
Note: I don’t want to add logic to the actual files, as they are essentially library code that’s duplicated across the extensions.
safari.extension.addContentScriptandsafari.extension.addContentScriptFromURLallow you to programmatically inject scripts. You can use these methods in either your global page, an extension popover, or an extension bar. For both methods, the arguments are:See this page in the Safari Extensions Reference for details.
Note that the whitelist and blacklist both use a URL pattern format that is similar (but not identical) to UNIX wildcards and is not nearly so powerful as regular expresssions. This pattern format is described at the bottom of this page.