Browse Source

Add a webapp manifest, and support share_target

Trevor Elliott 9 months ago
parent
commit
c33d562593
5 changed files with 74 additions and 1 deletions
  1. 6 0
      js/index.js
  2. 11 0
      js/serviceWorker.js
  3. 26 0
      lc/app.py
  4. 29 0
      static/manifest.json
  5. 2 1
      templates/main.mustache

+ 6 - 0
js/index.js

@@ -27,6 +27,11 @@ let confirmDelete = (url, id) => {
 };
 
 document.addEventListener("DOMContentLoaded", () => {
+  if ('serviceWorker' in navigator) {
+    // TODO(trevor) does it matter if this fails?
+    navigator.serviceWorker.register('/service-worker.js')
+  }
+
   $(".deletelink").each((idx, elem) => {
     $(elem).on("click", (event) => {
       confirmDelete(event.target.dataset.url, event.target.dataset.linkId);
@@ -67,4 +72,5 @@ document.addEventListener("DOMContentLoaded", () => {
       return false;
     }
   });
+
 });

+ 11 - 0
js/serviceWorker.js

@@ -0,0 +1,11 @@
+
+self.addEventListener("install", event => {
+  self.skipWaiting();
+});
+
+self.addEventListener("activate", event => {
+  return self.clients.claim();
+});
+
+self.addEventListener("fetch", event => {
+});

+ 26 - 0
lc/app.py

@@ -290,3 +290,29 @@ class PinboardImport(Endpoint):
             raise e.BadFileUpload("no file selected")
         u.import_pinboard_data(file.stream)
         return self.api_ok(u.base_url())
+
+
+@endpoint("/service-worker.js")
+class ServiceWorker(Endpoint):
+    def route(self, *args, **kwargs):
+        return flask.send_file("../js/serviceWorker.js", mimetype="text/javascript")
+
+@endpoint("/add-link")
+class AddLink(Endpoint):
+    def html(self):
+        if not self.user:
+            raise e.LCRedirect("/login")
+
+
+        args = flask.request.args.copy()
+
+        # Android sets the text field to the url only
+        url = args.get("url", None)
+        text = args.get("text", "")
+        if url is None and text.startswith("http"):
+            args["url"] = text
+            del args['text']
+
+        args = "&".join(f"{key}={value}" for key, value in args.items())
+
+        raise e.LCRedirect(f"/u/{self.user.name}/l?{args}")

+ 29 - 0
static/manifest.json

@@ -0,0 +1,29 @@
+{
+  "short_name": "Lament Configuration",
+  "name": "Lament Configuration",
+  "start_url": "/",
+  "scope": "/",
+  "display": "minimal-ui",
+  "icons": [
+    {
+      "src": "/static/lc_192.png",
+      "type": "image/png",
+      "sizes": "192x192"
+    },
+    {
+      "src": "/static/lc_512.png",
+      "type": "image/png",
+      "sizes": "512x512"
+    }
+  ],
+  "share_target": {
+    "action": "/add-link",
+    "method": "GET",
+    "enctype": "application/x-www-form-urlencoded",
+    "params": {
+      "title": "name",
+      "text": "text",
+      "url": "url"
+    }
+  }
+}

+ 2 - 1
templates/main.mustache

@@ -6,7 +6,8 @@
     <title>Lament Configuration</title>
     <link rel="stylesheet" type="text/css" href="/static/main.css" />
     <link rel="stylesheet" type="text/css" href="/static/tagify.css" />
-    <script type="text/javascript" src="/static/lc.js"></script>
+    <link rel="manifest" href="/static/manifest.json" />
+    <script type="text/javascript" src="/static/lc.js" defer></script>
   </head>
   <body>
     <div class="header">