点击Chrome扩展程序的图标 [英] Chrome Extension's Icon on click

查看:274
本文介绍了点击Chrome扩展程序的图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击chrome扩展程序图标后,我很难理解如何运行一些JS.例如,我想在单击图标时从文档中读取一些属性.

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
]

在popup.html中,我有以下内容:

chrome.browserAction.onClicked.addListener(function(tab) {
    alert('working?');
});

但是,这似乎不起作用.我尝试将上面的JS放在后台脚本(在manifest.json内部)中,但这也不起作用.

解决方案

您可以使用两种方法:

方法1:使用后台脚本.

manifest.json中:

 "browser_action": {
    "default_icon": "icon.png"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
],
"background": {
    "persistent": false,
    "scripts": ["background.js"]
}
 

(您也可以使用"page": "background.html"代替"scripts".)

background.js中:

 chrome.browserAction.onClicked.addListener(function(tab) {
    alert('working?');
});
 

方法2:使用弹出窗口. manifest.json:

 "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
]
 

popup.html中:

 <html>
 <head>
  <script src="popup.js"></script>
 </head>
</html>
 

popup.js中:

 alert('working?');
 

您的问题是您将两者混在一起.如果您使用browser_action.default_popup,则chrome.browserAction.onClicked从不触发. (而且您不希望使用名为popup.html的背景页面,因为这会引起各种混乱.)

I'm having a difficult time understanding how to have some JS to run when the chrome extension icon has been clicked. I'd like to for example, read some properties from the document, when the icon has been clicked.

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
]

And inside the popup.html, I have the following:

chrome.browserAction.onClicked.addListener(function(tab) {
    alert('working?');
});

But, this doesn't appear to be working. I tried having the JS above inside a background script (inside manifest.json) but that didn't work either.

解决方案

There are two approaches you can use:

Approach 1: Use a background script.

in manifest.json:

"browser_action": {
    "default_icon": "icon.png"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
],
"background": {
    "persistent": false,
    "scripts": ["background.js"]
}

(You can also use "page": "background.html" instead of "scripts".)

in background.js:

chrome.browserAction.onClicked.addListener(function(tab) {
    alert('working?');
});

Approach 2: Use a popup. manifest.json:

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
},
"permissions": [
    "activeTab",
    "clipboardWrite"
]

in popup.html:

<html>
 <head>
  <script src="popup.js"></script>
 </head>
</html>

in popup.js:

alert('working?');

Your problem was that you were mixing the two. If you use a browser_action.default_popup, then chrome.browserAction.onClicked is never triggered. (And you wouldn’t want a background page named popup.html, since that would cause all sorts of confusion.)

这篇关于点击Chrome扩展程序的图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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