Browse Source

use separate popup because of possible x-frame-options / error handling w/ iframe issues; simplify code

Andrew Dolgov 3 months ago
parent
commit
33870472c3
5 changed files with 17 additions and 71 deletions
  1. 15 12
      js/background.js
  2. 0 39
      js/popup.js
  3. 1 2
      manifest.json
  4. 1 1
      options.html
  5. 0 17
      popup.html

+ 15 - 12
js/background.js

@@ -1,38 +1,41 @@
 'use strict'
 
-function post_clicked_url(info/*, tab*/) {
+function post_to_shaarli(url, title) {
 	chrome.storage.sync.get('settings', (rv) => {
 		if (typeof rv.settings !== "undefined") {
 			const settings = rv.settings;
-			const link_url = info["srcUrl"] ? info["srcUrl"] : info["linkUrl"];
 
 			if (settings.url) {
-				const url = settings.url + '/?post=' + encodeURIComponent(link_url) +
+				const popup_url = settings.url + '/?post=' + encodeURIComponent(url) +
+					'&title=' + encodeURIComponent(title) +
 					'&source=save-to-shaarli';
 
 				const width = 600;
 				const height = 400;
-
 				const left = screen.width/2 - width/2;
 				const top = screen.height/2 - height/2;
 
-				//console.log('url', url);
-
-				window.open(url,
+				window.open(popup_url,
 					'_save_to_shaarli',
 					`left=${left},top=${top},width=${width},height=${height},
 					status=no,location=no,toolbar=no,directories=no,menubar=no,resizable=yes`);
 
-			} else {
-				alert("Please configure the extension first.");
-			}
-
+				}
 		} else {
-			alert("Please configure the extension first");
+			alert("Please configure the extension first.");
 		}
 	});
 }
 
+function post_clicked_url(info/*, tab*/) {
+	const url = info["srcUrl"] ? info["srcUrl"] : info["linkUrl"];
+	post_to_shaarli(url, "");
+}
+
+chrome.browserAction.onClicked.addListener((tab) => {
+	post_to_shaarli(tab.url, tab.title);
+});
+
 chrome.contextMenus.create({"title": "Save to Shaarli",
 	"contexts":[ "link" ],
 	"onclick": post_clicked_url});

+ 0 - 39
js/popup.js

@@ -1,39 +0,0 @@
-'use strict'
-
-chrome.storage.sync.get('settings', (rv) => {
-   if (typeof rv.settings !== "undefined") {
-      const settings = rv.settings;
-
-      if (settings.url) {
-         chrome.tabs.query({active:true, currentWindow:true}, (tabs) => {
-            const tab = tabs[0];
-
-            if (tab) {
-
-               const url = settings.url + '/?post=' + encodeURIComponent(tab.url) +
-                  '&title=' + encodeURIComponent(tab.title) +
-                  '&source=save-to-shaarli';
-
-               $("#message").hide();
-
-               $("#shaarli")
-                  .show()
-                  .attr("src", url);
-            } else {
-                  $("#message")
-                     .show()
-                     .text("Could not determine active browser tab.");
-            }
-         });
-
-      } else {
-         $("#message")
-            .show()
-            .text("Please configure the extension first.");
-      }
-   } else {
-      $("#message")
-         .show()
-         .text("Please configure the extension first.");
-   }
-});

+ 1 - 2
manifest.json

@@ -10,8 +10,7 @@
       "128": "images/icon_128.png"
    },
    "browser_action": {
-      "default_icon": "images/icon_128.png",
-      "default_popup" : "popup.html"
+      "default_icon": "images/icon_128.png"
    },
    "manifest_version": 2,
    "name": "Save to Shaarli",

+ 1 - 1
options.html

@@ -19,7 +19,7 @@
 	<form name="options" id="options">
 		<fieldset>
 			<label>Shaarli instance URL:</label>
-			<input id="url" name="url" required size="60" placeholder="https://example.com/shaarli"/>
+			<input id="url" name="url" required size="60" type="url" laceholder="https://example.com/shaarli"/>
 		</fieldset>
 
 		<p><input type="submit" value="Save"/> <span id="message"></span></p>

+ 0 - 17
popup.html

@@ -1,17 +0,0 @@
-<html>
-<head>
-	<title>Save to Shaarli options</title>
-
-	<script type="text/javascript" src="lib/jquery.js"></script>
-	<script type="text/javascript" src="js/popup.js"></script>
-
-	<style type="text/css">
-		body { margin : 0; }
-		#message { color : red; margin : 16px; }
-		</style>
-</head>
-<body>
-   <div id="message"></div>
-   <iframe style="display : none" id='shaarli' width='600' height='400'></iframe>
-</body>
-</html>