2010-08-26 74 views
1

我正在尝试创建一个SIMPLE插件,不需要任何界面,它将自动下载并保存到“Desktop/MyFolder /”页面加载的所有内容。我的想法是扩展FireBug,但这似乎颇具挑战性。我知道它做了一些事情,但是在图像,flv和mp3等内容中,内容似乎放置在文件中,但是当我尝试查看它们时,它们不是可查看/无效的格式。Firefox Plugin正在下载

我想我只需要做一些MimeType或文件格式的东西。它确实看起来不错,但显然缺少一些东西。

在此先感谢!

FBL.ns(function() { with (FBL) { 

const Cc = Components.classes; 
const Ci = Components.interfaces; 

const dirService = Cc["@mozilla.org/file/directory_service;1"] 
    .getService(Ci.nsIProperties); 

// ************************************************************************************************ 
// Module implementation 

Firebug.EverythingExportModule = extend(Firebug.Module, 
{ 
    initialize: function(owner) 
    { 
     Firebug.Module.initialize.apply(this, arguments); 

     // Register NetMonitor listener 
     this.netListener = new EverythingExport(); 
     Firebug.NetMonitor.addListener(this.netListener); 
    }, 

    shutdown: function() 
    { 
     Firebug.Module.shutdown.apply(this, arguments); 

     // Unregister NetMonitor listener 
     Firebug.NetMonitor.removeListener(this.netListener); 
     this.netListener.outputStream.close(); 
    } 
}); 

// ************************************************************************************************ 
// Net Panel Listener 

function EverythingExport(outputStream) 
{ 
    // Get unique file within user profile directory. 
    var file = dirService.get("ProfD", Ci.nsIFile); 
    file.append("netlistener"); 
    file.append("netMonitor.txt"); 
    file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, 0666); 

    // Initialize output stream. 
    this.outputStream = 
     Cc["@mozilla.org/network/file-output-stream;1"] 
     .createInstance(Ci.nsIFileOutputStream); 

    // write, create, truncate 
    this.outputStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
} 

EverythingExport.prototype = 
{ 
    onRequest: function(context, file) 
    { 
     if (FBTrace.DBG_NETLISTENER) 
      FBTrace.sysout("netListener.onResponse; " + (file ? file.href : "")); 
    }, 

    onExamineResponse: function(context, request) 
    { 
     if (FBTrace.DBG_NETLISTENER) 
      FBTrace.sysout("netListener.onExamineResponse;" + request.name); 
    }, 

    onResponse: function(context, file) 
    { 
     return; 
     if (FBTrace.DBG_NETLISTENER) 
      FBTrace.sysout("netListener.onResponse; " + (file ? file.href : "")); 

     try 
     { 
      var text = file.href + " (" + formatTime 
       (file.endTime - file.startTime) + ")\n"; 
      this.outputStream.write(text, text.length); 
     } 
     catch (err) 
     { 
      if (FBTrace.DBG_NETLISTENER || FBTRace.DBG_ERRORS) 
       FBTrace.sysout("netListener.onResponse; EXCEPTION", err); 
     } 
    }, 

    onResponseBody: function(context, file) 
    { 

     Firebug.Console.openGroup("EverythingDownloader", null, "group", null, false); 
     Firebug.Console.log("Found File"); 
     Firebug.Console.log(file); 
     Firebug.Console.log(context); 
     Firebug.Console.log(this.transport); 
     Firebug.Console.log(this); 
     Firebug.Console.log(file.mimeType); 
     savefile="C:\\Users\\MyUserName\\Desktop\\MyFolder\\" + file.startTime + "-music.mp3"; 
     //Yes I know that is not cross-platform friendly... 
     var req = new XMLHttpRequest(); 
     req.onreadystatechange = function() 
     { 
      if(this.readyState == 4 && this.status == 200) { 
       Firebug.Console.log(req); 
       try { 
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); 
       } catch (e) { 
        alert("Permission to save file was denied."); 
       } 
       var file = Components.classes["@mozilla.org/file/local;1"] 
        .createInstance(Components.interfaces.nsILocalFile); 
       file.initWithPath(savefile); 
       if (file.exists() == false) { 
        Firebug.Console.log("Creating file... "); 
        file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420); 
       } 
       var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"] 
        .createInstance(Components.interfaces.nsIFileOutputStream); 
       /* Open flags 
       #define PR_RDONLY  0x01 
       #define PR_WRONLY  0x02 
       #define PR_RDWR   0x04 
       #define PR_CREATE_FILE 0x08 
       #define PR_APPEND  0x10 
       #define PR_TRUNCATE  0x20 
       #define PR_SYNC   0x40 
       #define PR_EXCL   0x80 
       */ 
       /* 
       ** File modes .... 
       ** 
       ** CAVEAT: 'mode' is currently only applicable on UNIX platforms. 
       ** The 'mode' argument may be ignored by PR_Open on other platforms. 
       ** 
       ** 00400 Read by owner. 
       ** 00200 Write by owner. 
       ** 00100 Execute (search if a directory) by owner. 
       ** 00040 Read by group. 
       ** 00020 Write by group. 
       ** 00010 Execute by group. 
       ** 00004 Read by others. 
       ** 00002 Write by others 
       ** 00001 Execute by others. 
       ** 
       */ 
       outputStream.init(file, 0x04 | 0x08 | 0x20, 420, 0); 
       var result = outputStream.write(this.responseText, this.responseText.length); 
       Firebug.Console.log("Done!"); 
       outputStream.close(); 
       Firebug.Console.closeGroup(); 
      } 
     } 

     req.open("GET", file.href, true); 
     req.send(null); 
     return; 
     if (FBTrace.DBG_NETLISTENER) 
      FBTrace.sysout("netListener.onResponseBody; " + (file ? file.href : ""), file); 
     //Firebug.Console.log(file); 
     // 
    } 
}; 

var savefile=""; 
// ************************************************************************************************ 
// Registration 

Firebug.registerModule(Firebug.EverythingExportModule); 

// ************************************************************************************************ 
}}); 
+0

如果您简要描述您已经尝试过的内容(与发布完整的源代码不同)以及什么是或不在,您可能会得到更多更好的回应。 – MatrixFrog 2010-08-27 06:37:10

+0

从Firefox启动“将页面另存为”会不会更容易?监视页面加载,然后调用firefox动作。 – Thomas 2010-09-13 11:17:09

回答

0

好了,好,我不能figre说出来,所以我所做的就是有一个C#程序做文件系统观察(谷歌它),当它看到文件大小界限我一直在寻找,它内部的文件复制并重新命名为我需要的地方。这不是一个FireBug插件,但它确实有效。

+0

(UPDATE)另一个可以帮助完成此工作的插件叫做BetterCache(一个Firefox插件)。无论出于何种原因,都不会在谷歌上找到它,你必须在firefox插件网站上进行一点点挖掘。如果您发现问题,请告诉我。 – Nitroware 2010-11-11 01:42:53