【发布时间】:2015-03-05 21:11:02
【问题描述】:
首先,我对 chrome 扩展非常陌生,所以如果我错过了一些愚蠢的事情,请指出给我。我有一个内容脚本,里面有一个消息侦听器,一个 html 页面,其中包含正在发送的实际消息的脚本。我得到了标签 id、url,但不能让监听器触发(或者我相信)。它在调整后随机工作了 5 次,但没有任何一致性。
我的 popup.html 页面
<html>
<head>
<title></title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/ServerCommunicator.js"></script>
<script type="text/javascript" src="js/util.js"></script>
<!--<script type="text/javascript" src="js/loginPageController.js"></script>-->
<script type="text/javascript" src="js/managementPageController.js"></script>
<link rel="stylesheet" type="text/css" href="extension.css" />
</head>
<body id="managementPage">
<div id="content">
<label id="manageLabel"></label>
<button id="manage" type="button">Manage</button>
<button id="request" type="button">Request</button>
<input id="code" type="text" placeholder="request code"/>
</div>
</body>
</html>
我的内容脚本
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.action == "test")
sendResponse({test: "good test"});
});
我的脚本加载到弹出窗口 (managementPageController.js)
document.getElementById("manage").onclick = function()
{
chrome.tabs.getSelected(null, function(tab)
{
alert("about to send request. id: " + tab.id);
// Send a request to the content script.
chrome.tabs.sendMessage(tab.id, {action: "test"}, function(response)
{
alert("tab url: " + tab.url);
alert("sent request: " + response.test);
});
});
}
还有我的清单文件
{
"manifest_version": 2,
"name": "Getting started example",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"browser_action": {
"default_icon": "logo.png",
"default_popup": "index.html",
"default_title": "Inserteck Chrome Extension8"
},
"permissions": [
"tabs",
"activeTab",
"https://ajax.googleapis.com/",
"http://199.168.255.214:8080/*"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/background.js"],
"runs_at": "document_start"
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"}
我得到了一个未定义的值,我认为这是因为无法调用侦听器。如果有人对修复或问题原因有任何意见,或者如果我只是误解了消息的使用,我将不胜感激。
【问题讨论】:
标签: javascript google-chrome-extension