如何通过 browserAction 创建新选项卡,然后执行脚本
我想通过单击浏览器操作按钮来创建一个选项卡,然后插入内容脚本或执行脚本。到目前为止,它的运作并不顺利。
Background.html
chrome.browserAction.onClicked.addListener(function(tab)
{
chrome.tabs.create({url: "Dreamer.html"}, function(tab) //Dreamer.html is a file in my extension
{
//Add a script
chrome.tabs.executeScript(tab.id, {file:'Dreamer.js'});
});
});
Manifest.json
{
"name" : " Dreamer",
"version" : "0.1",
"description" : "My extensionr",
"browser_action" : {"default_icon" : "App/AppData/Images/icon.png", "default_title":"Start Dreamer" },
"background_page" : "App/AppData/background.html",
"content_scripts" :[{"matches":["http://*/*"],"js":["app/view/UIManager.js"]}],
"permissions": [ "cookies", "tabs", "http://*/*", "https://*/*" ]
}
我在后台页面中收到此错误
Error during tabs.executeScript: Cannot access contents of url "chrome-extension://femiindgnlfpdpajimkmldpgpccngfmd/Dreamer.html". Extension manifest must request permission to access this host.
我真的很想知道如何创建选项卡(新选项卡)并立即运行脚本
编辑:
我正在创建的应用程序类型需要以下操作:
- 允许用户通过单击浏览器操作按钮创建新选项卡
- 创建新选项卡时,我的扩展中的文件 (Dreamer.html) 将打开
- 添加内容脚本或在新选项卡中执行脚本
谢谢
I want to create a tab by clicking on the browser action button and then insert a content script or execute a script. So far, its not working well.
Background.html
chrome.browserAction.onClicked.addListener(function(tab)
{
chrome.tabs.create({url: "Dreamer.html"}, function(tab) //Dreamer.html is a file in my extension
{
//Add a script
chrome.tabs.executeScript(tab.id, {file:'Dreamer.js'});
});
});
Manifest.json
{
"name" : " Dreamer",
"version" : "0.1",
"description" : "My extensionr",
"browser_action" : {"default_icon" : "App/AppData/Images/icon.png", "default_title":"Start Dreamer" },
"background_page" : "App/AppData/background.html",
"content_scripts" :[{"matches":["http://*/*"],"js":["app/view/UIManager.js"]}],
"permissions": [ "cookies", "tabs", "http://*/*", "https://*/*" ]
}
i get this error in the background page
Error during tabs.executeScript: Cannot access contents of url "chrome-extension://femiindgnlfpdpajimkmldpgpccngfmd/Dreamer.html". Extension manifest must request permission to access this host.
I would really like to know how to create a tab(new tab) and run a script immediately
EDIT:
The kind of application i am creating requires the following actions:
-Allow user to create new tab by clicking the browserAction button
-On creation of the new tab, a file in my extension (Dreamer.html) is opened
-Add a content script or execute a script in the new tab
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要注入脚本有什么特殊原因吗?由于 Dreamer.html 和 Dreamer.js 似乎都是硬编码的,因此您只需在其中包含
前者,对吗?
附带的好处是,如果您需要它向后台页面发送信息,您可以使用 chrome.extension.getBackgroundPage() 直接访问它,而不是设置通常随内容脚本一起提供的复杂侦听器,也。
注入内容脚本用于在扩展沙箱之外注入脚本。然而,Dreamer.html 是扩展的一部分。
编辑
如果您确实想要一个有关如何在扩展程序页面中执行脚本的(笨拙的)示例,请参阅此处:
http://code.google.com/p/chromium/issues/detail?id=30756#c11
我认为它不适用不过,根据你的情况。
Is there any particular reason you need to inject the script? Since both Dreamer.html and Dreamer.js seem to be hardcoded, you could just include
<script type="text/javascript" src="Dreamer.js"></script>
in the former, right?As a side benefit, if you need it to send info to the background page, you can access it directly with
chrome.extension.getBackgroundPage()
instead of setting up complex listeners that usually come with content scripts, too.Injecting content scripts is for injecting scripts outside the extension sandbox. Dreamer.html, however, is a part of the extension.
Edit
If you do want an (unwieldy) example of how to execute a script in an extension page, see here:
http://code.google.com/p/chromium/issues/detail?id=30756#c11
I don't think it applies to your case, however.