mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-02-02 12:19:11 +00:00
Added HTTP upload saver
Now we can save changes to TiddlySpot
This commit is contained in:
parent
b4a75b25fc
commit
5e11de719c
@ -42,15 +42,15 @@ DownloadSaver.prototype.info = {
|
||||
/*
|
||||
Static method that returns true if this saver is capable of working
|
||||
*/
|
||||
exports.canSave = function() {
|
||||
exports.canSave = function(wiki) {
|
||||
return true;
|
||||
};
|
||||
|
||||
/*
|
||||
Create an instance of this saver
|
||||
*/
|
||||
exports.create = function() {
|
||||
return new DownloadSaver();
|
||||
exports.create = function(wiki) {
|
||||
return new DownloadSaver(wiki);
|
||||
};
|
||||
|
||||
})();
|
||||
|
@ -52,15 +52,15 @@ FirefoxSaver.prototype.info = {
|
||||
/*
|
||||
Static method that returns true if this saver is capable of working
|
||||
*/
|
||||
exports.canSave = function() {
|
||||
exports.canSave = function(wiki) {
|
||||
return window.location.protocol === "file:" && !!window.Components;
|
||||
};
|
||||
|
||||
/*
|
||||
Create an instance of this saver
|
||||
*/
|
||||
exports.create = function() {
|
||||
return new FirefoxSaver();
|
||||
exports.create = function(wiki) {
|
||||
return new FirefoxSaver(wiki);
|
||||
};
|
||||
|
||||
})();
|
||||
|
81
core/modules/savers/upload.js
Normal file
81
core/modules/savers/upload.js
Normal file
@ -0,0 +1,81 @@
|
||||
/*\
|
||||
title: $:/core/modules/savers/upload.js
|
||||
type: application/javascript
|
||||
module-type: saver
|
||||
|
||||
Handles saving changes via upload to a server
|
||||
|
||||
\*/
|
||||
(function(){
|
||||
|
||||
/*jslint node: true, browser: true */
|
||||
/*global $tw: false */
|
||||
"use strict";
|
||||
|
||||
/*
|
||||
Select the appropriate saver module and set it up
|
||||
*/
|
||||
var UploadSaver = function(wiki) {
|
||||
this.wiki = wiki;
|
||||
};
|
||||
|
||||
UploadSaver.prototype.save = function(text) {
|
||||
// Get the various parameters we need
|
||||
var backupDir = ".",
|
||||
userName = this.wiki.getTextReference("$:/UploadName"),
|
||||
password = $tw.utils.getPassword("upload"),
|
||||
uploadDir = ".",
|
||||
url = this.wiki.getTextReference("$:/UploadURL");
|
||||
if(!url) {
|
||||
url = "http://" + userName + ".tiddlyspot.com/store.cgi";
|
||||
}
|
||||
// Assemble the header
|
||||
var boundary = "---------------------------" + "AaB03x";
|
||||
var uploadFormName = "UploadPlugin";
|
||||
var head = [];
|
||||
head.push("--" + boundary + "\r\nContent-disposition: form-data; name=\"UploadPlugin\"\r\n");
|
||||
head.push("backupDir=" + backupDir + ";user=" + userName + ";password=" + password + ";uploaddir=" + uploadDir + ";;");
|
||||
head.push("\r\n" + "--" + boundary);
|
||||
head.push("Content-disposition: form-data; name=\"userfile\"; filename=\"index.html\"");
|
||||
head.push("Content-Type: text/html;charset=UTF-8");
|
||||
head.push("Content-Length: " + text.length + "\r\n");
|
||||
head.push("");
|
||||
// Assemble the tail and the data itself
|
||||
var tail = "\r\n--" + boundary + "--\r\n",
|
||||
data = head.join("\r\n") + text + tail;
|
||||
// Do the HTTP post
|
||||
var http = new XMLHttpRequest();
|
||||
http.open("POST",url,true,userName,password);
|
||||
http.setRequestHeader("Content-Type","multipart/form-data; ;charset=UTF-8; boundary=" + boundary);
|
||||
http.onreadystatechange = function() {
|
||||
if(http.readyState == 4 && http.status == 200) {
|
||||
alert(http.responseText);
|
||||
}
|
||||
}
|
||||
http.send(data);
|
||||
return true;
|
||||
};
|
||||
|
||||
/*
|
||||
Information about this saver
|
||||
*/
|
||||
UploadSaver.prototype.info = {
|
||||
name: "upload",
|
||||
priority: 500
|
||||
};
|
||||
|
||||
/*
|
||||
Static method that returns true if this saver is capable of working
|
||||
*/
|
||||
exports.canSave = function(wiki) {
|
||||
return wiki.tiddlerExists("$:/UploadName");
|
||||
};
|
||||
|
||||
/*
|
||||
Create an instance of this saver
|
||||
*/
|
||||
exports.create = function(wiki) {
|
||||
return new UploadSaver(wiki);
|
||||
};
|
||||
|
||||
})();
|
@ -487,7 +487,7 @@ exports.initSavers = function(moduleType) {
|
||||
this.savers = [];
|
||||
for(var t=0; t<$tw.plugins.moduleTypes[moduleType].length; t++) {
|
||||
var saver = $tw.plugins.moduleTypes[moduleType][t];
|
||||
if(saver.canSave()) {
|
||||
if(saver.canSave(this)) {
|
||||
this.savers.push(saver.create(this));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user