diff --git a/tiddlyfox/extension/content/inject.js b/tiddlyfox/extension/content/inject.js new file mode 100644 index 000000000..caa732290 --- /dev/null +++ b/tiddlyfox/extension/content/inject.js @@ -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; + +})(); \ No newline at end of file diff --git a/tiddlyfox/extension/content/overlay.js b/tiddlyfox/extension/content/overlay.js index 579e06383..d9d0bb4fe 100644 --- a/tiddlyfox/extension/content/overlay.js +++ b/tiddlyfox/extension/content/overlay.js @@ -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); diff --git a/tiddlyfox/extension/content/tiddlyfox_32x32.png b/tiddlyfox/extension/content/tiddlyfox_32x32.png index 8ed46bd7e..058cbf71a 100644 Binary files a/tiddlyfox/extension/content/tiddlyfox_32x32.png and b/tiddlyfox/extension/content/tiddlyfox_32x32.png differ diff --git a/tiddlyfox/extension/tiddlyfox.xpi b/tiddlyfox/extension/tiddlyfox.xpi index b84c2103c..377b133b3 100644 Binary files a/tiddlyfox/extension/tiddlyfox.xpi and b/tiddlyfox/extension/tiddlyfox.xpi differ