1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 11:29:58 +00:00
TiddlyWiki5/core/modules/savers/firefox.js

67 lines
1.9 KiB
JavaScript
Raw Normal View History

/*\
title: $:/core/modules/savers/firefox.js
type: application/javascript
module-type: saver
Handles saving changes via Firefox's XUL APIs
\*/
(function(){
/*jslint node: true, browser: true */
2012-07-13 10:26:12 +00:00
/*global $tw: false, netscape: false, Components: false */
"use strict";
2012-07-12 15:58:49 +00:00
var FirefoxSaver = function(wiki) {
};
FirefoxSaver.prototype.save = function(text) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
// Generate the local file path from the file URI
var url = document.location.toString(),
ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService),
fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler),
fileSpec = fileHandler.getFileFromURLSpec(url);
// Try to save the file
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(fileSpec.path);
if(!file.exists()) {
file.create(0,0x01B4);// 0x01B4 = 0664
}
var out = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);
out.init(file,0x22,0x04,null);
var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);
converter.init(out, "UTF-8", 0, 0);
converter.writeString(text);
converter.close();
return true;
} catch(ex) {
return false;
}
};
/*
Information about this saver
*/
FirefoxSaver.prototype.info = {
name: "firefox",
priority: 1000
};
/*
Static method that returns true if this saver is capable of working
*/
exports.canSave = function(wiki) {
return window.location.protocol === "file:" && !!window.Components;
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new FirefoxSaver(wiki);
};
})();