Chrome扩展程序在新选项卡中打开链接 - 没有错误,但在点击时不做任何事情 [英] chrome extension to open link in new tab - no error, but does nothing on click

查看:323
本文介绍了Chrome扩展程序在新选项卡中打开链接 - 没有错误,但在点击时不做任何事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个非常简单的Chrome扩展程序,以便在单击时在新选项卡中打开硬编码链接,而且我没有任何运气。添加扩展后,图标显示出来,但点击它时没有任何反应。任何建议?



manifest.json

  {
name:Drive Button,
version:1.0,
manifest_version:2,
description:打开Goog​​le Drive,
browser_action:{
default_icon:icon.png
},
background:background.html,
permissions:[
标签
]
}

background.html

 < html> 
< head>
< script>
//当用户点击浏览器动作时调用。
chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.create({url:http://drive.google.com});
}) ;
< / script>
< / head>
< / html>


解决方案

脚本中存在一些问题




  • 清单注册

  • CSP



清单注册



您应该将背景注册为

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



 background:{
page:background.html
},



CSP



如果您希望将背景设置为 html 页面,请在html页面中删除< script> 标签以遵守内容安全政策



消除后这些问题让我的脚本运行。



工作版本



manifest.json

注册

  {
name:Drive Button,
版本:1.0,
manifest_version:2,
description:打开Goog​​le Drive,
browser_action:{
default_icon:icon .png

background:{
scripts:[
background.js

},
权限:[
标签
]
}

background.js



无需任何修改就可以使用您的代码。

  chrome.browserAction.onClicked.addListener(function(tab){
chrome.tabs.create({
url:http://drive.google.com
});
});

我能够按预期创建窗口。



参考资料




I'm trying to create a really simple chrome extension to open a hard-coded link in a new tab when clicked, and I'm not having any luck. After adding the extension the icon shows up, but nothing happens when I click on it. Any suggestions?

manifest.json

{
"name": "Drive Button",
"version": "1.0",
"manifest_version": 2,
"description": "Open Google Drive",
"browser_action": {
"default_icon": "icon.png"
},
"background": "background.html",
"permissions": [
"tabs"
]
}

background.html

<html>
<head>
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.create({url: "http://drive.google.com"});
});
</script>
</head>
</html>

解决方案

There are some problems in your script

  • Manifest registration
  • CSP

Manifest registration

You should register background as

"background": {
    "scripts": ["background.js"]
  },

or

"background": {
    "page": "background.html"
  },

CSP

If you are looking to have background as a html page eliminate <script> tag in html page to adhere Content Security Policy.

After Eliminating these problems i got your script running.

Working Version

manifest.json

Registered background page to manifest file.

{
    "name": "Drive Button",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Open Google Drive",
    "browser_action": {
        "default_icon": "icon.png"
    },
    "background": {
        "scripts": [
            "background.js"
        ]
    },
    "permissions": [
        "tabs"
    ]
}

background.js

Used your code without any modifications.

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.create({
        url: "http://drive.google.com"
    });
});

I was able to create window as expected.

References

这篇关于Chrome扩展程序在新选项卡中打开链接 - 没有错误,但在点击时不做任何事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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