Firefox add-on에서 내 탭의 콘텐츠 스크립트에서 이벤트 처리기를 완료하면 탭의 "준비 됨"이벤트가 트리거됩니다.Firefox 부가 기능에서 탭의 콘텐츠 스크립트에서 이벤트 처리기가 완료되면 탭의 "준비 됨"이벤트가 발생합니다
콘텐츠 스크립트를 사용하여 탭 (원래 추가 기능의 data
폴더에 저장된 HTML 파일을 포함)의 내용을 변경할 수 있는지 알아 내려고 나중에 다시 방문하십시오. 탭을 클릭하십시오. 불행하게도 나를 위해, 탭의 페이지를 다시 부착 컨텐츠 스크립트에 (data/demo.js
라인 (6)의 출력에 의해 입증) 원래 HTML 파일에
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
(탭의 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
(탭의 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 + "'");
});