admin管理员组文章数量:1356582
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 badges1 Answer
Reset to default 9You 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);
本文标签:
版权声明:本文标题:javascript - Safari Extension: How do you inject content scripts only after a toolbar button is clicked? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743988882a2571769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论