WebExtension: Simplified and refactored code.

This commit is contained in:
tastytea 2019-05-26 18:57:44 +02:00
parent c70414114c
commit 976febdfc2
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
2 changed files with 23 additions and 19 deletions

View File

@ -6,11 +6,11 @@
<body>
<!-- I did not use a form here because I want to display status and
error messages inside the popup. -->
<label for="tags">Tags, comma separated:</label>
<input type="text" id="tags" autofocus>
<input type="button" id="button" value="Add URI">
<em id="status"></em>
<strong id="error"></strong>
<label for="txttags">Tags, comma separated:</label>
<input type="text" id="txttags" autofocus>
<input type="button" id="btnadd" value="Add URI">
<em id="msgstatus"></em>
<strong id="msgerror"></strong>
<script src="popup.js"></script>
</body>

View File

@ -9,7 +9,7 @@ function set_taburl(tabs) // Set taburl to URL of current tab.
function get_tags() // get tags from text input.
{
let tags = document.getElementById("tags").value;
let tags = txttags.value;
if (tags != "")
{
return "-t '" + tags + "' ";
@ -31,7 +31,7 @@ function read_options()
function onResponse(response) {
console.log("Received: " + response);
document.getElementById("status").textContent = "";
msgstatus.textContent = "";
if (response == "Command successful.")
{
@ -39,36 +39,40 @@ function onResponse(response) {
}
else
{
document.getElementById("error").textContent = response;
msgerror.textContent = response;
}
}
function onError(error) {
console.log(`Error: ${error}`);
document.getElementById("error").textContent = "Could not launch remwharead.";
document.getElementById("status").textContent = "";
msgerror.textContent = "Could not launch remwharead.";
msgstatus.textContent = "";
}
function launch() // Launch wrapper and send tags + URL to stdin.
function launch(args) // Launch wrapper and send tags + URL to stdin.
{
document.getElementById("status").textContent = "Launching remwharead…";
document.getElementById("error").textContent = "";
var arguments = get_tags() + archive + taburl;
console.log("Sending: " + arguments + " to remwharead");
var sending = browser.runtime.sendNativeMessage("remwharead", arguments);
msgstatus.textContent = "Launching remwharead…";
msgerror.textContent = "";
console.log("Sending: " + args + " to remwharead");
var sending = browser.runtime.sendNativeMessage("remwharead", args);
sending.then(onResponse, onError);
}
function add()
{
var arguments = get_tags() + archive + taburl;
launch(arguments);
}
read_options();
// Call set_taburl() with current tab.
browser.tabs.query({currentWindow: true, active: true}).then(set_taburl);
button.addEventListener("click", launch); // Call launch() if button is clicked.
btnadd.addEventListener("click", launch);
// Call launch if enter is hit in text input.
document.querySelector("#tags").addEventListener(
txttags.addEventListener( // Call launch() if enter is hit in text input.
"keyup", event =>
{
if(event.key !== "Enter")