remwharead/browser-plugins/webextension/launch.js

67 lines
1.7 KiB
JavaScript
Raw Normal View History

2019-05-20 19:23:16 +02:00
var taburl;
var port;
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
{
let 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
{
let tags = document.getElementById("tags").value;
if (tags != "")
{
return "-t " + tags + " ";
}
return "";
2019-05-20 19:23:16 +02:00
}
function onResponse(response) {
2019-05-21 13:30:53 +02:00
console.log("Received: " + response);
2019-05-21 17:11:14 +02:00
document.getElementById("status").textContent = "";
2019-05-21 13:30:53 +02:00
if (response == "Command successful.")
{
window.close();
}
else
{
2019-05-21 15:02:10 +02:00
document.getElementById("error").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}`);
document.getElementById("error").textContent = "Could not launch remwharead.";
document.getElementById("status").textContent = "";
}
function launch() // Launch wrapper and send tags + URL to stdin.
2019-05-20 19:23:16 +02:00
{
2019-05-21 17:11:14 +02:00
document.getElementById("status").textContent = "Launching remwharead…";
var arguments = get_tags() + taburl;
console.log("Sending: " + arguments + " to remwharead");
var sending = browser.runtime.sendNativeMessage("remwharead", arguments);
2019-05-20 19:23:16 +02:00
sending.then(onResponse, onError);
}
// Call set_taburl() with current tab.
2019-05-20 19:23:16 +02:00
browser.tabs.query({currentWindow: true, active: true}).then(set_taburl);
button.addEventListener("click", launch); // Call send() if submit is clicked.
// Click button if enter is hit in text input.
document.querySelector("#tags").addEventListener(
"keyup", event =>
{
if(event.key !== "Enter")
{
return;
}
document.querySelector("#button").click();
event.preventDefault();
});