|
1 | 1 | function find_these (query) { |
2 | 2 | return new Promise((resolve)=>{ |
3 | | - chrome.tabs.query(query, resolve); |
| 3 | + if (Object.keys(query).includes('title')) { |
| 4 | + // this is necessary until FF tab.title search works like Chrome |
| 5 | + chrome.tabs.query({}, (all_tabs)=>{ |
| 6 | + tabs = all_tabs.filter(tab=>tab.title.includes(query.title)) |
| 7 | + resolve(tabs) |
| 8 | + }); |
| 9 | + } else { |
| 10 | + return chrome.tabs.query(query, resolve); |
| 11 | + } |
4 | 12 | }); |
5 | 13 | } |
6 | 14 |
|
7 | | -function wait_for (id, resolve) { |
| 15 | +function wait_for (tab, resolve) { |
8 | 16 | let times_checked = 0; |
9 | 17 | let timerID, load_status; |
10 | 18 | const set_status = ({status}) =>load_status=status |
11 | 19 | const check_until = (status) => { |
12 | 20 | if (load_status === status || (times_checked += 1) > 150){ |
13 | 21 | window.clearTimeout(timerID) |
14 | | - return resolve() |
| 22 | + return resolve(tab) |
15 | 23 | } |
16 | | - chrome.tabs.get(id, set_status) |
| 24 | + chrome.tabs.get(tab.id, set_status) |
17 | 25 | timerID = window.setTimeout(check_until, 100, status); |
18 | 26 | } |
19 | 27 | check_until ( 'complete' ) |
20 | 28 | } |
21 | 29 |
|
22 | | -function reload (id) { |
| 30 | +function reload (tab) { |
23 | 31 | return new Promise((resolve) => { |
24 | | - chrome.tabs.reload(id) |
25 | | - wait_for(id, resolve) |
| 32 | + chrome.tabs.reload(tab.id) |
| 33 | + wait_for(tab, resolve) |
26 | 34 | }); |
27 | 35 | } |
28 | 36 |
|
29 | 37 | function reload_them (tabs) { |
30 | | - const reloads = tabs.map(({id})=>reload(id)) |
31 | | - return Promise.all(reloads) |
| 38 | + const reloaded = tabs.map((tab)=>reload(tab)) |
| 39 | + return Promise.all(reloaded) |
32 | 40 | } |
33 | 41 |
|
34 | | -function select_them (tabs) { |
35 | | - tabs.map( ({id}) => chrome.tabs.update(id, {selected:true}) ) |
36 | | - return tabs |
| 42 | +function activate_them (tabs) { |
| 43 | + activated = tabs.map( (tab)=>chrome.tabs.update(tab.id,{'active': true}) ) |
| 44 | + return Promise.all(activated) |
37 | 45 | } |
38 | 46 |
|
39 | 47 | function when_installed ({reason}) { |
40 | | - find_these({ title: "*codesy.io*" }) |
41 | | - .then(select_them) |
42 | | - .then(reload_them) |
43 | | - .then(()=>{ |
44 | | - find_these({ url: "*://*.github.com/*" }) |
45 | | - .then(reload_them) |
46 | | - }) |
| 48 | + find_these( {title:"codesy.io"} ) |
| 49 | + .then(reload_them) |
| 50 | + .then(activate_them) |
| 51 | + .then(()=>{ |
| 52 | + find_these( {url: "*://*.github.com/*"} ) |
| 53 | + .then(reload_them) |
| 54 | + }) |
47 | 55 | } |
48 | 56 |
|
49 | 57 | chrome.runtime.onInstalled.addListener(when_installed); |
0 commit comments