最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Safari Extension: How do you inject content scripts only after a toolbar button is clicked? - Stack Overflow

programmeradmin4浏览0评论

In my extension, I need to perform actions when the toolbar icon is clicked. However, there are a lot of scripts and stylesheets involved, so I want to avoid using plain "content scripts", because those are always going to be loaded into every page a user visits, slowing down their browser. Instead, I want to inject the scripts only after the button is clicked.

The docs only say the following:

If you need the mand [i.e., a toolbar button click] to initiate an action in an injected script, respond to the mand in the global HTML page or an extension bar and send a message to the script.

In Chrome, you can use chrome.tabs.executeScript()

In Firefox, you can use tabs.activeTab.attach()

How do you do it in Safari?

In my extension, I need to perform actions when the toolbar icon is clicked. However, there are a lot of scripts and stylesheets involved, so I want to avoid using plain "content scripts", because those are always going to be loaded into every page a user visits, slowing down their browser. Instead, I want to inject the scripts only after the button is clicked.

The docs only say the following:

If you need the mand [i.e., a toolbar button click] to initiate an action in an injected script, respond to the mand in the global HTML page or an extension bar and send a message to the script.

In Chrome, you can use chrome.tabs.executeScript()

In Firefox, you can use tabs.activeTab.attach()

How do you do it in Safari?

Share Improve this question asked Jun 13, 2013 at 22:46 sffcsffc 6,4343 gold badges49 silver badges76 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

You can use the following four methods to conditionally inject content into pages:

safari.extension.addContentScript
safari.extension.addContentScriptFromURL
safari.extension.addContentStyleSheet
safari.extension.addContentStyleSheetFromURL

Documentation here.

However, note that these methods will not inject content into any already existing tabs at the time you call them. Therefore, they may not be suitable for your use case. You could use one of these methods and then reload the tab, but you may not want that because of the jarring user experience.

Instead, you may need to use a technique like this: Have a very lightweight content script that gets injected into every page, whose only job is to listen for messages from the extension's global page and, when instructed, to create the appropriate <script> and <style> or <link> elements for the content you want to inject. Then, when your toolbar button is clicked, have the global page pass the appropriate message to the content script, perhaps including the URLs of the desired content.

A drawback of this approach is that the injected scripts (the ones you add by creating <script> tags) will not be able to municate directly with the global page or with other parts of the extension. If you need them to municate, they will need to do so using window.postMessage or some other technique.

Here's a minimal example. Assume your extension has a toolbar item that issues the mand "inject-content".

In the global page:

safari.application.addEventListener('mand', function (evt) {
    safari.application.activeBrowserWindow.activeTab.page.dispatchMessage(evt.mand);
}, false);

In the always-injected content script:

safari.self.addEventListener('message', function (evt) {
    if (evt.name == 'inject-content') {
        var myScriptTag = document.createElement('script');
        myScriptTag.src = safari.extension.baseURI + 'my_script.js';
        document.body.appendChild(myScriptTag);
    }
}, false);
发布评论

评论列表(0)

  1. 暂无评论