Use <input type="button" /> to replace the <button> and it will work fine.(or add type="button" to the <button>)
Because in firefox, the default value of <button>'s type is submit.
In Firefox add-on, completion of any event handler in tab's content script triggers tab's "ready" event
-
29-06-2023 - |
Question
In a Firefox add-on, completion of any event handler in my tab's content script triggers tab's "ready" event.
I'm trying to figure out if it's possible to use a content script to alter the contents of a tab (which originally contains an HTML file stored locally in the add-on's data
folder), and later re-visit the tab for further modifications. Unfortunately for me, the page in the tab seems to revert to the original HTML file (as evidenced by the output of line 6 in data/demo.js
) and to re-attach the content script,
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");
});
}
});
}
});
};
data/demo.html
(the tab's 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>
data/demo.js
(the tab's contentScriptFile
):
// content script is re-entered after pressing either button:
alert("entered content script");
// "initialized" event will be emitted once for each tab, IN THEORY.
bodyClasses = document.body.classList;
console.log("body classes: "+JSON.stringify(bodyClasses));
if (bodyClasses.contains("uninitialized")) {
bodyClasses.remove("uninitialized");
self.port.emit("initialized");
}
document.getElementById("demo").addEventListener("click", function() {
alert("event listener added by content script");
self.port.emit("demo-dun", "demo dun");
});
self.port.on("demo", function(text) {
alert("'demo' event listener called with message '" + text + "'");
});
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow