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> <body>
<!-- I did not use a form here because I want to display status and <!-- I did not use a form here because I want to display status and
error messages inside the popup. --> error messages inside the popup. -->
<label for="tags">Tags, comma separated:</label> <label for="txttags">Tags, comma separated:</label>
<input type="text" id="tags" autofocus> <input type="text" id="txttags" autofocus>
<input type="button" id="button" value="Add URI"> <input type="button" id="btnadd" value="Add URI">
<em id="status"></em> <em id="msgstatus"></em>
<strong id="error"></strong> <strong id="msgerror"></strong>
<script src="popup.js"></script> <script src="popup.js"></script>
</body> </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. function get_tags() // get tags from text input.
{ {
let tags = document.getElementById("tags").value; let tags = txttags.value;
if (tags != "") if (tags != "")
{ {
return "-t '" + tags + "' "; return "-t '" + tags + "' ";
@ -31,7 +31,7 @@ function read_options()
function onResponse(response) { function onResponse(response) {
console.log("Received: " + response); console.log("Received: " + response);
document.getElementById("status").textContent = ""; msgstatus.textContent = "";
if (response == "Command successful.") if (response == "Command successful.")
{ {
@ -39,36 +39,40 @@ function onResponse(response) {
} }
else else
{ {
document.getElementById("error").textContent = response; msgerror.textContent = response;
} }
} }
function onError(error) { function onError(error) {
console.log(`Error: ${error}`); console.log(`Error: ${error}`);
document.getElementById("error").textContent = "Could not launch remwharead."; msgerror.textContent = "Could not launch remwharead.";
document.getElementById("status").textContent = ""; 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…"; msgstatus.textContent = "Launching remwharead…";
document.getElementById("error").textContent = ""; msgerror.textContent = "";
var arguments = get_tags() + archive + taburl; console.log("Sending: " + args + " to remwharead");
console.log("Sending: " + arguments + " to remwharead"); var sending = browser.runtime.sendNativeMessage("remwharead", args);
var sending = browser.runtime.sendNativeMessage("remwharead", arguments);
sending.then(onResponse, onError); sending.then(onResponse, onError);
} }
function add()
{
var arguments = get_tags() + archive + taburl;
launch(arguments);
}
read_options(); read_options();
// Call set_taburl() with current tab. // Call set_taburl() with current tab.
browser.tabs.query({currentWindow: true, active: true}).then(set_taburl); 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. txttags.addEventListener( // Call launch() if enter is hit in text input.
document.querySelector("#tags").addEventListener(
"keyup", event => "keyup", event =>
{ {
if(event.key !== "Enter") if(event.key !== "Enter")