2014-09-01 16 views
0

我開發了一個Firefox附加組件。在當我打電話使用contentScriptFile一個js文件就無法調用main.js,函數(的addEventListener)得到從來沒有所謂的firefox插件中的contentScriptListener無法啓動相應文件

*** 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 *

+0

你正在使用addon-sdk所以用[標籤:firefox-addon-sdk]標記你的東西:)做筆記,因爲我看到你做了幾次:) – Noitidart 2014-09-01 13:32:59

+0

'panel.show()'出現在任何地方你的插件? – Lori 2014-09-01 15:12:05

+1

@Noitidart因爲我不知道,所以我會關心它。 – user1305313 2014-09-01 16:17:32

回答

0

如果需要趕DOMContentLoaded活動小組的文件,然後從panel.html下降contentScriptFile和包括get-text.js

(該腳本將具有相同的行爲,with a subtle difference關於port機制)

記住,DOMContentLoaded將觸發一次,當你調用Panel構造。對show/hide的後續調用將不會觸發它。

+0

他可以通過在面板的'onHide'函數中執行'panel.destroy()'來多次執行DOMContentLoaded fire? – Noitidart 2014-09-01 21:34:44

+0

@paa通過你已經告訴的方法**我可以調用該事件,但不能有一個XMLHttpRequest到遠程服務器** – user1305313 2014-09-02 04:33:27

+0

@ user1305313你的意思是當'get-text.js'通過'contentScriptFile'包含時,它能夠做遠程XHR? @Noitidart,我的觀點是'show()'不會觸發'DOMContentLoaded'。顯然,當你調用'destroy()'並創建一個新的'Panel'時,它將觸發事件。 – paa 2014-09-02 07:43:05