From 5f1784f514d64082a9baf235df7816df173e482a Mon Sep 17 00:00:00 2001 From: Clemens Damke Date: Fri, 10 Jul 2015 00:45:56 +0200 Subject: [PATCH 1/7] added filename sanitizer filenames are now sanitized to be valid in *nix, os x and windows as well as FAT LFN compatible. + fixed a minor bug that would cause single file download tabs to stay open after a completed backup. --- source/comicReader.js | 34 +++++++++++++++++++++++----------- source/common.js | 15 +++++++++++++++ source/options.html | 7 +++++++ 3 files changed, 45 insertions(+), 11 deletions(-) diff --git a/source/comicReader.js b/source/comicReader.js index d117c4d..043e9ce 100644 --- a/source/comicReader.js +++ b/source/comicReader.js @@ -573,16 +573,21 @@ function loadComic(callback, step) { interval(); }, end = function() { dom.getCanvasContainer().parentElement.removeEventListener("DOMNodeRemoved", rmListener, false); - step("zip"); - zipImages(function() { - step("save"); - downloadBlob(getName()+"."+(settings.container?"zip":"cbz"), function() { - document.documentElement.removeChild(div); - document.documentElement.removeChild(overlay); - realClick(firstPageFig); - callback(); + function done() { + document.documentElement.removeChild(div); + document.documentElement.removeChild(overlay); + realClick(firstPageFig); + callback(); + } + if(settings.container == 2) + done(); + else { + step("zip"); + zipImages(function() { + step("save"); + downloadBlob(getName()+"."+(settings.container?"zip":"cbz"), done); }); - }); + } }, rmListener = function(e) { clearTimeout(noChangeTimeout); var container = dom.canvasContainer, @@ -619,8 +624,15 @@ function getName() { if(getName.title != null) return getName.title; var title = document.getElementsByTagName('title'); - if(title[0]) - return (getName.title = title[0].innerHTML.substr(0, title[0].innerHTML.lastIndexOf("-")).trim().replace(/\s/g, "_").replace(/[^a-z0-9#.()\[\]_-]/gi, "")); + if(title[0]) { + var spaceReplacement = { + 1: "_", + 2: ".", + 3: "-", + 4: "" + }[settings.filename]; + return (getName.title = sanitizeFilename(title[0].innerHTML.substr(0, title[0].innerHTML.lastIndexOf("-")).trim(), spaceReplacement) || "comic"); + } return "comic"; } getName.title = null; diff --git a/source/common.js b/source/common.js index 4b9dbc3..c967862 100644 --- a/source/common.js +++ b/source/common.js @@ -18,6 +18,21 @@ function randomString(min, max) { // generates random alphanumeric string with a return r; } +// Cleans up a string to be a valid cross platform filename. +// Inspired by https://github.com/parshap/node-sanitize-filename. +function sanitizeFilename(input, spaceReplacement) { + var result = input + .replace(/\s?[\/\\\|\?<>:\*\":]\s?/g, " ") + .replace(/[\x00-\x1f\x80-\x9f]/g, "") + .replace(/(\.|\s)+$/g, "") + .replace(/^(con|prn|aux|nul|com[0-9]|lpt[0-9])(\..*)?$/i, ""); + + if(spaceReplacement != null) + result = result.replace(/\s/g, spaceReplacement); + + return result; +} + function nullFill(num, len) { num += ""; while(num.length < len) diff --git a/source/options.html b/source/options.html index 85bc428..e540d70 100644 --- a/source/options.html +++ b/source/options.html @@ -144,6 +144,13 @@

Backup behaviour


+
Filename convention:

Max. parallel downloads per tab: