scaffolding for firefox extension
This commit is contained in:
parent
746d7bde7e
commit
3eacd6a957
5 changed files with 120 additions and 0 deletions
46
background.js
Normal file
46
background.js
Normal file
|
@ -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, '<').replace(/>/g, '>');
|
||||
}
|
1
icons/.keep
Normal file
1
icons/.keep
Normal file
|
@ -0,0 +1 @@
|
|||
keep
|
35
manifest.json
Normal file
35
manifest.json
Normal file
|
@ -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": ["<all_urls>"],
|
||||
"js": ["borderify.js"]
|
||||
}
|
||||
],
|
||||
"options_ui":{
|
||||
"page":"options.html"
|
||||
},
|
||||
"browser_specific_settings":{
|
||||
"gecko":{
|
||||
"id": "hearsay@git.bivouac.wiki"
|
||||
}
|
||||
},
|
||||
|
||||
"permissions": [
|
||||
"activeTab",
|
||||
"contextMenus",
|
||||
"clipboardWrite",
|
||||
"storage"
|
||||
]
|
||||
}
|
15
options.html
Normal file
15
options.html
Normal file
|
@ -0,0 +1,15 @@
|
|||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<form>
|
||||
<label>Border color <input type="text" id="color" name="color" /></label>
|
||||
<button type="submit">Save</button>
|
||||
</form>
|
||||
|
||||
<script src="options.js"></script>
|
||||
</body>
|
||||
</html>
|
23
options.js
Normal file
23
options.js
Normal file
|
@ -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);
|
||||
|
Loading…
Reference in a new issue