]> git.ktnx.net Git - icedeb.git/blob - icedeb.js
b9e8460c89ce92cf118f867a4d57abe03f75a3ec
[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');
178   document.querySelectorAll('.likely')
179     .forEach((el) => {
180         el.classList.remove('likely');
181     });
182
183   if ( /^#?\d+$/.test(q) || /^CVE-/.test(q) )
184     cnt.add('like-b');
185
186   if ( /^[a-z0-9][a-z0-9\-+.]+$/.test(q) )
187     cnt.add('like-p');
188
189   if ( /.@[a-z0-9-]/i.test(q) )
190     cnt.add('like-m');
191
192   if ( /^<.+@.+>$/.test(q) )
193     cnt.add('like-i');
194
195   document.querySelectorAll('.like-b .hint.b, .like-p .hint.p, .like-m .hint.m, .like-i .hint.i')
196     .forEach((el) => {
197       el.parentElement.parentElement.classList.add('likely');
198     } );
199 }
200
201 function get_clipboard_contents() {
202   let clip_pot = document.getElementById('clip-pot');
203   let clip_input = document.getElementById("clipboard");
204   clip_pot.focus();
205   if (document.execCommand("Paste")) {
206     let q = clip_pot.textContent.trim();
207     clip_input.value = q;
208     clip_input.focus();
209     clip_input.setSelectionRange(0, clip_input.value.length);
210
211     check_likely_inputs(q);
212   }
213 }
214
215 window.addEventListener('DOMContentLoaded', (e) => {
216   document.querySelectorAll('.icedeb-button, #button-list-container a')
217     .forEach(function(el){
218       el.addEventListener('mouseup', link_clicked);
219     });
220
221   document.querySelector('#clipboard')
222     .addEventListener('input', function(ev) {
223       check_likely_inputs(ev.target.value);
224     });
225
226   document.addEventListener('change', (e) => {
227     if ( !e.target.classList.contains('icedeb-option') )
228       return;
229
230     save_settings();
231   });
232
233   window.requestAnimationFrame(()=>{
234     window.requestAnimationFrame(get_clipboard_contents);
235   });
236 });