open links/tabs via promises
authorDamyan Ivanov <dmn@debian.org>
Mon, 19 Jun 2017 12:31:40 +0000 (12:31 +0000)
committerDamyan Ivanov <dmn@debian.org>
Mon, 19 Jun 2017 12:31:40 +0000 (12:31 +0000)
closing the pop-up before the promise is resolved/rejected
stops the JS

icedeb.js

index 8c2a7e6..c36044c 100644 (file)
--- a/icedeb.js
+++ b/icedeb.js
@@ -36,44 +36,82 @@ function trim(word) {
 }
 
 function open_tab(url) {
-  browser.tabs.create({url:url})
-    .then(
-      function() {},
-      function(err) {
-        console.log('Error creating tab', err);
-      }
-    );
+  return new Promise((resolve, reject) => {
+    browser.tabs.create({url:url, active:false})
+      .then(
+        function() {
+          resolve();
+        },
+        function(err) {
+          //console.log('Error creating tab', err);
+          reject(err);
+        }
+      );
+  });
 }
 
 function open_link(url, in_new_tab) {
+  //console.log('open_link', url, in_new_tab);
+
   if (in_new_tab) {
-    open_tab(url);
-    return;
+    return open_tab(url);
   }
 
-  browser.tabs.query({active:true, currentWindow:true})
-    .then(
-      function(tabs) {
-        browser.tabs.executeScript(tabs[0].id, {file: '/icedeb-content.js'})
-          .then(
-            function(){
-              browser.tabs.sendMessage( tabs[0].id, {url:url} );
-            },
-            function(err){
-              console.log('Error executing script', err);
-            });
-
-      },
-      function(err) {
-        console.log('Error querying the active tab of the current window', err);
-        open_tab(url);
-      }
-    );
+  //console.log('querying active tab');
+  return new Promise((resolve, reject) => {
+    browser.tabs.query({active:true, currentWindow:true})
+      .then(
+        function(tabs) {
+          //console.log('active tab queried');
+          browser.tabs.executeScript(tabs[0].id, {file: '/icedeb-content.js'})
+            .then(
+              function(){
+                //console.log('content script executed');
+                browser.tabs.sendMessage( tabs[0].id, {url:url} )
+                  .then(
+                    function() {
+                      //console.log('message sent');
+                      resolve();
+                    },
+                    function(err) {
+                      console.log('error sending message', err);
+                      reject(err);
+                    }
+                  );
+              },
+              function(err){
+                console.log('Error executing script. Probably a system tab is active', err, tabs[0]);
+                open_tab(url)
+                  .then(
+                    function() { resolve(); },
+                    function(err) { reject(err); } );
+              });
+
+        },
+        function(err) {
+          console.log('Error querying the active tab of the current window', err);
+          open_tab(url)
+            .then(
+              function() { resolve(); },
+              function(err) { reject(err) }
+            );
+        }
+      );
+  });
 }
 
 function link_clicked(e) {
-  if (e.target.tagName == 'a') {
-    open_link(e.target.href, e.button == 1);
+  //console.log(e.target.tagName);
+  let autoclose = true;
+
+  if (e.target.tagName == 'A') {
+    open_link(e.target.href, e.button == 1)
+      .then( function() {
+        if (autoclose) {
+          //console.log('closing pop-up');
+          window.close();
+        }
+      });
     e.preventDefault();
     return false;
   }
@@ -118,9 +156,13 @@ function link_clicked(e) {
       break;
   }
 
-  open_link(url, e.button == 1);
-  if (autoclose) window.close();
-}
+  open_link(url, e.button == 1)
+    .then( function() {
+      if (autoclose) {
+        //console.log('closing pop-up');
+        window.close();
+      }
+    });
 
 window.addEventListener('load', (e) => {
   document.getElementById('button-list-container').addEventListener('click', link_clicked);