|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const HANGOUTS_PREFIX = "https://hangouts.google.com/call/"; |
4 |
| -const DEFAULT_SIGN_ENDPOINT = "http://192.168.1.22:5000"; |
| 3 | +const HANGOUTS_PATTERN = 'https://hangouts.google.com/call/*'; |
| 4 | +const DEFAULT_SIGN_ENDPOINT = 'http://192.168.1.22:5000'; |
5 | 5 |
|
6 |
| -function handleRemovedTab(tabId, existingHangouts, signEndpoint) { |
7 |
| - const tabIdx = existingHangouts.indexOf(tabId); |
8 |
| - if (tabIdx >= 0) { |
9 |
| - existingHangouts.splice(tabIdx) |
10 |
| - if (existingHangouts.length === 0) { |
11 |
| - fetch(signEndpoint + "/off?source=vc", { |
12 |
| - mode: "no-cors" |
13 |
| - }); |
14 |
| - } |
15 |
| - } |
16 |
| -} |
17 |
| - |
18 |
| - chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { |
19 |
| - if (changeInfo.url) { |
20 |
| - chrome.storage.local.get(null, (value) => { |
21 |
| - const existingHangouts = value ? value.hangouts || [] : []; |
22 |
| - const signEndpoint = value ? value.endpoint || DEFAULT_SIGN_ENDPOINT : DEFAULT_SIGN_ENDPOINT; |
23 |
| - if (changeInfo.url.startsWith(HANGOUTS_PREFIX)) { |
24 |
| - if (existingHangouts.length === 0) { |
25 |
| - fetch(signEndpoint + "/on?source=vc", { |
26 |
| - mode: "no-cors" |
27 |
| - }); |
28 |
| - } |
29 |
| - existingHangouts.push(tabId); |
30 |
| - } else { |
31 |
| - handleRemovedTab(tabId, existingHangouts, signEndpoint); |
32 |
| - } |
33 |
| - chrome.storage.local.set({hangouts: Array.from(new Set(existingHangouts))}); |
34 |
| - }); |
35 |
| - } |
36 |
| - }); |
| 6 | +function checkTabsAndUpdateStatusIfNecessary() { |
| 7 | + chrome.storage.local.get(null, value => { |
| 8 | + const committedStatus = value && value.committedStatus === 'boolean' ? value.committedStatus : null; |
| 9 | + const signEndpoint = value && value.endpoint ? value.endpoint : DEFAULT_SIGN_ENDPOINT; |
37 | 10 |
|
38 |
| - chrome.tabs.onRemoved.addListener((tabId) => { |
39 |
| - chrome.storage.local.get(null, (value) => { |
40 |
| - const existingHangouts = value ? value.hangouts || [] : []; |
41 |
| - const signEndpoint = value ? value.endpoint || DEFAULT_SIGN_ENDPOINT : DEFAULT_SIGN_ENDPOINT; |
42 |
| - handleRemovedTab(tabId, existingHangouts, signEndpoint); |
43 |
| - chrome.storage.local.set({hangouts: existingHangouts}); |
| 11 | + chrome.tabs.query({ url: HANGOUTS_PATTERN }, tabs => { |
| 12 | + const currentStatus = tabs.length > 0; |
| 13 | + if (currentStatus != committedStatus) { |
| 14 | + const path = currentStatus ? 'on' : 'off'; |
| 15 | + fetch(`${signEndpoint}/${path}?source=vc`, { |
| 16 | + mode: 'no-cors', |
| 17 | + }).then(() => { |
| 18 | + chrome.storage.local.set({ committedStatus: currentStatus }); |
| 19 | + }); |
| 20 | + } |
44 | 21 | });
|
45 | 22 | });
|
| 23 | +} |
| 24 | + |
| 25 | +chrome.tabs.onUpdated.addListener(checkTabsAndUpdateStatusIfNecessary); |
| 26 | + |
| 27 | +chrome.tabs.onRemoved.addListener(checkTabsAndUpdateStatusIfNecessary); |
0 commit comments