aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRahiel Kasim <rahielkasim@gmail.com>2018-12-01 12:50:23 +0100
committerRahiel Kasim <rahielkasim@gmail.com>2018-12-01 12:50:23 +0100
commit036a197258d3912b05686d68bcfcb143c42df42d (patch)
tree7b33d33ef6f7e1285a34e52ca159d298e58476f1
parentb50d7d0b1a5c99923fded8b2c53e486c363ac460 (diff)
add first 4 bytes of sha256 hash of MHTML archives to the filename
To have unique filenames when multiple pages with the same title are saved with the same timestamp (i.e. same minute)
-rw-r--r--src/archiveror.js42
-rw-r--r--src/manifest.json2
-rw-r--r--src/utils.js4
3 files changed, 36 insertions, 12 deletions
diff --git a/src/archiveror.js b/src/archiveror.js
index 480d874..cfe05a5 100644
--- a/src/archiveror.js
+++ b/src/archiveror.js
@@ -219,17 +219,41 @@ function saveLocal(tab, automatic, path) {
chrome.pageCapture.saveAsMHTML({tabId: tab.id}, blobToDisk);
function blobToDisk(mhtmlData) {
- let filename = makeFilename(tab.title);
- let url = URL.createObjectURL(mhtmlData);
- if (automatic === true) {
- silentDownload(url, filename, path, function (downloadId) {
- saveDownload(downloadId, tab.url);
- });
- } else {
- chrome.downloads.download({url: url, filename: filename, saveAs: true}, function (downloadId) {
- changeBadge([tab], badgeStyles.default);
+ let reader = new FileReader();
+ reader.onload = function () {
+ crypto.subtle.digest("SHA-256", reader.result).then(function (h) {
+ let filename = makeFilename(tab.title, hex(h).slice(0, 8));
+ let url = URL.createObjectURL(mhtmlData);
+ if (automatic === true) {
+ silentDownload(url, filename, path, function (downloadId) {
+ saveDownload(downloadId, tab.url);
+ });
+ } else {
+ chrome.downloads.download({url: url, filename: filename, saveAs: true}, function (downloadId) {
+ changeBadge([tab], badgeStyles.default);
+ });
+ }
});
+ };
+ reader.readAsArrayBuffer(mhtmlData);
+ }
+
+ // from: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest (public domain code)
+ function hex(buffer) {
+ let hexCodes = [];
+ let view = new DataView(buffer);
+ for (let i = 0; i < view.byteLength; i += 4) {
+ // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time)
+ let value = view.getUint32(i);
+ // toString(16) will give the hex representation of the number without padding
+ let stringValue = value.toString(16);
+ // We use concatenation and slice for padding
+ let padding = "00000000";
+ let paddedValue = (padding + stringValue).slice(-padding.length);
+ hexCodes.push(paddedValue);
}
+ // Join all the hex strings into one
+ return hexCodes.join("");
}
}
diff --git a/src/manifest.json b/src/manifest.json
index 55b4811..f9aa56c 100644
--- a/src/manifest.json
+++ b/src/manifest.json
@@ -3,7 +3,7 @@
"author": "Rahiel Kasim",
"description": "Archive webpages on archive.is, archive.org and more or on your own computer in MHTML format. Automatically archive your bookmarks.",
"homepage_url": "https://github.com/rahiel/archiveror",
- "version": "0.11",
+ "version": "0.12",
"applications": {
"gecko": {
"id": "archiveror@love.addons.mozilla.org",
diff --git a/src/utils.js b/src/utils.js
index 021159a..9f3de70 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -49,8 +49,8 @@ export function sanitizeFilename(title) {
return name;
}
-export function makeFilename(title) {
- return sanitizeFilename(title) + "_" + getTimestamp() + ".mhtml";
+export function makeFilename(title, hash) {
+ return sanitizeFilename(title) + "_" + getTimestamp() + "_" + hash + ".mhtml";
}
function getTimestamp() {