2012-07-10 22:18:44 +00:00
|
|
|
/*\
|
|
|
|
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 */
|
2012-07-10 22:18:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
2012-07-12 15:58:49 +00:00
|
|
|
var FirefoxSaver = function(wiki) {
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2012-07-13 12:03:38 +00:00
|
|
|
exports.canSave = function(wiki) {
|
2012-07-12 12:11:58 +00:00
|
|
|
return window.location.protocol === "file:" && !!window.Components;
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Create an instance of this saver
|
|
|
|
*/
|
2012-07-13 12:03:38 +00:00
|
|
|
exports.create = function(wiki) {
|
|
|
|
return new FirefoxSaver(wiki);
|
2012-07-10 22:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
})();
|