X-Git-Url: https://git.ktnx.net/?p=icedeb.git;a=blobdiff_plain;f=icedeb.js;h=c36044c7c6d3a4d75c29cb3cc37f218aaa25f21e;hp=dcd1197506839e86cebcd3920cb37f021f2a0fb2;hb=f58a4234607ccc574ea15773b26c7fec6eb259d7;hpb=770b99eb23ca37c131fd5497cb5d1ce22bfb1b8d diff --git a/icedeb.js b/icedeb.js index dcd1197..c36044c 100644 --- a/icedeb.js +++ b/icedeb.js @@ -36,32 +36,95 @@ 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); + } + ); + }); } -document.addEventListener('click', (e) => { - console.log(e.button); +function open_link(url, in_new_tab) { + //console.log('open_link', url, in_new_tab); + + if (in_new_tab) { + return 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) { + //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; + } + if ( !e.target.classList.contains('icedeb-button') ) return; let autoclose = true; let clip_input = document.getElementById("clipboard"); - clip_input.focus(); - if (!document.execCommand("paste")) { - let err = document.getElementById('error'); - err.textContent = 'Error executing Paste'; - err.classList.remove('hidden'); - return; - } + let clip = trim(clip_input.value); - let clip = trim(clip_input.textContent); + console.log(clip); let url; @@ -93,49 +156,29 @@ document.addEventListener('click', (e) => { break; } - if (e.button == 1) { - open_tab(url); - if (autoclose) window.close(); - return; - } - - 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} ); - if (autoclose) window.close(); - }, - 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); - if (autoclose) window.close(); + open_link(url, e.button == 1) + .then( function() { + if (autoclose) { + //console.log('closing pop-up'); + window.close(); } - ); -}); + }); -document.addEventListener('change', (e) => { - if ( !e.target.classList.contains('icedeb-option') ) - return; - - save_settings(); -}); +window.addEventListener('load', (e) => { + document.getElementById('button-list-container').addEventListener('click', link_clicked); -browser.runtime.getPlatformInfo() - .then(function(info) { - console.log(info.os); + document.addEventListener('change', (e) => { + if ( !e.target.classList.contains('icedeb-option') ) + return; - switch (info.os) { - case 'linux': - case 'openbsd': - document.getElementById('sorry').classList.remove('hidden'); - break; - } + 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'); + } +});