Skip to content

Commit

Permalink
Implement basic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
minioin committed Jun 6, 2020
1 parent f9a9fe7 commit 3355ec7
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
package-lock.json
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions background.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<script type="module" src="background.js"></script>
</head>
</html>
61 changes: 61 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 8 additions & 4 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<all_urls>",
"bookmarks",
"history",
"tabs"
"storage",
"tabs",
"unlimitedStorage"
]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}

0 comments on commit 3355ec7

Please sign in to comment.