Chrome扩展程序:如何在新标签页中打开链接? [英] Chrome extension: How to open a link in new tab?

查看:161
本文介绍了Chrome扩展程序:如何在新标签页中打开链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Stackoverflow文件夹中,我有 stackoverflow.ico 和2个波纹管文件。将其导入Chrome后,它会在地址栏中显示该图标,但当我点击它时,Chrome不会打开任何新标签页。我做错了什么?



manifest.json

  {
name:Stackoverflow,
version:1,
browser_action:
{
default_icon :stackoverflow.ico
},
background:
{
page:index.html
},
权限:[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>


解决方案

问题是您违反了最新版本2的内容安全策略 。要解决它,你所要做的就是摆脱内联脚本,在这种情况下你的背景 page 。将其转换为背景脚本,如下所示:
$ b manifest.json

background:{
scripts:[background.js]
$,

background.js

chrome.browserAction.onClicked.addListener(function(activeTab){
var newURL =http:// stackoverflow .com /;
chrome.tabs.create({url:newURL});
});

如果由于某种原因,您确实需要它成为一个页面,那么只需将脚本包含为一个外部文件并像以前一样将其声明为一个页面。


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>

解决方案

The 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.

这篇关于Chrome扩展程序:如何在新标签页中打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆