Browse Source

Add icons, options, error notification

Christoph Stelz 1 year ago
parent
commit
73d15f5996

+ 15 - 12
firefoxplugin/background.js

@@ -45,28 +45,31 @@ browser.browserAction.onClicked.addListener(async (tab) => {
             } else {
                 absoluteUrl = baseUrl + "/" + src;
             }
-            //console.log("Fetching ", absoluteUrl);
 
             const dataUrl = await imageUrlToBase64(absoluteUrl);
 
-            console.log(src + " becomes " + dataUrl);
             return "src=\"" + dataUrl + "\"";
         });
-        //for (const [_, source] of articleHtml.matchAll(srcRegexp)) {
-
-        //    fetch(source)
-        //}
         console.log("All matching done", inlinedImageHtml)
 
 
 
-
-        await fetch("http://192.168.15.244:17144", {
-            method: 'POST',
-            body: inlinedImageHtml
-        });
+        const url = (await browser.storage.local.get("koreaderurl")).koreaderurl;
+        console.log("posting to " + url);
+        try {
+            await fetch(url, {
+                method: 'POST',
+                body: inlinedImageHtml
+            });
+        } catch(error) {
+            throw 'KOReader server unreachable, check connection and URL in settings';
+        }
     } catch(err) {
-        console.error("Problem while running script: ", err);
+        browser.notifications.create({
+            'type': 'basic',
+            'message': 'Could not post article to KOReader: ' + err,
+            'title': 'Firefox2KOReader'
+        });
     }
 
 })

BIN
firefoxplugin/icons/koreader-19.png


BIN
firefoxplugin/icons/koreader-38.png


BIN
firefoxplugin/icons/koreader.png


+ 14 - 2
firefoxplugin/manifest.json

@@ -4,16 +4,28 @@
     "version": "1.0",
     "description": "Sync articles instantly to your KOReader",
     "icons": {
-        "48": "icons/border-48.png"
+        "19": "icons/koreader-19.png",
+        "38": "icons/koreader-38.png",
+        "600": "icons/koreader.png"
     },
     "permissions": [
         "tabs",
+        "storage",
+        "notifications",
         "<all_urls>"
     ],
     "browser_action": {
-        "default_icon": "icons/koreader.png",
+        "default_icon": {
+            "19": "icons/koreader-19.png",
+            "38": "icons/koreader-38.png",
+            "600": "icons/koreader.png"
+        },
         "default_title": "Show on KOReader"
     },
+    "options_ui": {
+        "page": "options.html",
+        "browser_style": true
+    },
     "background": {
         "scripts": ["background.js"]
     }

+ 16 - 0
firefoxplugin/options.html

@@ -0,0 +1,16 @@
+<!doctype html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+    </head>
+
+    <body>
+        <form>
+            <label for="koreaderurl">KOReader URL</label>
+            <input type="text" id="koreaderurl" />
+
+            <button type="submit">Save</button>
+        </form>
+        <script src="options.js"></script>
+    </body>
+</html>

+ 11 - 0
firefoxplugin/options.js

@@ -0,0 +1,11 @@
+const urlField = document.querySelector("#koreaderurl");
+
+browser.storage.local.get("koreaderurl").then((res) => {
+    console.log(res);
+    urlField.value = res.koreaderurl || "http://127.0.0.1:17144";
+});
+
+document.querySelector("button").onclick = async function(e) {
+    await browser.storage.local.set({"koreaderurl": urlField.value});
+    e.preventDefault();
+}