如此简单的事情怎么会如此不可能?
我要做的就是单击我的扩展程序的 browser_action 按钮,打开一个带有几个设置的表单,然后单击表单上的按钮来启动一个进程。
我一生都无法在后台表单中单击按钮来工作。
我试图让http://developer.chrome.com/extensions/contentSecurityPolicy.html#H2-3上的示例工作,但它没有。browser_action 和 background 的规则有区别吗?这就是我的事件监听器没有触发的原因吗?
有人可以提供一个工作示例吗?
清单.json:
{
"name": "Convert",
"version": "0.1",
"description": "Converts the current page",
"browser_action": {
"default_icon": "exticon.png",
"default_popup": "background.html"
},
"content_scripts": [{
"matches": ["*://*/*"],
"js": ["contentscript_static.js"]
}],
"permissions": [
"tabs", "http://*/*", "https://*/*"
]
}
背景.html:
<html>
<head>
<title>Converter</title>
<script src="background.js"/>
<script>
// Initialize the localStorage
if (null == localStorage["htmlImport"])
localStorage["htmlImport"] = false;
// Called when the user clicks on the browser action icon.
chrome.browserAction.onClicked.addListener(function(tab) {
console.log('in listener');
// execute the content script
chrome.tabs.executeScript(null,
{
file: "contentscript.js",
allFrames: true // It doesn't work before 4.0.266.0.
});
});
// Listen to the requests from the content script
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
switch (request.name)
{
case "getPreferences":
sendResponse(
{
prefIgnoreLinks : localStorage["htmlImport"]
});
break;
case "PressShortcut":
sendResponse({}); // don't response.
// execute the content script
chrome.tabs.executeScript(null,
{
file: "contentscript.js",
allFrames: true // It doesn't work before 4.0.266.0.
});
break;
default:
sendResponse({}); // don't response.
break;
}
});
</script>
</head>
<body style='min-width:250px;'>
Link depth: <input type='text' name='depth' value='3'/><br/>
<input type='checkbox' name='changedomain'>Include external domains</input><br/>
<button id='beginConvert'>Convert</button>
</body>
</html>
背景.js:
function awesome() {
// Do something awesome!
console.log('awesome')
}
function totallyAwesome() {
// do something TOTALLY awesome!
console.log('totallyAwesome')
}
function awesomeTask() {
awesome();
totallyAwesome();
}
function clickHandler(e) {
setTimeout(awesomeTask, 1000);
}
// Add event listeners once the DOM has fully loaded by listening for the
// `DOMContentLoaded` event on the document, and adding your listeners to
// specific elements when it triggers.
//document.addEventListener('DOMContentLoaded', function () {
// document.querySelector('button').addEventListener('click', clickHandler);
//});
// Add event listeners once the DOM has fully loaded by listening for the
// DOMContentLoaded event on the document, and adding your listeners to
// specific elements when it triggers.
document.addEventListener('DOMContentLoaded', function () {
// console.log('event listener for button connected to beginConversion()');
//document.querySelector('button').addEventListener('click', beginConversion);
document.getElementById('beginConvert').addEventListener('click', clickHandler);
});