如何在新标签页中打开链接

如何在新标签页中打开链接

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

问题描述

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

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?

ma​​nifest.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>

推荐答案

问题是你违反了 manifest version 2's 内容安全策略.要修复它,您只需删除内联脚本,在这种情况下是您的背景 page.把它变成背景 script 像这样:

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:

ma​​nifest.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 扩展程序:如何在新标签页中打开链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:26