Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
47 lines (44 loc) · 1.89 KB
/
Copy pathbackground.js
File metadata and controls
47 lines (44 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/* KnightSense background service worker
- Opens the welcome / landing page on first install
- Routes the stealth keyboard command to the active Chess.com tab
*/
const WELCOME_URL = chrome.runtime.getURL('welcome.html');
chrome.runtime.onInstalled.addListener((details) => {
if (details.reason === 'install') {
chrome.tabs.create({ url: WELCOME_URL, active: true });
} else if (details.reason === 'update') {
// Keep returning users informed of the new build via the welcome page.
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const cur = tabs && tabs[0];
if (cur && cur.url && cur.url.indexOf('chess.com') !== -1) {
// Don't yank a user out of a game — show it only on non-chess pages.
return;
}
if (cur && cur.url && cur.url.indexOf(chrome.runtime.id) !== -1) {
return; // already on an extension page
}
chrome.tabs.create({ url: WELCOME_URL, active: false });
});
}
});
// Keyboard shortcut: toggle overlay visibility in the active tab.
chrome.commands.onCommand.addListener(async (command) => {
if (command !== 'toggle-overlay') return;
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (!tab || !tab.id || !tab.url || tab.url.indexOf('chess.com') === -1) return;
try {
await chrome.tabs.sendMessage(tab.id, { type: 'TOGGLE_OVERLAY_HIDDEN' });
} catch (e) {
// No content script on that page yet — safe to ignore.
}
});
// A badge mini-status so the user sees the extension is live on Chess.com.
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'loading' || !tab.url) return;
if (tab.url.indexOf('chess.com') !== -1) {
chrome.action.setBadgeBackgroundColor({ color: '#ffaa00', tabId });
chrome.action.setBadgeText({ text: 'ON', tabId });
} else {
chrome.action.setBadgeText({ text: '', tabId });
}
});