aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2018-04-16 13:57:05 +0200
committerRahiel Kasim <rahielkasim@gmail.com>2018-04-16 13:57:05 +0200
commit3bbe47bd062b2f071a5a98ca8c69c9cd3563308b (patch)
tree05768b34a35b119d20a7a668fcf86d180f5e1a41
parent7f0e51c43c3727a714edd39100001ceb93e5c20d (diff)
refactoring: move some functions to utils
-rw-r--r--src/archiveror.js44
-rw-r--r--src/utils.js40
2 files changed, 43 insertions, 41 deletions
diff --git a/src/archiveror.js b/src/archiveror.js
index a388de5..fa0917c 100644
--- a/src/archiveror.js
+++ b/src/archiveror.js
@@ -1,4 +1,6 @@
-import { defaults, getArchivingURL, isLocal, services, hasPageCapture } from "./utils.js";
+import {
+ defaults, getArchivingURL, hasPageCapture, isLocal, makeFilename, sanitizeFilename, services, writeClipboard
+} from "./utils.js";
function archive_is(url) {
@@ -69,19 +71,6 @@ chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
}
});
-function writeClipboard(text) {
- // write text to clipboard
- let textarea = document.createElement("textarea");
- textarea.textContent = text;
- document.body.appendChild(textarea);
- let range = document.createRange();
- range.selectNode(textarea);
- window.getSelection().addRange(range);
- document.execCommand("copy");
- window.getSelection().removeAllRanges();
- document.body.removeChild(textarea);
-}
-
function showArchive(url, bookmark) {
// Notify user if we have an archive of the current page, otherwise archive if needed
let keys = {
@@ -196,33 +185,6 @@ function saveLocal(tab, automatic, path) {
}
}
-function sanitizeFilename(title) {
- // Chromium disallows <>:"/\|?*~ in filenames (raises an error in chrome.downloads.download)
- let name = title;
- let re = /[<>:"/\\|?*~]/g;
- name = title.replace(re, "_").trim();
- // Chromium disallows filenames starting with a .
- while (name.startsWith(".")) {
- name = name.slice(1);
- }
- return name;
-}
-
-function makeFilename(title) {
- return sanitizeFilename(title) + "_" + getTimestamp() + ".mhtml";
-}
-
-function getTimestamp() {
- let date = new Date();
- let y = date.getUTCFullYear();
- let m = (date.getUTCMonth() + 1).toString().padStart(2, "0");
- let d = date.getUTCDate().toString().padStart(2, "0");
- let H = date.getUTCHours().toString().padStart(2, "0");
- let M = date.getUTCMinutes().toString().padStart(2, "0");
- let timestamp = `${y}-${m}-${d}_${H}-${M}`;
- return timestamp;
-}
-
// Keyboard shortcuts
chrome.commands.onCommand.addListener(function (command) {
let action;
diff --git a/src/utils.js b/src/utils.js
index 21eb177..021159a 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -36,3 +36,43 @@ export function getArchivingURL(page, service, email) {
}
return url;
}
+
+export function sanitizeFilename(title) {
+ // Chromium disallows <>:"/\|?*~ in filenames (raises an error in chrome.downloads.download)
+ let name = title;
+ let re = /[<>:"/\\|?*~]/g;
+ name = title.replace(re, "_").trim();
+ // Chromium disallows filenames starting with a .
+ while (name.startsWith(".")) {
+ name = name.slice(1);
+ }
+ return name;
+}
+
+export function makeFilename(title) {
+ return sanitizeFilename(title) + "_" + getTimestamp() + ".mhtml";
+}
+
+function getTimestamp() {
+ let date = new Date();
+ let y = date.getUTCFullYear();
+ let m = (date.getUTCMonth() + 1).toString().padStart(2, "0");
+ let d = date.getUTCDate().toString().padStart(2, "0");
+ let H = date.getUTCHours().toString().padStart(2, "0");
+ let M = date.getUTCMinutes().toString().padStart(2, "0");
+ let timestamp = `${y}-${m}-${d}_${H}-${M}`;
+ return timestamp;
+}
+
+export function writeClipboard(text) {
+ // write text to clipboard
+ let textarea = document.createElement("textarea");
+ textarea.textContent = text;
+ document.body.appendChild(textarea);
+ let range = document.createRange();
+ range.selectNode(textarea);
+ window.getSelection().addRange(range);
+ document.execCommand("copy");
+ window.getSelection().removeAllRanges();
+ document.body.removeChild(textarea);
+}