chrome.browserAction.onClicked.addListener() with popup
chrome.browserAction.onClicked.addListener() with popup
我想在每次点击浏览器图标时触发的事件中添加Listener。我也有一个弹出,它出现在点击这个图标。
我尝试了chrome.browserAction.onClicked.addListener()
,但没有让它被解雇,后来我看到文档说:
Fired when a browser action icon is clicked.
This event will not fire if the browser action has a popup.
所以,我有弹出,所以这个Listener不起作用。在我的情况下,我可以做些什么来附加侦听器图标?
没有办法给这个事件附加一个监听器,但是你可以使用消息传递让后台页面知道弹出窗口已经打开。
在你的弹出窗口中,尽快:
chrome.runtime.sendMessage({popupOpen: true});
背景页:
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
if(message.popupOpen) { /* do your stuff */ }
});
对于@Xan的答案,我需要一些更明确的东西,所以我这样做了:
这是我的index.html
<!DOCTYPE html>
<html>
<head>
<title>Ahead of time compilation</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="loader.js"></script>
<script src="popup.js"></script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
<script src="dist/build.js"></script>
</html>
这是popup.js
chrome.runtime.sendMessage({popupOpen: true});
manifest.json
{
"manifest_version": 2,
"name" : "Test ang 2",
"description" : "jons test",
"short_name" : "test",
"version" : "1.0",
"browser_action": {
"default_icon" : "app/assets/icon.png",
"default_title": "hi jon",
"default_popup": "index.html"
},
"permissions": [
"debugger",
"activeTab",
"tabs",
"alarms",
"clipboardWrite",
"notifications",
"background",
"storage",
"cookies",
"https://*/",
"http://*/"
],
"web_accessible_resources": [
"assets/*",
"bower_components/*",
"components/*",
"app.module.js",
"app.routes.js",
"index.html",
"app/*"
],
"externally_connectable": {
"matches": [
"*://*.capitalone.com/*"
]
},
"background": {
"scripts":["background.js"]
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content-scripts.js"]
}
],
"content_security_policy": "script-src 'self' ; object-src 'self'"
}
background.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
console.log(message);
alert('hello world');
if(message.popupOpen) {
console.log('popup is open');
}
});
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- facebook”;添加评论“;popup获胜'不要消失
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- 从popup.js|Chrome扩展访问DOM
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- Understanding Javascript scope with "var that = this&qu
- Architecture for CPU intensive tasks with NodeJS & Socke
- How to declare a Map containing certain properties with flow
- Oauth 2 popup with Angular 2
- chrome.browserAction.onClicked.addListener() with popup
- Javascript window.open() with php code (true popup)