Last active
December 18, 2024 16:10
Browser Console Snippet to decompress mozlz4 and jsonlz4 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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); | |
} | |
} | |
} | |
}); |
Updates for Firefox 104:
OS.Constants.Path.profileDir
toPathUtils.profileDir
OS.File.read()
toIOUtils.readUTF8()
OS.File.writeAtomic()
toIOUtils.writeUTF8()
Try/catch added for Firefox 125 (currently in Nightly), at lines 18-20, 22.
Commented out Line 34 which broke in Firefox 132 or earlier.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I created a pair of webapps that are simpler to use: