2012-02-26 787 views
2

我剛開始使用Firefox製作插件。這個附加組件是爲了在FF之外打開一個本地文件夾而編寫的。該文件夾已被瀏覽器打開。在上下文菜單中,你會看到一個選項來打開瀏覽器外的文件夾(我使用Win7)。 這是我使用的代碼:在Firefox中打開本地文件夾的插件

var contextMenu = require("context-menu"); 

var menuItem = contextMenu.Item({ 
    label: "Open Local File", 
    context: contextMenu.URLContext("file:///*"), 
    contentScript: 'self.on("click", function() {'+ 
         'openDir(document.URL);'+ 
        '});', 
}); 

function openDir(val) 
{ 
    if (val == "") 
    { 
     alert("Directory not defined"); 
     return; 
    } 
    if(navigator.userAgent.indexOf("Firefox") == -1) 
    { 
     alert("Currently active folder links supported only for Mozilla Firefox web browser"); 
     return; 
    } 
    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
    var localFile = 
     Components.classes["@mozilla.org/file/local;1"] 
     .createInstance(Components.interfaces.nsILocalFile); 

    var env = 
     Components.classes["@mozilla.org/process/environment;1"] 
     .createInstance(Components.interfaces.nsIEnvironment); 

    var systemRoot = env.get("SystemRoot"); 
    if (systemRoot == "") 
    { 
     alert("Unable to retrieve SystemRoot environment variable"); 
    } 

    localFile.initWithPath(systemRoot + "\\explorer.exe"); 
    var process = 
     Components.classes["@mozilla.org/process/util;1"] 
     .createInstance(Components.interfaces.nsIProcess); 
    process.init(localFile); 
    process.run(false, Array(val), 1); 
} 

現在的問題是,當我保存附加http://builder.addons.mozilla.org/下......它不能被編譯。相反,一個紅色框出現了「XPI未建立」的消息。這是日誌:

GET https://builder.addons.mozilla.org/xpi/test/.../ 404 NOT FOUND 236ms

我應該怎麼辦?


修改後的代碼:

var contextMenu = require("context-menu"); 

    var menuItem = contextMenu.Item({ 
     label: "Open Local File", 
     contentScript: 'self.on("context", function(node)'+ 
         '{'+ 
         ' return node.ownerDocument.URL.indexOf("file:///") == 0;'+ 
         '});'+ 
         'self.on("click", function(node)' + 
         '{' + 
         ' self.postMessage(node.ownerDocument.URL);' + 
         '});', 

     onMessage: function(url) 
     { 
      openDir(url); 
     } 

     }) ; 

    function openDir(val) 
    { 
     var {Cc, Ci} = require("chrome"); 
     var ioService = Cc["@mozilla.org/network/io-service;1"] 
          .getService(Ci.nsIIOService); 
     var uri = ioService.newURI(val, null, null); 
     if (uri instanceof Ci.nsIFileURL && uri.file.isDirectory()) 
     { 
      uri.file.QueryInterface(Ci.nsILocalFile).launch(); 
     } 
    } 

回答

2

此插件生成器的Web應用程序是有打包你的代碼,並創建一個擴展 - 一旦完成火狐僅僅安裝擴展。附加組件生成器存在問題,而不是Firefox。我只能推薦你file a bug report

您的代碼有許多問題,但是:

  • 看來你想顯示在使用file:/// URL方案,而不是指向文件的鏈接頁面的上下文菜單項。有這個沒有預定義的情況下,你將不得不使用內容的腳本(見Specifying Contexts > In Content Scripts喜歡的東西:
self.on("context", function(node) 
{ 
    return node.ownerDocument.URL.indexOf("file:///") == 0; 
}); 
  • 功能openDir()沒有在內容腳本定義,它是指你。擴展這意味着你必須發送一條信息給你的擴展與URL(見Handling Menu Item Clicks最後一個例子),事情是這樣的:
contentScript: 'self.on("context", ...);' + 
       'self.on("click", function(node, data)' + 
       '{' + 
       ' self.postMessage(node.ownerDocument.URL);' + 
       '});', 
onMessage: function(url) 
{ 
    openDir(url); 
} 
  • 檢查您的代碼是否在Firefox中運行毫無意義 - 目前,附加SDK僅支持Firefox。
  • 您不應該使用已棄用的PrivilegeManager.enablePrivilege方法 - 您的代碼已經以最高權限運行。但是,您需要use chrome authority,但默認情況下,使用附加SDK構建的擴展無法訪問低級功能。
  • 您不應該直接運行Windows資源管理器,使用nsILocalFile.launch(),它將運行Windows資源管理器(或操作系統中定義的任何操作以打開目錄)。總之在openDir()的代碼應該是這樣的:
var {Cc, Ci} = require("chrome"); 
var ioService = Cc["@mozilla.org/network/io-service;1"] 
        .getService(Ci.nsIIOService); 
var uri = ioService.newURI(val, null, null); 
if (uri instanceof Ci.nsIFileURL && uri.file.isDirectory()) 
    uri.file.QueryInterface(Ci.nsILocalFile).launch(); 

文檔:nsIIOServicensIFileURL

+0

非常感謝。我已將附加到我剛纔附加到我的文章的代碼上。但似乎onMessage內的代碼不會運行。 – limacina 2012-02-27 18:06:08

+0

你在main.js的exports.main中運行嗎? – 2013-08-27 04:08:27

+0

@BrettZamir:沒關係。一些在基於SDK的擴展中運行的代碼。 – 2013-08-27 06:02:28