Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Infocatcher
Created December 20, 2012 08:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Infocatcher/4343743 to your computer and use it in GitHub Desktop.
Save Infocatcher/4343743 to your computer and use it in GitHub Desktop.
Repair Custom Buttons after save in Nightly with javascript.options.xml.chrome = false http://custombuttons.sf.net/forum/viewtopic.php?p=2905#p2905
// http://custombuttons.sf.net/forum/viewtopic.php?p=2905#p2905
// (c) Infocatcher 2012
// version 0.1.0 - 2012-12-20
// Repair Custom Buttons after save in Nightly with javascript.options.xml.chrome = false
// Use at your own risk!
// Backup your %profile%/custombuttons directory first!
Components.utils.import("resource://gre/modules/NetUtil.jsm");
Components.utils.import("resource://gre/modules/FileUtils.jsm");
var cbDir = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties)
.get("ProfD", Components.interfaces.nsIFile);
cbDir.append("custombuttons");
var entries = cbDir.directoryEntries;
while(entries.hasMoreElements()) {
var entry = entries.getNext().QueryInterface(Components.interfaces.nsIFile);
if(entry.isFile() && /^\w*buttonsoverlay\.xul$/.test(entry.leafName))
repairFile(entry);
}
function repairFile(file) {
NetUtil.asyncFetch(file, function(istream, status) {
if(!Components.isSuccessCode(status)) {
alert("NetUtil.asyncFetch() failed: " + getErrorName(status));
return;
}
try {
var data = NetUtil.readInputStreamToString(
istream,
istream.available(),
{ charset: "UTF-8", replacement: "\ufffd" }
);
}
catch(e) {
alert("NetUtil.readInputStreamToString() failed:\n" + e + "\nEmpty file?");
return;
}
var repaired = repairXML(data);
if(repaired == data)
return;
var bak = file.clone(), i = -1;
do
bak.leafName = file.leafName + ".repair" + (++i || "") + ".bak";
while(bak.exists());
file.copyTo(null, bak.leafName);
var ostream = FileUtils.openSafeFileOutputStream(file);
var suc = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
suc.charset = "UTF-8";
istream = suc.convertToInputStream(repaired);
NetUtil.asyncCopy(istream, ostream, (function(status) {
if(!Components.isSuccessCode(status)) {
alert("NetUtil.asyncCopy() failed: " + getErrorName(status));
return;
}
alert("Repaired: " + file.leafName + "\nBackup: " + bak.leafName);
}));
});
}
function repairXML(data) {
return data
.replace(/ xmlns=""/g, "")
.replace(/="[^"]+"/g, function(s) {
return s.replace(/[\x00-\x19]/g, function(chr) {
return "&#x" + chr.charCodeAt(0).toString(16).toUpperCase() + ";";
});
});
}
function getErrorName(code) {
var cr = Components.results;
for(var errName in cr)
if(cr[errName] == code)
return errName;
return code;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment