2015-04-19 79 views
2

我正在開發一個帶addon sdk的firefox插件,但我沒有成功地在插件中導入外部庫。重要的是要知道,插件打開一個新的標籤,我需要導入的庫。在firefox插件中加載外部庫

這是我的文件夾結構:

> data 
    > jquery.js 
    > pixlr.js 
> lib 
    > main.js 

要導入庫的jquery.js和pixlr.js,我曾嘗試已經多件事情。由於這些庫需要在新的選項卡訪問,我已經試過如下:

tabs.open({ 
    url: startURL, 
    inNewWindow: inNewWindow, 
    inBackground: inBackground, 
    contentScriptFile: [self.data.url("jquery.js")], 
    onReady: function (tab) { 
     tab.attach({ 
      contentScriptFile: [self.data.url("jquery.js"), 
           self.data.url("pixlr.js")] 
     }); 
    } 
}); 

有誰知道爲什麼會不工作?提前致謝!

+0

您能否提供更多信息?你的擴展何時調用'tabs.open'? – dgil

回答

0

雖然從erikvold答案沒有工作對我來說,我還發現了另一個(以及一些簡單的)解決方案:

Cu.import(self.data.url("pixlr.js", this)); 
Cu.import(self.data.url("urlShortener.js", this)); 

您可以只使用導入功能改爲由Firefox addon sdk提供。我也想包含jquery,但是這對於插件運行的沙盒模式並不適用。由於我主要使用jQuery發送ajax請求,所以我使用了Firefox插件sdk提供的Request模塊。

感謝您的幫助。

+0

我不知道爲什麼這會對你有所幫助。 – canuckistani

2

假設startURL是加載項中的文件的url(儘管您尚未明確startURL的用途)。

試試這個:

tabs.open({ 
    url: startURL, 
    inNewWindow: inNewWindow, 
    inBackground: inBackground, 
    onReady: function (tab) { 
     tab.attach({ 
      contentScript: "self.options.urls.forEach(url => {" + 
          "var script = document.createElement('script')" + 
          "script.src = url;" + 
          "document.body.appendChild(script);" + 
         "});", 
      contentScriptOptions: { 
      urls: [ 
       self.data.url("jquery.js"), 
       self.data.url("pixlr.js") 
      ] 
      } 
     }); 
    } 
});