]> git.ktnx.net Git - icedeb.git/blobdiff - icedeb.js
use a hidden <textarea> for the clipboard pasting and transfer the text into a nice...
[icedeb.git] / icedeb.js
index 8c2a7e66080d64372bb16f47933583f70fb6b05b..9c9a77053aa597e6b418f69d5efd6c2306a92eb6 100644 (file)
--- a/icedeb.js
+++ b/icedeb.js
@@ -36,57 +36,92 @@ function trim(word) {
 }
 
 function open_tab(url) {
 }
 
 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) {
 }
 
 function open_link(url, in_new_tab) {
+  //console.log('open_link', url, in_new_tab);
+
   if (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) {
 }
 
 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;
   }
 
     e.preventDefault();
     return false;
   }
 
-  if ( !e.target.classList.contains('icedeb-button') )
-    return;
-
-  let autoclose = true;
-
   let clip_input = document.getElementById("clipboard");
   let clip_input = document.getElementById("clipboard");
+  if(clip_input.value == '')
+    get_clipboard_contents();
   let clip = trim(clip_input.value);
 
   let clip = trim(clip_input.value);
 
-  console.log(clip);
+  if(clip_input.value == '') return;
 
   let url;
 
 
   let url;
 
@@ -118,12 +153,41 @@ function link_clicked(e) {
       break;
   }
 
       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();
+      }
+    });
+
+  e.preventDefault();
+  return false;
 }
 
 }
 
-window.addEventListener('load', (e) => {
-  document.getElementById('button-list-container').addEventListener('click', link_clicked);
+function get_clipboard_contents() {
+  document.getElementById('error').classList.add('hidden');
+
+  let clip_pot = document.getElementById('clip-pot');
+  let clip_input = document.getElementById("clipboard");
+  clip_pot.focus();
+  if (!document.execCommand("Paste")) {
+    let err = document.getElementById('error');
+    err.textContent = 'Error retrieving clipboard contents';
+    err.classList.remove('hidden');
+  }
+  else {
+    clip_input.value = clip_pot.textContent.trim();
+    clip_input.focus();
+    clip_input.setSelectionRange(0, clip_input.value.length)
+  }
+}
+
+window.addEventListener('DOMContentLoaded', (e) => {
+  document.querySelectorAll('.icedeb-button, #button-list-container a')
+    .forEach(function(el){
+      el.addEventListener('mouseup', link_clicked);
+    });
 
   document.addEventListener('change', (e) => {
     if ( !e.target.classList.contains('icedeb-option') )
 
   document.addEventListener('change', (e) => {
     if ( !e.target.classList.contains('icedeb-option') )
@@ -132,11 +196,7 @@ window.addEventListener('load', (e) => {
     save_settings();
   });
 
     save_settings();
   });
 
-  let clip_input = document.getElementById("clipboard");
-  clip_input.focus();
-  if (!document.execCommand("paste")) {
-    let err = document.getElementById('error');
-    err.textContent = 'Error retrieving clipboard contents';
-    err.classList.remove('hidden');
-  }
+  window.requestAnimationFrame(()=>{
+    window.requestAnimationFrame(get_clipboard_contents);
+  });
 });
 });