remwharead/browser-plugins/webextension/launch.js

63 lines
1.5 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);
if (response == "Command successful.")
{
window.close();
}
else
{
document.getElementById("error").innerHTML =
"<strong>" + response + "</strong>";
}
2019-05-20 19:23:16 +02:00
}
function onError(error) {
2019-05-21 13:30:53 +02:00
console.log(`Error: ${error}`);
}
function launch() // Launch wrapper and send tags + URL to stdin.
2019-05-20 19:23:16 +02:00
{
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();
});