Skip to content

Commit

Permalink
overriding with setting from the tiddlywiki #18
Browse files Browse the repository at this point in the history
  • Loading branch information
buggyj committed Nov 9, 2017
1 parent 2eeeae5 commit 6349796
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 27 deletions.
30 changes: 18 additions & 12 deletions extension/ContentScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,31 @@ function injectMessageBox(doc) {
// Attach the event handler to the message box
messageBox.addEventListener("tiddlyfox-save-file",function(event) {
// Get the details from the message
var message = event.target,
path = message.getAttribute("data-tiddlyfox-path"),
content = message.getAttribute("data-tiddlyfox-content");
var message = event.target,msg = {};
msg.path = message.getAttribute("data-tiddlyfox-path");
msg.content = message.getAttribute("data-tiddlyfox-content");
msg.base = message.getAttribute("data-tiddlyfox-base");
msg.backupdir = message.getAttribute("data-tiddlyfox-backupdir");
msg.backuppath = message.getAttribute("data-tiddlyfox-backuppath");
// Remove the message element from the message box
message.parentNode.removeChild(message);
// Save the file
saveFile(path,content,function(response) {
saveFile(msg,function(response) {
// Send a confirmation message
var event1;
console.log ("savetiddlers: response is "+response);
if (response === "failedloc" || response === "failedpath" ) {
alert(" TW not in tiddlywikilocations within the download directory.\n using default download directory");
finishSave(path,content, function(response){
alert(" TW not in "+ (base?base:"tiddlywikilocations") +" within the download directory.\n using default download directory");
finishSave(msg.path,msg.content, function(response){
event1 =doc.createEvent("Events");
event1.initEvent("tiddlyfox-have-saved-file",true,false);
event1.savedFilePath = path;
event1.savedFilePath = msg.path;
message.dispatchEvent(event1);
})
} else {
event1 =doc.createEvent("Events");
event1.initEvent("tiddlyfox-have-saved-file",true,false);
event1.savedFilePath = path;
event1.savedFilePath = msg.path;
message.dispatchEvent(event1);
}
});
Expand All @@ -72,14 +75,17 @@ function injectMessageBox(doc) {
},false);
}

function saveFile(filePath,content,callback) {
function saveFile(messg,callback) {

// Save the file
try {
var msg = {};
msg.filePath = filePath;
msg.txt = content;
msg.type = "start"
msg.filePath = messg.path;
msg.txt = messg.content;
msg.type = "start";
msg.locations = messg.base;
msg.backupdir = messg.backupdir;
msg.backuppath = messg.backuppath;
console.log("from cs: we are inside downloads at "+msg.filePath);
chrome.runtime.sendMessage(msg,callback);
return true;
Expand Down
40 changes: 25 additions & 15 deletions extension/background.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
var testmode = false; //set to true to avoid path test
var minutebacks = false; //set to true to allow backs every minute for testing

var tiddlywikilocations = "tiddlywikilocations";
var $ = {"/":"/"};

var testfilecontent = "This is a test file for savetiddlers extension";

function datesArray(now,andHours,andMinutes)
Expand All @@ -30,25 +27,35 @@ function equalDateArrays(Ar1,Ar2) {

chrome.runtime.getPlatformInfo( function(info) {if(info.os == "win") { $["/"] = "\\"; }

var testbase = 'tiddlywikilocations'+$["/"]+'readTiddlySaverInstruction';
var round = '59723833'; //by rotating this string of digits we can have 8 unique named test files for simutaneous use
//ie testpath = testbase+round+'.html';rotate(round) for next test file
var rlen = round.length - 1;


var defaultlocations = "tiddlywikilocations";
var $ = {"/":"/"};

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
console.log("savetiddlers: background got message.twdl");

function dodownload (msg){
function dodownload (msg, tiddlywikilocations){
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
filename: 'tiddlywikilocations'+$["/"]+ msg.path,
filename: tiddlywikilocations+$["/"]+ msg.path,
conflictAction: 'overwrite'
},
function(id) {
console.log("savetiddlers: saved "+msg.path);
chrome.storage.local.get({backup:false,periodchoice:"day",period:[],backupdir:"backupdir",backedup:{}}, function(items) {
if (msg.backuppath||msg.backupdir) {
var backupdir = msg.backupdir?msg.backupdir+$["/"]:"";
var backuppath =msg.backuppath?msg.backuppath:msg.path;
console.log("back control by tw");
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
filename: tiddlywikilocations+$["/"]+backupdir+backuppath,
conflictAction: 'overwrite'
}, function(id) {sendResponse("backupsaved");});
}
else chrome.storage.local.get({backup:false,periodchoice:"day",period:[],backupdir:"backupdir",backedup:{}}, function(items) {
var newvals={}, newdate = new Date(),
date = datesArray(newdate,items.periodchoice == "hour",minutebacks),
bkdate = newdate.toISOString().slice(0,10);
Expand All @@ -73,18 +80,21 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
newvals.backedup[msg.path] = true;
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
filename: 'tiddlywikilocations'+$["/"]+items.backupdir+$["/"]+msg.path.replace(new RegExp('.{' + msg.path.lastIndexOf(".") + '}'), '$&' + bkdate),
filename: tiddlywikilocations+$["/"]+items.backupdir+$["/"]+msg.path.replace(new RegExp('.{' + msg.path.lastIndexOf(".") + '}'), '$&' + bkdate),
conflictAction: 'overwrite'
},function(id){sendResponse("backupsaved");});
console.log("savetiddlers: backedup "+msg.path);
chrome.storage.local.set(newvals);
});
});
}
console.log("savetiddlers: background 2nd step");

////////////////////////// start ///////////////////////////////
if (msg.type === "start") {
var path, firstloc = msg.filePath.indexOf($["/"]+tiddlywikilocations+$["/"]);
var path,
tiddlywikilocations = msg.locations||defaultlocations,
firstloc = msg.filePath.indexOf($["/"]+tiddlywikilocations+$["/"]),
testbase = tiddlywikilocations+$["/"]+'readTiddlySaverInstruction';

msg.fPath = msg.filePath.substring(0, firstloc);
if (firstloc === -1) {
Expand All @@ -97,14 +107,14 @@ if (msg.type === "start") {
msg.path = msg.filePath.slice(firstloc+tiddlywikilocations.length + "//".length);
msg.twdl = true;
}
console.log("savetiddlers: background 3nd step");

// show the choose file dialogue when tw not under 'tiddlywikilocations'
if (!msg.twdl) {
console.log("savetiddlers: not in tiddlywikilocations "+msg.path);
console.log("savetiddlers: not in "+tiddlywikilocations+msg.path);
sendResponse("failedloc");
} else if (testmode) {
console.log("savetiddlers: avoid path testing");
dodownload(msg);//avoid path testing
dodownload(msg, tiddlywikilocations);//avoid path testing
} else{
// first download check our destination is valid by download a dummy file first and then reading back the filepath
round = round[rlen] + round.substring(0, rlen);
Expand All @@ -119,7 +129,7 @@ if (msg.type === "start") {
chrome.downloads.search({id:id}, function(x){
if (msg.fPath == x[0].filename.split($["/"]+testbase)[0]) {
// All tests passed!
dodownload(msg);
dodownload(msg,tiddlywikilocations);
} else {
console.log("savetiddlers: failed path "+msg.fPath +"!="+x[0].filename.split($["/"]+testbase)[0]);
sendResponse("failedpath");
Expand Down

0 comments on commit 6349796

Please sign in to comment.