remwharead/browser-plugins/webextension/popup.js

99 lines
2.3 KiB
JavaScript
Raw Normal View History

/* globals browser */
let taburl = "";
const txttags = document.getElementById("txttags");
const chkarchive = document.getElementById("chkarchive");
const btnadd = document.getElementById("btnadd");
const msgstatus = document.getElementById("msgstatus");
const msgerror = document.getElementById("msgerror");
2019-05-20 19:23:16 +02:00
function set_taburl(tabs) // Set taburl to URL of current tab.
2019-05-20 19:23:16 +02:00
{
const tab = tabs[0];
taburl = '\'' + tab.url + '\'';
2019-05-20 19:23:16 +02:00
}
function get_tags() // get tags from text input.
2019-05-20 19:23:16 +02:00
{
const tags = txttags.value;
if (tags != "")
{
2019-05-22 13:41:49 +02:00
return "-t '" + tags + "' ";
}
return "";
2019-05-20 19:23:16 +02:00
}
function read_options()
{
const item = browser.storage.sync.get('archive');
item.then((res) =>
{
if (res.archive === false)
{
chkarchive.checked = false;
}
});
}
function onResponse(response) {
2019-05-21 13:30:53 +02:00
console.log("Received: " + response);
msgstatus.textContent = "";
2019-05-21 13:30:53 +02:00
if (response == "Command successful.")
{
window.close();
}
else
{
msgerror.textContent = response;
2019-05-21 13:30:53 +02:00
}
2019-05-20 19:23:16 +02:00
}
function onError(error) {
2019-05-21 13:30:53 +02:00
console.log(`Error: ${error}`);
msgerror.textContent = "Could not launch remwharead.";
msgstatus.textContent = "";
}
function launch(args) // Launch wrapper and send tags + URL to stdin.
2019-05-20 19:23:16 +02:00
{
msgstatus.textContent = "Launching remwharead…";
msgerror.textContent = "";
console.log("Sending: " + args + " to remwharead");
const sending = browser.runtime.sendNativeMessage("remwharead", args);
2019-05-20 19:23:16 +02:00
sending.then(onResponse, onError);
}
function add()
{
let archive = "";
if (chkarchive.checked === false)
{
archive = "--no-archive ";
}
const args = get_tags() + archive + taburl;
console.log(args);
launch(args);
}
read_options();
// Call set_taburl() with current tab.
2019-05-20 19:23:16 +02:00
browser.tabs.query({currentWindow: true, active: true}).then(set_taburl);
btnadd.addEventListener("click", add);
txttags.addEventListener( // Call launch() if enter is hit in text input.
"keyup", event =>
{
if(event.key !== "Enter")
{
return;
}
add();
event.preventDefault();
});