【问题标题】:In Firefox add-on, completion of any event handler in tab's content script triggers tab's "ready" event在 Firefox 插件中,完成选项卡内容脚本中的任何事件处理程序都会触发选项卡的“就绪”事件
【发布时间】:2014-04-07 20:34:33
【问题描述】:

Firefox add-on 中,完成我的标签内容脚本中的任何事件处理程序都会触发标签的“就绪”事件。

我正在尝试确定是否可以使用内容脚本来更改选项卡的内容(该选项卡最初包含本地存储在加载项的 data 文件夹中的 HTML 文件),然后重新访问选项卡进行进一步修改。对我来说不幸的是,选项卡中的页面似乎恢复为原始 HTML 文件(如 data/demo.js 中第 6 行的输出所证明的那样)并重新附加内容脚本,

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 + "'");
});

【问题讨论】:

    标签: tabs firefox-addon firefox-addon-sdk content-script


    【解决方案1】:

    使用 替换

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多