From 3355ec7769751d5c445b965459bca792c4841ae3 Mon Sep 17 00:00:00 2001 From: Minioin Date: Sat, 6 Jun 2020 14:09:02 +0545 Subject: [PATCH] Implement basic functionality --- .gitignore | 2 ++ Readme.md | 4 +++- background.html | 7 ++++++ background.js | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ manifest.json | 12 ++++++---- package.json | 7 ++++-- 6 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 .gitignore create mode 100644 background.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..504afef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +package-lock.json diff --git a/Readme.md b/Readme.md index 589db3e..c894b13 100644 --- a/Readme.md +++ b/Readme.md @@ -7,9 +7,11 @@ We browse the internet and often find pieces of information in the internet. But ## How does it work? It sends requests to the archiving servers to archive the urls. However, to not overwhelm the servers, it keeps track of when was the last time given URL was sent for archiving and even checks if given URL resource has been updated since last archive via HEAD requests. +## Features +- Automatically archive new bookmarks + ## Archiving Servers - Archive.org -- Archive.md ## License Unlicense diff --git a/background.html b/background.html index e69de29..fe1f0ee 100644 --- a/background.html +++ b/background.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/background.js b/background.js new file mode 100644 index 0000000..ecd84b3 --- /dev/null +++ b/background.js @@ -0,0 +1,61 @@ +const ignoreProtocols = [ + "file://", + "tel:", + "about:", + "http://localhost", + "https://localhost", + "http://127.0.0.1", +] + +function isBlacklisted(url) { + for(let protocol of ignoreProtocols) { + if(url.startsWith(protocol)) + return true; + } + return false; +} + +async function getLastModified(url) { + try { + let res = await fetch(url, {method: 'head'}) + return new Date(res.headers.get("Last-Modified")) + } catch(e) { + console.error("Couldn't fetch last updated time of", url, e) + return new Date(); + } +} + +async function archiveAndUpdate(url) { + console.info("Archiving ", url) + let lastModified = await getLastModified(url); + let lastArchived = await browser.storage.local.get(url);; + + console.info("Last archived on ", lastArchived[url]) + console.info("Last modified on ", new Date(lastModified)) + if(lastArchived && new Date(lastArchived[url]).getTime() >= lastModified ) { + console.info("Already up to date.") + return; + } + + console.log("Archiving.") + let enabledServers = getEnabledServers(); + try { + let res = await fetch("https://web.archive.org/save/" + url, {method: 'head'}) + if(res.ok && res.status === 200) { + let object = {} + object[url] = lastModified + await browser.storage.local.set(object) + } + } catch(e) { + console.error(e) + } +} + +browser.bookmarks.onCreated.addListener(onBookmarked) +async function onBookmarked(id, bookmark) { + let {title, url, dateAdded } = bookmark; + if(!url || isBlacklisted(url)){ + return; + } + return await archiveAndUpdate(url) +} diff --git a/manifest.json b/manifest.json index 23cc143..f694990 100644 --- a/manifest.json +++ b/manifest.json @@ -3,16 +3,20 @@ "name": "Archiveit", "version": "1.1", "description": "Archive important pieces of information automatically.", - "icons": { - "48": "icons/border-48.png" - }, "background": { "page": "background.html" }, + "applications": { + "gecko": { + "id": "archiveit@minioin.protonmail.com" + } + }, "permissions": [ "", "bookmarks", "history", - "tabs" + "storage", + "tabs", + "unlimitedStorage" ] } diff --git a/package.json b/package.json index 196f375..c781005 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,14 @@ { - "name": "archiveit-webext", + "name": "archiveit", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start:firefox": "web-ext run --verbose --source-dir ." }, "author": "", + "devDependencies": { + "web-ext": "^4.2.0" + }, "license": "ISC" }