Skip to content

Commit 8a48963

Browse files
committed
new strategy for the chrome extension
1 parent 20155b0 commit 8a48963

File tree

2 files changed

+28
-39
lines changed

2 files changed

+28
-39
lines changed

.vscode/.prettierrc

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"printWidth": 120,
3+
"singleQuote": true,
4+
"trailingComma": "all",
5+
"arrowParens": "avoid",
6+
"proseWrap": "always"
7+
}

chrome_extension/background.js

+21-39
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,27 @@
11
'use strict';
22

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';
55

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;
3710

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+
}
4421
});
4522
});
23+
}
24+
25+
chrome.tabs.onUpdated.addListener(checkTabsAndUpdateStatusIfNecessary);
26+
27+
chrome.tabs.onRemoved.addListener(checkTabsAndUpdateStatusIfNecessary);

0 commit comments

Comments
 (0)