在Firefox add-on中,在我标签的内容脚本触发标签"准备好"事件
我试图弄清楚是否可以使用内容脚本来更改选项卡的内容(最初包含本地存储在附加组件中的HTML文件{ {1}}文件夹),稍后重新访问该选项卡以进行进一步修改。不幸的是,选项卡中的页面似乎恢复为原始HTML文件(由data
中第6行的输出证明)并重新附加内容脚本,
data/demo.js
:
lib/main.js
exports.main = function() {
var data = require("sdk/self").data;
// Create a widget that will open a tab:
require("sdk/widget").Widget({
id: "listener",
label: "listener demo",
content: "?",
onClick: function() {
// Open tab:
require("sdk/tabs").open({
url: data.url("demo.html"),
onReady: function(tab) {
var worker = tab.attach({
// Content script is re-run following EITHER button
// being pressed (whether or not 'demo' event listener
// is activated):
contentScriptFile: data.url("demo.js")
});
worker.port.on("demo-dun", function(message) {
// This message is output following pressing button
// with listener attached by content script:
console.log("'demo-dun' emitted message '" +
message + "'");
});
// Sample text below is received by content script every
// time content script is run, which is to say, every time
// either button is pressed.
worker.port.on("initialized", function () {
worker.port.emit("demo", "sample text");
});
}
});
}
});
};
(标签' s data/demo.html
):
content
<!DOCTYPE HTML>
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8">
<title>Listner demo</title>
</head>
<!-- The content script will remove the class from the body element. -->
<body class="uninitialized">
<form>
<!-- The following button has a click listener from the start -->
<button onclick="alert('hard coded event listener')">press me for hard-coded event listener</button>
<!-- An event listener will be added to the following button by the content script -->
<button id="demo">press me for event listener attached by content script</button>
</form>
</body>
</html>
(标签&#39; s data/demo.js
):
contentScriptFile
答案 0 :(得分:1)
使用&lt; input type =“button”/&gt;替换&lt; button&gt;并且它将正常工作。(或将&type;“按钮”添加到&lt; button&gt;) 因为在Firefox中,&lt; button&gt;的类型的默认值是submit。