]> git.ktnx.net Git - icedeb.git/blob - icedeb.js
put content JS into a closure
[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   browser.tabs.create({url:url})
40     .then(
41       function() {},
42       function(err) {
43         console.log('Error creating tab', err);
44       }
45     );
46 }
47
48 function open_link(url, in_new_tab) {
49   if (in_new_tab) {
50     open_tab(url);
51     return;
52   }
53
54   browser.tabs.query({active:true, currentWindow:true})
55     .then(
56       function(tabs) {
57         browser.tabs.executeScript(tabs[0].id, {file: '/icedeb-content.js'})
58           .then(
59             function(){
60               browser.tabs.sendMessage( tabs[0].id, {url:url} );
61             },
62             function(err){
63               console.log('Error executing script', err);
64             });
65
66       },
67       function(err) {
68         console.log('Error querying the active tab of the current window', err);
69         open_tab(url);
70       }
71     );
72 }
73
74 function link_clicked(e) {
75   if (e.target.tagName == 'a') {
76     open_link(e.target.href, e.button == 1);
77     e.preventDefault();
78     return false;
79   }
80
81   if ( !e.target.classList.contains('icedeb-button') )
82     return;
83
84   let autoclose = true;
85
86   let clip_input = document.getElementById("clipboard");
87   let clip = trim(clip_input.value);
88
89   console.log(clip);
90
91   let url;
92
93   switch (e.target.id) {
94     case 'bts':
95       url = 'https://bugs.debian.org/' + clip;
96       break;
97     case 'pts':
98       url = 'https://tracker.debian.org/' + clip;
99       break;
100     case 'deb':
101       url = 'https://packages.debian.org/' + clip;
102       break;
103     case 'ml':
104       url = 'https://lists.debian.org/msgid-search/' + clip;
105       break;
106     case 'ddpo':
107       url = 'https://qa.debian.org/developer.php';
108       if (clip != '')
109         url += '?login=' + clip;
110       break;
111     case 'buildd':
112       url = 'https://buildd.debian.org/' + clip;
113       break;
114     case 'security':
115       url = 'https://security-tracker.debian.org/';
116       if (clip != '')
117         url += 'tracker/' + clip;
118       break;
119   }
120
121   open_link(url, e.button == 1);
122   if (autoclose) window.close();
123 }
124
125 window.addEventListener('load', (e) => {
126   document.getElementById('button-list-container').addEventListener('click', link_clicked);
127
128   document.addEventListener('change', (e) => {
129     if ( !e.target.classList.contains('icedeb-option') )
130       return;
131
132     save_settings();
133   });
134
135   let clip_input = document.getElementById("clipboard");
136   clip_input.focus();
137   if (!document.execCommand("paste")) {
138     let err = document.getElementById('error');
139     err.textContent = 'Error retrieving clipboard contents';
140     err.classList.remove('hidden');
141   }
142 });