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