]> git.ktnx.net Git - icedeb.git/blob - icedeb.js
add hints to each button
[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 get_clipboard_contents() {
166   let clip_pot = document.getElementById('clip-pot');
167   let clip_input = document.getElementById("clipboard");
168   clip_pot.focus();
169   if (document.execCommand("Paste")) {
170     clip_input.value = clip_pot.textContent.trim();
171     clip_input.focus();
172     clip_input.setSelectionRange(0, clip_input.value.length)
173   }
174 }
175
176 window.addEventListener('DOMContentLoaded', (e) => {
177   document.querySelectorAll('.icedeb-button, #button-list-container a')
178     .forEach(function(el){
179       el.addEventListener('mouseup', link_clicked);
180     });
181
182   document.addEventListener('change', (e) => {
183     if ( !e.target.classList.contains('icedeb-option') )
184       return;
185
186     save_settings();
187   });
188
189   window.requestAnimationFrame(()=>{
190     window.requestAnimationFrame(get_clipboard_contents);
191   });
192 });