firefox附加组件中的contentScriptListener无法启动相应的文件

时间:2014-09-01 08:39:51

标签: javascript firefox firefox-addon firefox-addon-sdk

我开发了一个firefox。在main.js中,当我使用contentScriptFile调用js文件时,它无法调用,函数(addEventListener)从未被调用

 *** Edit 1 ***
 Sorry for missing what I actually need . 
 In get-text.js I need to send a XMLHttpRequest using GET method . If I attach my     javascript in panel.html , then I'm unable to receive  the request thereby ,

我在这里附上整个文件

main.js文件

var { ToggleButton } = require('sdk/ui/button/toggle');
var panels = require("sdk/panel");
var self = require("sdk/self");
var data=require("sdk/self").data;

var button = ToggleButton({
         id: "my-button",
         label: "my button",
         icon: {
              "16": "./icon.png",
              "32": "./icon.png",
               "64": "./icon.png"
          },
         onChange: handleChange
});

var panel = require("sdk/panel").Panel({
         width:350,
         contentURL: data.url("panel.html"),
         contentScriptFile: data.url("get-text.js"),
         onHide: handleHide
});
function handleChange(state) {
         if (state.checked) {
         panel.show({position: button});
  }
}

function handleHide() {
     button.state('window', {checked: false});
}

get-text.js文件

(function(){
   var init = function() {
       var xhr = new XMLHttpRequest();
       xhr.open("GET", "http://www.hackerearth.com/chrome-extension/events/", true);
       xhr.send();
       xhr.onreadystatechange = function () {
          if(xhr.readyState===4) {
               if(xhr.status===200) {
                  console.log("hello2");
                  var json = JSON.parse(xhr.responseText);
                  console.log(json);
               } else {
                  console.log("Status is :"+xhr.status);
              }
          }
       };
};
console.log("function passing");
document.addEventListener('DOMContentLoaded', function () {
    console.log("pankaj \t");
    init();
  });
});

*编辑1 *

1 个答案:

答案 0 :(得分:0)

如果您需要捕获面板文档的DOMContentLoaded事件,请放弃contentScriptFile并在get-text.js中添加panel.html

(脚本的行为相同,with a subtle difference关于port机制)

请注意,当您调用DOMContentLoaded构造函数时,Panel将被触发一次。对show / hide的后续调用不会触发它。