Firefox extension: download and unpack ZIP - firefox

How can I download and unpack ZIP file to extension folder?
Need to update my resources/config from ZIP package stored online.
Is this possible?
Please point me over to documentation or examples
Thanks

In Firefox 4+ you can get the directory of your extension like this:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
AddonManager.getAddonByID(extensionID, function(addon) {
var extensionDir =
Services.io.getProtocolHandler("file").QueryInterface(Ci.nsIFileProtocolHandler).
getFileFromURLSpec(addon.getResourceURI(null).spec);
}
To download the file from an extension, create an XMLHttpRequest using:
var xhr = Cc["#mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpReques‌​t);
You can read the ZIP file using the nsIZipReader XPCOM interface (see http://mxr.mozilla.org/mozilla-central/source/modules/libjar/nsIZipReader.idl). Instantiate the component like this:
var zipReader = Cc["#mozilla.org/libjar/zip-reader;1"].createInstance(Ci.nsIZipReader);

Related

Laravel How to download all files in a directory

Using Laravel, I am trying to make a function which would download all files in a directory from S3.
I have used to retrieve all keys of the files using $file = Storage::disk('s3')->allFiles('path');, but I do not know what to do next.
It seems like I have to download all files into local, then zip those files, then download that zip file. I would like to know if there is a better way.
Any suggestion or advice would be appreciated.
I was able to do what I wanted with this:
http://coderaweso.me/zip-and-download-files-directory-from-amazon-s3-with-laravel/
It is not possible to send more than one file simultaneously over the same request with the HTTP protocol. Laravel also does not support this. You have to pack the files in, for example, a zip file.
Install Chumper/Zipper package and return a zip containing all your files:
$files = Storage::disk('s3')->allFiles('path');
Zipper::make(public_path('test.zip'))->add($files);
return response()->download(public_path('test.zip'));

Cannot open File protocol links in Resource protocol page in Firefox 41

I've made a local file (log.html) in Firefox profile and tried to open it in an add-on page (add-on folder/data/log.html and it's shown as Resource protocol in URL bar).
self.port && self.port.on('add-log-path', function(payLoad) {
addLogPath(payLoad);
});
function addLogPath(url) {
// url == "file:///Users/usr/Library/Application Support/Firefox/Profiles/05rhodfg.cfxo/log.html"
$('#logpath').attr('href', url);
}
I've also tried changing that to JS method window.open
function addLogPath(url) {
$('#logpath').on("click", function() {
window.open(url);
});
}
But the error is
JavaScript error: , line 0: Error: Access to
'file:///Users/usr/Library/Application%20Support/Firefox/Profiles/05rhodfg.cfxo/log.html'
from script denied
BTW, before Firefox version 41, it has no problem doing this.
Can you suggest other workaround to solve this? Thank you!
edit:
added add-on example to reproduce the problem
download and run the following statement in Terminal:
$ cd fileProtocolExample && cfx run
Do self.data.url('filename_here') to get that path to your file.
It will look something like resource://your-extension-id/data/filename_here, then this should load fine. That resource:// in front is important, make sure you get and use that URL.
The file:// won't work, because your addon is packed in a zip. Its not extracted into the system. How did it work in Firefox 41? Was your addon unpacked at that time? This is an Addon-SDK addon right?

How to create a xpi file from scratchpad

I have developed my add-on in scratchpad environment and now developing is finished and I want to create final xpi file.
I replace only this:
Cu.import('resource://gre/modules/ctypes.jsm');
by this:
var {Cu} = require("chrome");
var{ctypes} = Cu.import("resource://gre/modules/ctypes.jsm", null);
Then using nodejs (jpm init and jpm xpi commands) I created xpi file however this is not worked properly.
What we did was follow the jpm tutorial: https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_Started_%28jpm%29 and https://developer.mozilla.org/en-US/Add-ons/SDK/Tools/jpm#Installation
I did this on a Windows system:
we downloaded node.js
npm came with it
created a directory, in this directory i did jpm-init from command line
filled out the prompts then filled in the code for the addon:
We then created a similiar addon to this demo addon here:
https://github.com/Noitidart/jpm-chromeworker
I cant share the actuall addon as that was personal to the user. But the above is simpler and shows how to do it.
We did our jsctypes in a chromeworker, and have it communicate with index.js via messaging

Use relative path in Firefox extension

I develop Firefox extension with bundled executable file which should be run on browser startup.
To run process I need get nsIFile or nsILocalFile instance which points to executable file.
I know one solution how to get it using directory service:
var file = Components.classes["#mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties).get("ProfD", Components.interfaces.nsIFile);
file.append("extensions");
file.append("<extension id>");
file.append("<relative path>");
But this solution has two disadvantages:
It doesn't work in development mode, when instead of installed extension I have only text file with real extension path
I'm not sure that it will work on all Firefox configurations because of hardcoded "extensions" part of the path
So is there any nicer way to run executable file which comes with Firefox extension?
Thanks.
You are making way too many assumptions about the directory structure of the Firefox profile - don't. The Add-on Manager API lets you get the path of a file inside the extension, you should use it:
Components.utils.import("resource://gre/modules/AddonManager.jsm");
AddonManager.getAddonByID("<extension id>", function(addon)
{
var uri = addon.getResourceURI("<relative path>");
var file = uri.QueryInterface(Components.interfaces.nsIFileURL).file;
...
});
A restartless addon's startup function (in the bootstrap.js file) will receive, as its first parameter, the path where the addon is installed. You can then play various tricks to read files inside the .jar file, if any: see https://github.com/protz/GMail-Conversation-View/blob/master/bootstrap.js#L55 as an example.
In a non-restartless case, I must confess I don't have much of an idea :).
I found this thread looking for a way to reference a path to an image hosted in extension's directory from a content script. Here's a solution:
Include your files in web_accessible_resources in the extension's manifest.
"web_accessible_resources": [
"images/*"
]
Absolute paths to these resources contain randomly generated UUID, therefore we're using runtime.getUrl() giving it the path relative to manifest.json. Example:
let myImg = document.createElement('img');
myImg.src = browser.runtime.getURL("images/my-img.png")

Unzip files with node.js on Windows

What options are there to handle unzipping .zip files from within a Node.js script on Windows (XP)?
I'm working with the latest (at present) node.js v0.5.8 Windows node.exe.
Suggestions welcome.
-P.
I've found this zip library. It's very easy to install and use:
npm install zip
/* Only js dependencies, no local building needed */
from test.js
var z = require("zip");
var FS = require("fs");
var data = FS.readFileSync("test.zip")
var reader = z.Reader(data);
console.log(reader.toObject('utf-8'));
Provides also ways to iterate through the zip entries and getting data through Buffers.
ADM-ZIP is a pure JavaScript implementation for zip data compression for NodeJS.
Node provides support for the ZLIB library which should allow you to decompress a zip file using gzip: http://nodejs.org/docs/v0.5.8/api/zlib.html

Resources