Skip to content

Commit

Permalink
fixes #44 #45
Browse files Browse the repository at this point in the history
  • Loading branch information
buggyj committed Aug 6, 2019
1 parent 73083f4 commit 0f9293b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
44 changes: 31 additions & 13 deletions extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var tiddlywikilocations = "tiddlywikilocations";
var $ = {"/":"/"};

var testfilecontent = "This is a test file for savetiddlers extension";
var probBlob = new Blob([testfilecontent], {type: 'text/plain'});
var probBlobUrl = URL.createObjectURL(probBlob);

function datesArray(now,andHours,andMinutes)
{
Expand Down Expand Up @@ -42,16 +44,21 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
console.log("savetiddlersbg: got request");


function dodownload (msg,tiddlywikilocations){
function dodownload (msg,tiddlywikilocations){
var objUrl = URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'}));
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
url: objUrl,
filename: tiddlywikilocations+$["/"]+ msg.path,
conflictAction: 'overwrite'
},
function(id) {
function(id) {chrome.downloads.onChanged.addListener(function hearchange(deltas){
// wait for completion
if (deltas.id == id && deltas.state && deltas.state.current === "complete") {
chrome.downloads.onChanged.removeListener(hearchange);
console.log("savetiddlers: saved "+msg.path);
URL.revokeObjectURL(objUrl);
chrome.storage.local.get({backuptw5:true,backuptwc:false,periodchoice:"day",period:[],backupdir:"backupdir",backedup:{}}, function(items) {
var newvals={}, newdate = new Date(),
var newvals={}, newdate = new Date(), objUrl2,
date = datesArray(newdate,items.periodchoice == "hour",minutebacks),
bkdate = newdate.toISOString().slice(0,10);
if ((msg.tw5 && items.backuptw5 === false) || (!msg.tw5 && items.backuptwc === false)) {
Expand All @@ -73,15 +80,22 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
}
// remember we backedup on this filepath
newvals.backedup[msg.path] = true;
objUrlBkup = URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'}));
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
url: objUrlBkup,
filename: tiddlywikilocations+$["/"]+items.backupdir+$["/"]+msg.path.replace(new RegExp('.{' + msg.path.lastIndexOf(".") + '}'), '$&' + bkdate),
conflictAction: 'overwrite'
},function(id){sendResponse({status:"backupsaved"});});
},function(id){chrome.downloads.onChanged.addListener(function hearchange(deltas){
// wait for completion
if (deltas.id == id && deltas.state && deltas.state.current === "complete") {
chrome.downloads.onChanged.removeListener(hearchange);
URL.revokeObjectURL(objUrlBkup);
sendResponse({status:"backupsaved"});
}})});
console.log("savetiddlersbg: backedup "+msg.path);
chrome.storage.local.set(newvals);
});
});
}})});
}

////////////////////////// start ///////////////////////////////
Expand Down Expand Up @@ -115,7 +129,7 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
// 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);
chrome.downloads.download({
url: URL.createObjectURL(new Blob([testfilecontent], {type: 'text/plain'})),
url: probBlobUrl,
filename: testbase+round+'.html',
conflictAction: 'overwrite'
},function(id){chrome.downloads.onChanged.addListener(function hearchange(deltas){
Expand Down Expand Up @@ -147,27 +161,31 @@ chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
});
return true;
} else {
console.log("savetiddlersbg: start finish");
console.log("savetiddlersbg: start save the file manually");
var path = msg.filePath.split($["/"]);
path = path[path.length-1];
var objUrl = URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'}));
chrome.downloads.download({
url: URL.createObjectURL(new Blob([msg.txt], {type: 'text/plain'})),
url: objUrl,
filename: tiddlywikilocations+$["/"]+path,
saveAs : true
},function(id){
if (id === undefined) {
sendResponse({status:"cancelled"});
console.log("savetiddlersbg: sent cancelled");
} else {
chrome.downloads.onChanged.addListener(function hearchange2(deltas){
chrome.downloads.onChanged.addListener(function hearchange(deltas){
if (deltas.id == id && deltas.state && deltas.state.current === "interrupted") {
sendResponse({status:"cancelled"});
console.log("savetiddlersbg: sent cancelled");
chrome.downloads.onChanged.removeListener(hearchange2);return true;
chrome.downloads.onChanged.removeListener(hearchange);
URL.revokeObjectURL(objUrl);
return true;
}
// wait for completion
if (deltas.id == id && deltas.state && deltas.state.current === "complete") {
chrome.downloads.onChanged.removeListener(hearchange2);
chrome.downloads.onChanged.removeListener(hearchange);
URL.revokeObjectURL(objUrl);
console.log("savetiddlersbg: finishing manual save");
chrome.downloads.search({id:id}, function(x){
var bodyy = msg.filePath, bodyx = x[0].filename.split($["/"]+testbase)[0];
Expand Down
8 changes: 4 additions & 4 deletions extension/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ function isTiddlyWikiClassic(doc) {
var debouncing =[];

function injectMessageBox(doc) {
var s;
var s, savetiddlers = "savetiddlers";
doc = document;
if (isTiddlyWikiClassic(doc)) {
s = document.createElement('script');
Expand All @@ -70,17 +70,17 @@ function injectMessageBox(doc) {
var messageBox = doc.getElementById("tiddlyfox-message-box");
if(messageBox) {
var othersw = messageBox.getAttribute("data-message-box-creator")|| null;
if (othersw) {
if (othersw && (othersw !== savetiddlers)) {
alert (othersaver1+othersw+othersaver2);
return;
} else {
messageBox.setAttribute("data-message-box-creator","savetiddlers");
messageBox.setAttribute("data-message-box-creator",savetiddlers);
}
} else {
messageBox = doc.createElement("div");
messageBox.id = "tiddlyfox-message-box";
messageBox.style.display = "none";
messageBox.setAttribute("data-message-box-creator","savetiddlers");
messageBox.setAttribute("data-message-box-creator",savetiddlers);
doc.body.appendChild(messageBox);
}
// Attach the event handler to the message box
Expand Down
2 changes: 1 addition & 1 deletion extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Save Tiddlers",
"version": "0.7",
"version": "0.9",
"manifest_version": 2,
"applications": {
"gecko": {
Expand Down

0 comments on commit 0f9293b

Please sign in to comment.