Chrome扩展程序在新标签上打开新标签 [英] Chrome extension open new tab on new tab
问题描述
我创建了一个Chrome扩展程序,作为其操作的一部分,它会打开一个带有指定网址的新选项卡。
chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if (request.message ===open_new_tab){
chrome.tabs.create({url:request.url});
}
}
);
(完整代码见 GitHub )
这可以在网页的标签上正常工作,但我无法使用它空的选项卡,例如: chrome:// apps /
为了澄清,如果我有一个打开的选项卡,它在stackoverflow.com上,然后当我点击我的扩展按钮它会打开一个加载生成的url的新选项卡。当我在一个新的标签上,或者一个URL以 chrome://
开头的标签时,那么扩展就不起作用了。
我需要包含哪些权限才能允许在任意选项卡中打开扩展程序?包括新标签和任何 chrome://
标签?
Manifest.json:
{
manifest_version:2,
name:MyMiniCity Checker,
短名称:MyMiniCity Checker,
description:检查你的城市最需要什么,并相应地重定向浏览器。,
version:0.2,
author :Richard Parnaby-King,
homepage_url:https://github.com/richard-parnaby-king/MyMiniCity-Checker/,
图标:{
128:icon-big.png
},
options_page:options / options.html,
browser_action:{
default_icon:icon.png
},
permissions:[tabs,storage,http://*.myminicity.com/,
scripts:[background.js],
持久:false
},
content_scripts:[{
matches:[http:// * / *,https:// * / *],
js:[jquery-1。 11.3.min.js]
}]
}
Background.js :
//当用户点击按钮时,运行脚本
chrome.browserAction.onClicked.addListener(function(tab ){
chrome.tabs.executeScript(null,{file:jquery-1.11.3.min.js},function(){
chrome.tabs.executeScript(null,{file: contentscript.js});
});
});
chrome.runtime.onMessage.addListener(
函数(request,sender,sendResponse){
if(request.message ===open_new_tab){
chrome.tabs.create({url:request.url});
}
}
);
好像background.js文件没有被执行。我怀疑这是一个权限。我需要什么权限才能在每个选项卡中运行此扩展程序?
因为我想让它在每个页面上运行这意味着我不能拥有内容脚本中的代码。我把所有的代码都移到了后台脚本中:
chrome.browserAction.onClicked.addListener(function(tab){
// ...
chrome.tabs.create({url:newTabUrl});
// ...
});
所以当我点击我的按钮时,上面的代码被调用,使用随附的jquery脚本。 p>
I have created a Chrome extension that, as part of it's operation, opens a new tab with a specified url.
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "open_new_tab" ) {
chrome.tabs.create({"url": request.url});
}
}
);
(Full code available on GitHub)
This works fine on tabs with webpages, but I cannot get it to work on empty tabs, for example: chrome://apps/
To clarify, if I have a tab open and it is on stackoverflow.com, then when I click on my extension button it opens a new tab loading a generated url. When I am on a new tab, or a tab where the url begins with chrome://
then the extension does not work.
What permissions do I need to include to allow the extension to open in ANY tab? Including new tabs and any chrome://
tab?
Manifest.json:
{
"manifest_version": 2,
"name": "MyMiniCity Checker",
"short_name": "MyMiniCity Checker",
"description": "Checks what your city needs most and redirects the browser accordingly.",
"version": "0.2",
"author":"Richard Parnaby-King",
"homepage_url": "https://github.com/richard-parnaby-king/MyMiniCity-Checker/",
"icons": {
"128": "icon-big.png"
},
"options_page": "options/options.html",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": ["tabs","storage","http://*.myminicity.com/","http://*/*", "https://*/*"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [ {
"matches": [ "http://*/*", "https://*/*"],
"js": [ "jquery-1.11.3.min.js" ]
}]
}
Background.js:
//When user clicks on button, run script
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, { file: "jquery-1.11.3.min.js" }, function() {
chrome.tabs.executeScript(null, { file: "contentscript.js" });
});
});
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if( request.message === "open_new_tab" ) {
chrome.tabs.create({"url": request.url});
}
}
);
It appears as though the background.js file is not being executed. I suspect this to be a permissions. What permissions do I need in order to run this extension in every tab?
As I was wanting this to run on EVERY page it meant I could not have the code in the content script. I moved all the code into the background script:
chrome.browserAction.onClicked.addListener(function(tab) {
//...
chrome.tabs.create({"url": newTabUrl});
//...
});
So when I click on my button the above code is called, using the enclosed jquery script.
这篇关于Chrome扩展程序在新标签上打开新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!