1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-12-25 17:40:29 +00:00

Basic, dumb saving in TiddlyFox now works

It seems to work OK, but could do with more of a user interface for
confirmations and so on...
This commit is contained in:
Jeremy Ruston 2012-08-27 21:48:57 +01:00
parent 3cc38ba1b2
commit 2a56c043d7
4 changed files with 138 additions and 9 deletions

View File

@ -0,0 +1,45 @@
/*
The JavaScript in this file is injected into each TiddlyWiki page that loads
*/
(function () {
/*
Returns true if successful, false if failed, null if not available
*/
var injectedSaveFile = function(path,content) {
// Find the message box element
var messageBox = document.getElementById("tiddlyfox-message-box");
if(messageBox) {
// Create the message element and put it in the message box
var message = document.createElement("div");
message.setAttribute("tiddlyfox-path",path);
message.setAttribute("tiddlyfox-content",content);
messageBox.appendChild(message);
// Create and dispatch the custom event to the extension
var event = document.createEvent("Events");
event.initEvent("tiddlyfox-save-file",true,false);
message.dispatchEvent(event);
}
return true;
};
/*
Returns text if successful, false if failed, null if not available
*/
var injectedLoadFile = function(path) {
try {
// Just the read the file synchronously
var xhReq = new XMLHttpRequest();
xhReq.open("GET", "file://" + path, false);
xhReq.send(null);
return xhReq.responseText;
} catch(ex) {
return false;
}
};
window.mozillaSaveFile = injectedSaveFile;
window.mozillaLoadFile = injectedLoadFile;
})();

View File

@ -1,12 +1,96 @@
var TiddlyFox = {
onLoad: function() {
// initialization code
this.initialized = true;
},
/*
The JavaScript code in this file is executed via `overlay.xul` when Firefox starts up.
*/
var TiddlyFox = {
// Called when the main browser has loaded
onLoad: function(event) {
// Register a page load event
var appcontent = document.getElementById("appcontent");
if(appcontent){
appcontent.addEventListener("DOMContentLoaded",TiddlyFox.onPageLoad,true);
}
},
// Called each time a page loads
onPageLoad: function(event) {
// Get the document and window
var doc = event.originalTarget,
win = doc.defaultView;
// If it is a TiddlyWiki
if(TiddlyFox.isTiddlyWiki(doc,win)) {
alert("TiddlyFox: Enabling TiddlyWiki file saving");
TiddlyFox.injectPage(doc);
}
},
injectPage: function(doc) {
// Inject the message box
var messageBox = doc.createElement("div");
messageBox.id = "tiddlyfox-message-box";
messageBox.style.display = "none";
doc.body.appendChild(messageBox);
// Attach the event handler to the message box
messageBox.addEventListener("tiddlyfox-save-file",TiddlyFox.onSaveFile,false);
// Load the script text
var xhReq = new XMLHttpRequest();
xhReq.open("GET","chrome://tiddlyfox/content/inject.js",false);
xhReq.send(null);
var injectCode = xhReq.responseText;
// Inject the script
var code = doc.createTextNode(injectCode);
var scr = doc.createElement("script");
scr.type = "text/javascript";
scr.appendChild(code);
doc.getElementsByTagName("head")[0].appendChild(scr)
},
saveFile: function(filePath,content) {
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(filePath);
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);
out.write(content,content.length);
out.flush();
out.close();
return true;
} catch(ex) {
return false;
}
},
onSaveFile: function(event) {
// Get the details from the message
var message = event.target,
path = message.getAttribute("tiddlyfox-path"),
content = message.getAttribute("tiddlyfox-content");
// Save the file
TiddlyFox.saveFile(path,content);
// Remove the message element from the message box
message.parentNode.removeChild(message);
return false;
},
// Called via `overlay.xul` when the menu item is selected
onMenuItemCommand: function() {
window.open("chrome://tiddlyfox/content/hello.xul", "", "chrome");
},
isTiddlyWiki: function(doc,win) {
// Test whether the document is a TiddlyWiki (we don't have access to JS objects in it)
return (doc.scripts[0].id === "versionArea");
}
onMenuItemCommand: function() {
window.open("chrome://tiddlyfox/content/hello.xul", "", "chrome");
}
};
window.addEventListener("load", function(e) { TiddlyFox.onLoad(e); }, false);
window.addEventListener("load",function load(event) {
window.removeEventListener("load",load,false);
TiddlyFox.onLoad(event);
},false);

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

After

Width:  |  Height:  |  Size: 3.6 KiB

Binary file not shown.