From 3eacd6a9571d8d3889fec00bae57b68b7963d0bf Mon Sep 17 00:00:00 2001 From: risotto Date: Sun, 12 Jan 2025 01:40:55 -0700 Subject: [PATCH] scaffolding for firefox extension --- background.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ icons/.keep | 1 + manifest.json | 35 +++++++++++++++++++++++++++++++++++ options.html | 15 +++++++++++++++ options.js | 23 +++++++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 background.js create mode 100644 icons/.keep create mode 100644 manifest.json create mode 100644 options.html create mode 100644 options.js diff --git a/background.js b/background.js new file mode 100644 index 0000000..a444a16 --- /dev/null +++ b/background.js @@ -0,0 +1,46 @@ +browser.contextMenus.create({ + id: "copy-link-to-clipboard", + title: "Copy link to clipboard", + contexts: ["link"], +}, +() => void browser.runtime.lastError, +); + +function submit_url(settings_url,to_submit) { + try { + const resp = fetch(settings_url,{ + method: "POST", + headers: { + "Content-Type":"application/json", + "Authorization":settings_auth, + }, + body: JSON.stringify({"url":to_submit}) + }); + if (!resp.ok){ + throw new Error(`Response status: ${resp.status}`); + } + } catch (error){ + console.error(error.message); + } +} +function onError(error) { + console.log(`Error: ${error}`); + } + +browser.contextMenus.onClicked.addListener((info, tab) => { +if (info.menuItemId === "copy-link-to-clipboard") { + console.log("run extension"); + const url = escapeHTML(info.linkUrl); + console.log(url); // submit this to the listener configured in settings + const getting = browser.storage.sync.get("submission_url"); + getting.then(submit_url(url), onError) +} +}); + +// https://gist.github.com/Rob--W/ec23b9d6db9e56b7e4563f1544e0d546 +function escapeHTML(str) { + return String(str) + .replace(/&/g, '&') + .replace(/"/g, '"').replace(/'/g, ''') + .replace(//g, '>'); +} \ No newline at end of file diff --git a/icons/.keep b/icons/.keep new file mode 100644 index 0000000..c693f13 --- /dev/null +++ b/icons/.keep @@ -0,0 +1 @@ +keep \ No newline at end of file diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..bb68952 --- /dev/null +++ b/manifest.json @@ -0,0 +1,35 @@ +{ + "manifest_version": 2, + "name": "Hearsay: recommend links", + "description": "A tool for recommending links", + "version": "1.0", + "homepage_url": "https://git.bivouac.wiki/misc/hearsay", + + "background": { + "scripts": [ + "background.js" + ], + "persistent": false + }, + "content_scripts":[ + { + "matches": [""], + "js": ["borderify.js"] + } + ], + "options_ui":{ + "page":"options.html" + }, + "browser_specific_settings":{ + "gecko":{ + "id": "hearsay@git.bivouac.wiki" + } + }, + + "permissions": [ + "activeTab", + "contextMenus", + "clipboardWrite", + "storage" + ] +} \ No newline at end of file diff --git a/options.html b/options.html new file mode 100644 index 0000000..c80ed57 --- /dev/null +++ b/options.html @@ -0,0 +1,15 @@ + + + + + + + +
+ + +
+ + + + diff --git a/options.js b/options.js new file mode 100644 index 0000000..074957d --- /dev/null +++ b/options.js @@ -0,0 +1,23 @@ +function saveOptions(e) { + e.preventDefault(); + browser.storage.sync.set({ + color: document.querySelector("#color").value, + }); + } + + function restoreOptions() { + function setCurrentChoice(result) { + document.querySelector("#color").value = result.color || "blue"; + } + + function onError(error) { + console.log(`Error: ${error}`); + } + + let getting = browser.storage.sync.get("color"); + getting.then(setCurrentChoice, onError); + } + + document.addEventListener("DOMContentLoaded", restoreOptions); + document.querySelector("form").addEventListener("submit", saveOptions); + \ No newline at end of file