-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
86 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |