HTTPPost.js
Summary
This module is used to save documents via HTTP POST. You must have a server which is configured to accept the POST data and take action. Examples for catching the POST data are provided in PHP and JSP format. The PHP examples include tests for handling saving errors.
To configure Mozile to use HTTPPost for saving, add the following line to the "modules" array in your mozile.js configuration file: "HTTPPost". You can add more options, for example: "HTTPPost: url='http://somewhere.com/save.php', default=true".
Version: 0.7.0
Author: James A. Overton
mozile.getModule("HTTPPost").init = function() {
mozile.addSaveModule(mozile.getModule("HTTPPost"));
mozile.getModule("HTTPPost").setOption("label", "Save via HTTP POST");
mozile.getModule("HTTPPost").setOption("showURL", true);
if(!mozile.getModule("HTTPPost").getOption("url")) {
mozile.getModule("HTTPPost").setOption("url", "http://localhost");
}
if(mozile.getModule("HTTPPost").getOption("default")) {
mozile.setDefaultSaveModule(mozile.getSaveModule("HTTPPost"));
}
mozile.getModule("HTTPPost").getOption("location", "automatic");
mozile.getModule("HTTPPost").getOption("display", "automatic");
}
mozile.getModule("HTTPPost").getInterface = function() {
if(!this._interface) {
this._interface = new MozileInterface(mozile.getModule("HTTPPost").getPath() +"savemsg.xml", "HTTPPostInterface: width=450px");
this._interface.init = function() {
var response = document.getElementById("HTTPPostResponseText");
var statustext = document.getElementById("HTTPPostStatusText");
while(statustext.childNodes.length) statustext.removeChild(statustext.firstChild);
if(mozileXHR.responseText) response.value = mozileXHR.responseText;
if(this.getOption("statustext")) statustext.appendChild(document.createTextNode(this.getOption("statustext")));
else statustext.appendChild(document.createTextNode("Status Unknown"));
}
this._interface.update = function() {
this.reposition();
var abortButton = document.getElementById("HTTPPostAbortButton");
if(mozileXHR.readyState == 0 || mozileXHR.readyState == 4) abortButton.collapsed = true;
else abortButton.collapsed = false;
}
}
return this._interface;
}
var mozileXHR = new XMLHttpRequest();
mozile.getModule("HTTPPost").save = function() {
var f = new Array();
f["File"] = "HTTPPost/HTTPPost.js";
f["Function"] = "save";
var httpSavePath = mozile.getSaveOption("current", "url");
if(mozile.getSaveOption("current", "content") == "editor" &&
mozile.getCurrentEditor().id) {
if(httpSavePath.indexOf('?') != -1){
httpSavePath = httpSavePath + "&MozileID=" + mozile.getCurrentEditor().id;
}else{
httpSavePath = httpSavePath + "?MozileID=" + mozile.getCurrentEditor().id;
}
}
var contentType = "text/plain";
if (mozile.getSaveOption("current", "contentType")) contentType = mozile.getSaveOption("current", "contentType");
else if(mozile.getSaveOption("current", "content") == "document") contentType = document.contentType;
const CR = '\x0D';
const LF = '\x0A';
var content = CR + LF + mozile.content() + CR + LF;
this.getInterface().setOption("statustext", undefined);
mozile.status(f,3,"Begining save to '"+ mozile.getSaveOption("current", "url") +"'", 0, "mozile.getModule('HTTPPost').getInterface().show()");
try {
mozileXHR.abort();
mozileXHR.open("POST", httpSavePath, true, null, null);
mozileXHR.setRequestHeader('Content-Type', contentType + "; " + document.characterSet);
if(mozile.getMozillaVersion().indexOf("1.7") > 0) mozileXHR.setRequestHeader('Content-Length', content.length);
mozileXHR.setRequestHeader('Content-Location', mozile.getSaveOption("current", "url"));
mozileXHR.onerror = this._onerror;
mozileXHR.onreadystatechange = this._onchange;
mozileXHR.onload = this._onload;
mozileXHR.send(content);
mozile.debug(f,1,"POST sent");
}catch (e){
mozile.debug(f,4,"Exception: "+e);
mozile.status(f,3,"Save Failed! Permission denied to save to URL \"" + mozile.getSaveOption("current", "url") +"\"");
}
}
mozile.getModule("HTTPPost").abort = function() {
var f = new Array();
f["File"] = "HTTPPost/HTTPPost.js";
f["Function"] = "abort";
mozileXHR.abort();
mozile.status(f,3,"Save aborted by user.");
}
mozile.getModule("HTTPPost")._onerror = function() {
var f = new Array();
f["File"] = "HTTPPost/HTTPPost.js";
f["Function"] = "onerror";
mozile.status(f,3,"Save Failed! There was an error in the HTTP POST.");
}
mozile.getModule("HTTPPost")._onchange = function() {
var f = new Array();
f["File"] = "HTTPPost/HTTPPost.js";
f["Function"] = "onchange";
var message = "Save via HTTP POST: ";
switch ( mozileXHR.readyState ) {
case 1 :
mozile.status(f,3, message+"1. Initialising...", 10, "mozile.getModule('HTTPPost').getInterface().show()");
break;
case 2 :
mozile.status(f,3, message+"2. Sending...", 50, "mozile.getModule('HTTPPost').getInterface().show()");
break;
case 3 :
mozile.status(f,3, message+"3. Receiving acknowledgement...", 90, "mozile.getModule('HTTPPost').getInterface().show()");
break;
case 4 :
break;
}
}
mozile.getModule("HTTPPost")._onload = function() {
var f = new Array();
f["File"] = "HTTPPost/HTTPPost.js";
f["Function"] = "onload";
if( mozileXHR.responseXML &&
(mozileXHR.status == 200 || mozileXHR.status == 201 ) ) {
var root = mozileXHR.responseXML.getElementsByTagName("mozile")[0];
if(root) {
var status = root.getElementsByTagName("status")[0];
var statustext = root.getElementsByTagName("statustext")[0];
var location = root.getElementsByTagName("location")[0];
var display = root.getElementsByTagName("display")[0];
if(status && status.firstChild && status.firstChild.textContent &&
statustext && statustext.firstChild && statustext.firstChild.textContent) {
mozile.getModule("HTTPPost").getInterface().setOption("statustext", statustext.firstChild.textContent);
if(status.firstChild.textContent == "1") mozile.status(f,3,"Save Complete. "+ statustext.firstChild.textContent, 100, "mozile.getModule('HTTPPost').getInterface().show()");
else mozile.status(f,3,"Save Failed! Server error: "+ statustext.firstChild.textContent, 0, "mozile.getModule('HTTPPost').getInterface().show()");
if(mozile.getModule("HTTPPost").getOption("location")=="automatic" &&
location && location.firstChild && location.firstChild.textContent) {
if(location.getAttribute("replace") &&
location.getAttribute("replace").toLowerCase() == "true")
document.location = location.firstChild.textContent;
else window.open(location.firstChild.textContent);
}
if(mozile.getModule("HTTPPost").getOption("display")=="automatic" &&
display && display.firstChild && display.firstChild.textContent=="yes") mozile.getModule("HTTPPost").getInterface().show();
}
else mozile.status(f,3, "Save Failed! Invalid server response.", 0, "mozile.getModule('HTTPPost').getInterface().show()");
}
}
else if (mozileXHR.status == 204) {
mozile.status(f,3,"Save Complete.", 100, "mozile.getModule('HTTPPost').getInterface().show()");
}
else if (mozileXHR.status == 404) {
mozile.status(f,3,"Save failed! Error 404 'File not found'.", 0, "mozile.getModule('HTTPPost').getInterface().show()");
mozile.getModule("HTTPPost").getInterface().setOption("statustext", "HTTP Response Code "+mozileXHR.status);
}
else {
mozile.status(f,3, "Save Failed! Unexpected server response.", 0, "mozile.getModule('HTTPPost').getInterface().show()");
mozile.getModule("HTTPPost").getInterface().setOption("statustext", "HTTP Response Code "+mozileXHR.status);
}
if(mozile.getModule("HTTPPost").getOption("display")=="always") mozile.getModule("HTTPPost").getInterface().show();
}
Documentation generated by
JSDoc on Wed Nov 1 15:37:30 2006