Search code examples
javascriptgoogle-chromesafarigoogle-chrome-extensionsafari-extension

Inject dynamic script to each tab onLoad in Safari Extension


In Chrome there is a simple way to inject a dynamically loaded script into each tab onLoad, through the chrome.tabs.onUpdated.addListener method, just like this:

$.get('somedomain.com/script.js', function(code) {
        chrome.tabs.onUpdated.addListener(function(tab_id, info, tab) {
            if (info['status'] == 'complete') {
                chrome.tabs.executeScript(tab_id, { file: 'jquery.js' });
                chrome.tabs.executeScript(tab_id, { code: code });
            }
        });
    });

Now, i tried porting same thing to Safari Extension, but i only found that i can supply the injected script in the extension builder, and the API description it too poor

Is there a way to do same in Safari?

ps: the script is loaded once in a background (so called 'global' in Safari) page


Solution

  • See addContentScript() and addContentScriptFromURL().