commit 03128e75157467091796b0be64ba40156db9186c Author: Ondřej Hruška Date: Sat Feb 23 18:48:48 2019 +0100 initial version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44ce34d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +web-ext-artifacts diff --git a/.web-extension-id b/.web-extension-id new file mode 100644 index 0000000..249279a --- /dev/null +++ b/.web-extension-id @@ -0,0 +1,3 @@ +# This file was created by https://github.com/mozilla/web-ext +# Your auto-generated extension ID for addons.mozilla.org is: +@save_link_to_file \ No newline at end of file diff --git a/background.js b/background.js new file mode 100644 index 0000000..427f71e --- /dev/null +++ b/background.js @@ -0,0 +1,58 @@ +/** HTML escape */ +function escapeHtml(unsafe) { + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); +} + +/** Escape URL chars that could throw off the meta redirect tag */ +function sanitizeUrl(unsafe) { + return unsafe + .replace(/;/g, "%3B"); +} + +/** Remove the most risky characters from a filename string */ +function cleanFilename(unsafe) { + return unsafe + .replace(/[/\\?*|"'<>:]+/g, " ") + .replace(/\s+/g, " "); +} + +browser.browserAction.onClicked.addListener((tab) => { + const escapedUrl = escapeHtml(sanitizeUrl(tab.url)); + const escapedTitle = escapeHtml(tab.title); + const filename = cleanFilename(tab.title); + + const content = new Blob([ +` + + + + ${escapedTitle} + + + + + Redirecting to: ${escapedUrl} + + +`]); + + browser.downloads.download({ + filename: `${filename}.link.html`, + url: URL.createObjectURL(content) + }); +}); diff --git a/icon.svg b/icon.svg new file mode 100644 index 0000000..9e5a446 --- /dev/null +++ b/icon.svg @@ -0,0 +1,79 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..1a6d9da --- /dev/null +++ b/manifest.json @@ -0,0 +1,29 @@ +{ + "applications":{ + "gecko":{ + "id":"@save_link_to_file", + "strict_min_version":"64.0" + } + }, + "manifest_version":2, + "author": "Ondřej Hruška", + "name":"Save Link to File", + "version":"0.2", + "description":"Saves the current page as a HTML file with auto-redirect. It's like a bookmark you can store anywhere on your disk.", + "icons":{ + "48":"icon.svg", + "128":"icon.svg" + }, + "background":{ + "scripts":["background.js"] + }, + "permissions":[ + "activeTab", + "downloads" + ], + "browser_action": { + "browser_style": true, + "default_icon": "icon.svg", + "default_title": "Save Link" + } +}