Skip to content

Instantly share code, notes, and snippets.

@Arlen22
Created January 20, 2015 01:13
Show Gist options
  • Save Arlen22/3ff06f8a2f9f7e6b740c to your computer and use it in GitHub Desktop.
Save Arlen22/3ff06f8a2f9f7e6b740c to your computer and use it in GitHub Desktop.
/*\
title: NodeWebKitSaver.js
type: application/javascript
module-type: saver
Handles saving changes via node.js in the browser (node-webkit).
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false, netscape: false, Components: false */
"use strict";
var NodeWebKitSaver = function(wiki) {
try{
this.fs = require("fs");
} catch (exc) {
//console.log(exc);
this.fs = null;
}
};
NodeWebKitSaver.prototype.save = function(text,method,callback) {
//var messageBox = document.getElementById("tiddlyfox-message-box");
//console.log(process.version);
if(this.fs) {
// Get the pathname of this document
var pathname = document.location.toString().split("#")[0];
// Replace file://localhost/ with file:///
if(pathname.indexOf("file://localhost/") === 0) {
pathname = "file://" + pathname.substr(16);
}
// Windows path file:///x:/blah/blah --> x:\blah\blah
if(/^file\:\/\/\/[A-Z]\:\//i.test(pathname)) {
// Remove the leading slash and convert slashes to backslashes
pathname = pathname.substr(8).replace(/\//g,"\\");
// Firefox Windows network path file://///server/share/blah/blah --> //server/share/blah/blah
} else if(pathname.indexOf("file://///") === 0) {
pathname = "\\\\" + unescape(pathname.substr(10)).replace(/\//g,"\\");
// Mac/Unix local path file:///path/path --> /path/path
} else if(pathname.indexOf("file:///") === 0) {
pathname = unescape(pathname.substr(7));
// Mac/Unix local path file:/path/path --> /path/path
} else if(pathname.indexOf("file:/") === 0) {
pathname = unescape(pathname.substr(5));
// Otherwise Windows networth path file://server/share/path/path --> \\server\share\path\path
} else {
pathname = "\\\\" + unescape(pathname.substr(7)).replace(new RegExp("/","g"),"\\");
}
this.fs.writeFile(decodeURIComponent(pathname), text, function (err) {
//if (err) throw err;
//console.log('NodeWebKitSaver', err);
callback(err);
});
return true;
} else {
return false;
}
};
/*
Information about this saver
*/
NodeWebKitSaver.prototype.info = {
name: "nodewebkit",
priority: 1500,
capabilities: ["save", "autosave"]
};
/*
Static method that returns true if this saver is capable of working.
It will only work with the 'file' protocol and only if node.js is available.
*/
exports.canSave = function(wiki) {
//console.log(window.location.protocol);
return (window.location.protocol === "file:");
};
/*
Create an instance of this saver
*/
exports.create = function(wiki) {
return new NodeWebKitSaver(wiki);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment