]> git.ktnx.net Git - icedeb.git/blob - icedeb.js
add indicators of the services that seem appropriate for the input
[icedeb.git] / icedeb.js
1 function load_settings() {
2   browser.storage.local.get({auto_close: true})
3   .then(
4     function(v) {
5       document.getElementById('auto-close').checked = v.auto_close === true;
6       //console.log('Success', v);
7     },
8     function() {
9       document.getElementById('auto-close').checked = true;
10       //console.log('Failure');
11     }
12   );
13 }
14
15 function save_settings() {
16   let v = document.getElementById('auto-close').checked;
17   browser.storage.local.set({
18     auto_close: v,
19   });
20   //console.log('stored', v);
21 }
22
23 function trim(word) {
24   if (!word) return word;
25
26   var oldword;
27   do {
28     oldword = word;
29
30     word = word.replace(/^[^a-zA-Z0-9]+/, '');
31     word = word.replace(/[^a-zA-Z0-9]+$/, '');
32     word = word.replace(/^Bug#/i, '');
33   } while ( oldword != word );
34
35   return word;
36 }
37
38 function open_tab(url) {
39   return new Promise((resolve, reject) => {
40     browser.tabs.create({url:url, active:false})
41       .then(
42         function() {
43           resolve();
44         },
45         function(err) {
46           //console.log('Error creating tab', err);
47           reject(err);
48         }
49       );
50   });
51 }
52
53 function open_link(url, in_new_tab) {
54   //console.log('open_link', url, in_new_tab);
55
56   if (in_new_tab) {
57     return open_tab(url);
58   }
59
60   //console.log('querying active tab');
61   return new Promise((resolve, reject) => {
62     browser.tabs.query({active:true, currentWindow:true})
63       .then(
64         function(tabs) {
65           //console.log('active tab queried');
66           browser.tabs.executeScript(tabs[0].id, {file: '/icedeb-content.js'})
67             .then(
68               function(){
69                 //console.log('content script executed');
70                 browser.tabs.sendMessage( tabs[0].id, {url:url} )
71                   .then(
72                     function() {
73                       //console.log('message sent');
74                       resolve();
75                     },
76                     function(err) {
77                       console.log('error sending message', err);
78                       reject(err);
79                     }
80                   );
81               },
82               function(err){
83                 console.log('Error executing script. Probably a system tab is active', err, tabs[0]);
84                 open_tab(url)
85                   .then(
86                     function() { resolve(); },
87                     function(err) { reject(err); } );
88               });
89
90         },
91         function(err) {
92           console.log('Error querying the active tab of the current window', err);
93           open_tab(url)
94             .then(
95               function() { resolve(); },
96               function(err) { reject(err) }
97             );
98         }
99       );
100   });
101 }
102
103 function link_clicked(e) {
104   //console.log(e.target.tagName);
105   let autoclose = true;
106
107   if (e.target.tagName == 'A') {
108     open_link(e.target.href, e.button == 1)
109       .then( function() {
110         if (autoclose) {
111           //console.log('closing pop-up');
112           window.close();
113         }
114       });
115     e.preventDefault();
116     return false;
117   }
118
119   let clip_input = document.getElementById("clipboard");
120   let clip = trim(clip_input.value);
121
122   if(clip_input.value == '') return;
123
124   let url;
125
126   switch (e.target.id) {
127     case 'bts':
128       url = 'https://bugs.debian.org/' + clip;
129       break;
130     case 'pts':
131       url = 'https://tracker.debian.org/' + clip;
132       break;
133     case 'deb':
134       url = 'https://packages.debian.org/' + clip;
135       break;
136     case 'ml':
137       url = 'https://lists.debian.org/msgid-search/' + clip;
138       break;
139     case 'ddpo':
140       url = 'https://qa.debian.org/developer.php?login=' + clip;
141       break;
142     case 'dmd':
143       url = 'https://udd.debian.org/dmd.cgi?email1=' + clip;
144       break;
145     case 'buildd':
146       url = 'https://buildd.debian.org/' + clip;
147       break;
148     case 'security':
149       url = 'https://security-tracker.debian.org/tracker/' + clip;
150       break;
151   }
152
153   open_link(url, e.button == 1)
154     .then( function() {
155       if (autoclose) {
156         //console.log('closing pop-up');
157         window.close();
158       }
159     });
160
161   e.preventDefault();
162   return false;
163 }
164
165 function check_likely_inputs(q) {
166   let cnt = document.getElementById('button-list-container').classList;
167
168   cnt.remove('like-b', 'like-p', 'like-m', 'like-i');
169   document.querySelectorAll('.likely')
170     .forEach((el) => {
171         el.classList.remove('likely');
172     });
173
174   if ( /^\d+$/.test(q) || /^CVE-/.test(q) )
175     cnt.add('like-b');
176
177   if ( /^[a-z0-9][a-z0-9\-+.]+$/.test(q) )
178     cnt.add('like-p');
179
180   if ( /.@[a-z0-9-]/i.test(q) )
181     cnt.add('like-m');
182
183   if ( /^<.+@.+>$/.test(q) )
184     cnt.add('like-i');
185
186   document.querySelectorAll('.like-b .hint.b, .like-p .hint.p, .like-m .hint.m, .like-i .hint.i')
187     .forEach((el) => {
188       el.parentElement.parentElement.classList.add('likely');
189     } );
190 }
191
192 function get_clipboard_contents() {
193   let clip_pot = document.getElementById('clip-pot');
194   let clip_input = document.getElementById("clipboard");
195   clip_pot.focus();
196   if (document.execCommand("Paste")) {
197     let q = clip_pot.textContent.trim();
198     clip_input.value = q;
199     clip_input.focus();
200     clip_input.setSelectionRange(0, clip_input.value.length);
201
202     check_likely_inputs(q);
203   }
204 }
205
206 window.addEventListener('DOMContentLoaded', (e) => {
207   document.querySelectorAll('.icedeb-button, #button-list-container a')
208     .forEach(function(el){
209       el.addEventListener('mouseup', link_clicked);
210     });
211
212   document.querySelector('#clipboard')
213     .addEventListener('input', function(ev) {
214       check_likely_inputs(ev.target.value);
215     });
216
217   document.addEventListener('change', (e) => {
218     if ( !e.target.classList.contains('icedeb-option') )
219       return;
220
221     save_settings();
222   });
223
224   window.requestAnimationFrame(()=>{
225     window.requestAnimationFrame(get_clipboard_contents);
226   });
227 });