Skip to content

Instantly share code, notes, and snippets.

@jscher2000
Last active March 18, 2024 03:10
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jscher2000/4403507e33df0918289619edb83f8193 to your computer and use it in GitHub Desktop.
Browser Console Snippet to decompress mozlz4 and jsonlz4 files
/* Decompression Script for the Browser Console
NOTE: BEFORE RUNNING THIS SCRIPT, CHECK THIS SETTING:
Type or paste about:config into the address bar and press Enter
Click the button promising to be careful
In the search box type devt and pause while Firefox filters the list
If devtools.chrome.enabled is false, double-click it to toggle to true
Paste this entire script into the command line at the bottom of the Browser Console (Windows: Ctrl+Shift+j)
Then press Enter to run the script. A file picker should promptly open.
See: http://forums.mozillazine.org/viewtopic.php?p=14111285#p14111285
*/
var {utils:Cu} = Components;
// Set up file chooser
var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
var fu = Cu.import("resource://gre/modules/FileUtils.jsm").FileUtils
try { // Fx125+
fp.init(window.browsingContext, 'Open File', Components.interfaces.nsIFilePicker.modeOpen);
} catch(e) { // Fx124 and earlier
fp.init(window, 'Open File', Components.interfaces.nsIFilePicker.modeOpen);
}
fp.appendFilter("LZ4 Compressed Files", "*.mozlz4;*.jsonlz4*");
//fp.displayDirectory = fu.File(OS.Constants.Path.profileDir);
fp.displayDirectory = fu.File(PathUtils.profileDir); // Fx104
// Call file chooser
fp.open((aResult) => {
if (aResult == Components.interfaces.nsIFilePicker.returnOK) {
if (fp.file.exists() && fp.file.isFile() && fp.file.isReadable()) {
var oldfile = fp.file.path;
// Construct output file name
var newfile = oldfile + "_converted.json";
try {
Cu.import("resource://gre/modules/osfile.jsm");
//let promise = OS.File.read(oldfile,{compression:"lz4"});
let promise = IOUtils.readUTF8(oldfile, { decompress: true }); // Fx104
promise = promise.then(jsonString => {
//OS.File.writeAtomic(newfile, jsonString);
IOUtils.writeUTF8(newfile, jsonString); // Fx104
});
promise = promise.then(retval => {
console.log('Saved as: "' + newfile + '"');
});
}
catch (err) {
console.log(err);
}
}
}
});
@jscher2000
Copy link
Author

jscher2000 commented Nov 6, 2017

I created a pair of webapps that are simpler to use:

@jscher2000
Copy link
Author

Updates for Firefox 104:

  • OS.Constants.Path.profileDir to PathUtils.profileDir
  • OS.File.read() to IOUtils.readUTF8()
  • OS.File.writeAtomic() to IOUtils.writeUTF8()

@jscher2000
Copy link
Author

Try/catch added for Firefox 125 (currently in Nightly), at lines 18-20, 22.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment