【发布时间】:2015-05-11 17:12:30
【问题描述】:
我的项目是一个 Chrome 扩展程序,它将执行以下操作。
- 按下扩展图标。
- 将出现弹出窗口(来自 popup.html)
- 5 个按钮将出现在弹出窗口中。
- 当您单击四个按钮之一时,将执行一个 javascript 代码。
- 关闭弹出窗口。
所以取决于这里这篇文章的答案
Detect a button click in the browser_action form of a Google Chrome Extension
(感谢迈克尔的巨大帮助)
此示例仅适用于一个按钮。仅使用我的一个 javascript 代码创建它并且工作完美。 但是当涉及到所有 5 个按钮时,我尝试进行这种编码,但它根本不起作用(我是 javascript 代码的新手,所以不要讨厌)
这里是代码
MANIFEST.JSON
{
"background": {
"scripts": [ "background.js" ]
},
"browser_action": {
"default_icon": "img/icon.png",
"default_title": "TITLE",
"default_popup": "popup.html"
},
"icons": {
"128": "img/icon_128.png",
"19": "img/icon19.png",
"38": "img/icon38.png",
"48": "img/icon_48_2.png"
},
"manifest_version": 2,
"name": " NAME",
"description": " DESCR ",
"permissions": [ "activeTab" ],
"version": "2.0"
}
POPUP.HTML
<html>
<head>
<script src="popup.js"></script>
<style type="text/css" media="screen">
body { min-width:250px; text-align: center; }
#click-me-l { font-size: 20px; }
#click-me-f { font-size: 20px; }
</style>
</head>
<body>
<button id='click-me-l'>Click1</button>
<button id='click-me-f'>Click2</button>
</body>
</html>
POPUP.JS
function clickHandler(e) {
chrome.extension.sendMessage({directive: "popup-click-l"}, function(response) {
this.close(); // close the popup when the background finishes processing request
});
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('click-me-l').addEventListener('click', clickHandler);
})
function clickHandler(e) {
chrome.extension.sendMessage({directive: "popup-click-f"}, function(response) {
this.close(); // close the popup when the background finishes processing request
});
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('click-me-f').addEventListener('click', clickHandler);
})
BACKGROUND.JS
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
switch (request.directive) {
case 1 "popup-click-l":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
file: "script1.js", // script to inject into page and run in sandbox
allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0.
});
case 2 "popup-click-f":
// execute the content script
chrome.tabs.executeScript(null, { // defaults to the current tab
file: "script2.js", // script to inject into page and run in sandbox
allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0.
});
sendResponse({}); // sending back empty response to sender
break;
default:
// helps debug when request directive doesn't match
alert("Unmatched request of '" + request + "' from script to background.js from " + sender);
}
}
);
因此链接中的代码仅适用于 1 个按钮。 在这个例子中,我试图让它适用于 2 个按钮,但我找不到我做错了什么。如果有人有任何想法,我将不胜感激。
非常感谢您的宝贵时间!!!
(更新 2。更新了 2 个按钮的代码,但不起作用。)
【问题讨论】:
-
警告:您以一种使现有答案无效的方式编辑了您的问题。一般来说,您希望避免这种情况。
-
@Xan 我对 Teepeemm 说我会编辑它......我不是故意这样做的,所以答案将是题外话。我只是从我的问题的乞求开始。
-
你的意思可能是“开始” 这不是题外话,如果你让答案看起来不正确,这只是一个冒险的编辑。如果您与答案的作者一起清除它,那么没有问题。
-
是的,这是一个错字。无论如何,他的代码让我有点困惑,因为它们似乎都不适用于我的问题。
-
由于您的目标是五个按钮,我的建议是回到尝试两个按钮的原始版本。让一个按钮起作用,然后问“我如何获得更多”似乎不是一个有趣的问题。
标签: javascript google-chrome google-chrome-extension