admin管理员组文章数量:1135241
In my Stackoverflow folder, I have stackoverflow.ico
and 2 bellow files. When importing it to Chrome, it shows the icon in address bar, but when I click on it, Chrome doesn't open any new tab. What am I doing wrong?
manifest.json
{
"name": "Stackoverflow",
"version": "1",
"browser_action":
{
"default_icon": "stackoverflow.ico"
},
"background":
{
"page": "index.html"
},
"permissions": ["tabs"],
"manifest_version": 2
}
index.html
<html>
<head>
<script>
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var newURL = "/";
chrome.tabs.create({ url: newURL });
});
</script>
</head>
</html>
In my Stackoverflow folder, I have stackoverflow.ico
and 2 bellow files. When importing it to Chrome, it shows the icon in address bar, but when I click on it, Chrome doesn't open any new tab. What am I doing wrong?
manifest.json
{
"name": "Stackoverflow",
"version": "1",
"browser_action":
{
"default_icon": "stackoverflow.ico"
},
"background":
{
"page": "index.html"
},
"permissions": ["tabs"],
"manifest_version": 2
}
index.html
<html>
<head>
<script>
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var newURL = "http://stackoverflow.com/";
chrome.tabs.create({ url: newURL });
});
</script>
</head>
</html>
Share
Improve this question
edited Dec 27, 2016 at 11:53
NoName
asked May 12, 2013 at 3:22
NoNameNoName
8,02515 gold badges60 silver badges114 bronze badges
0
4 Answers
Reset to default 156The problem is that you are violating manifest version 2's content security policy
. To fix it all you have to do is get rid of inline script, in this case your background page
. Turn it into a background script
like this:
manifest.json
"background":{
"scripts": ["background.js"]
},
background.js
chrome.browserAction.onClicked.addListener(function(activeTab){
var newURL = "http://stackoverflow.com/";
chrome.tabs.create({ url: newURL });
});
If, for some reason, you do need it to be a page, then simply include the script as an external file and declare it as a page like before.
In my case I needed to open link in a new tab when I clicked a link within extension popup window, it worked fine with target
attribute set to _blank
:
<a href="http://www.example.com" target="_blank">Example</a>
I would prefer simpler solution - just add action to onclick
$('body').on('click', 'a[target="_blank"]', function(e){
e.preventDefault();
chrome.tabs.create({url: $(this).prop('href'), active: false});
return false;
});
This will open all links (even links that were dynamically created) that have target="_blank" attribute in a new tab without loosing popup focus.
You do not need jQuery. Just use window.open("http://example.com", "_blank")
.
本文标签: javascriptChrome extension How to open a link in new tabStack Overflow
版权声明:本文标题:javascript - Chrome extension: How to open a link in new tab? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736920196a1956435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论