hearsay/background.js

46 lines
No EOL
1.3 KiB
JavaScript

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, '&lt;').replace(/>/g, '&gt;');
}