Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
81 lines (71 loc) · 2.67 KB
/
Copy pathbackground.js
File metadata and controls
81 lines (71 loc) · 2.67 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// MV3 background context: keyboard command handling and update bookkeeping.
if (typeof importScripts === 'function') importScripts('settings.js');
if (chrome.runtime.setUninstallURL) {
chrome.runtime.setUninstallURL(YtocSettings.UNINSTALL_SURVEY_URL);
}
function message(key, substitutions) {
return chrome.i18n.getMessage(key, substitutions);
}
function profileShortcutHint(shortcut) {
return shortcut ? message('hudProfileHint', shortcut) : undefined;
}
function sendHud(title, detail) {
chrome.tabs.query({ active: true, currentWindow: true }, tabs => {
const tab = tabs[0];
if (!tab?.id) return;
chrome.tabs.sendMessage(tab.id, { action: 'showHud', title, detail }).catch(() => {});
});
}
chrome.commands.onCommand.addListener(command => {
chrome.storage.sync.get(null, result => {
const settings = YtocSettings.migrateSettings(result);
const next = YtocSettings.applyCommand(settings, command);
chrome.storage.sync.set(next, () => {
switch (command) {
case 'toggle-enhancements':
sendHud(message(next.extensionEnabled ? 'hudEnhancementsOn' : 'hudEnhancementsOff'));
break;
case 'cycle-profile':
chrome.commands.getAll(commands => {
const shortcut = commands.find(item => item.name === 'cycle-profile')?.shortcut;
sendHud(
message(YtocSettings.PROFILE_MESSAGE_KEYS[next.accessibilityProfile]),
profileShortcutHint(shortcut)
);
});
break;
case 'toggle-always-show':
sendHud(message(next.alwaysShowControls ? 'hudControlsVisible' : 'hudControlsAutoHide'));
break;
default:
break;
}
});
});
});
chrome.runtime.onInstalled.addListener(details => {
if (details.reason === 'install') {
chrome.storage.local.set({ installedAt: Date.now(), playerHintPending: true });
chrome.tabs.create({ url: chrome.runtime.getURL('welcome.html') });
}
if (details.reason === 'update') {
chrome.storage.sync.get(null, stored => {
const hasSettings = Object.keys(YtocSettings.LEGACY_DEFAULTS)
.some(key => Object.prototype.hasOwnProperty.call(stored || {}, key));
if (!hasSettings) {
chrome.storage.sync.set(
YtocSettings.migrateSettings({ ...YtocSettings.LEGACY_DEFAULTS })
);
}
});
chrome.storage.local.get('installedAt', stored => {
const localSettings = {
whatsNewVersion: chrome.runtime.getManifest().version
};
if (!Object.prototype.hasOwnProperty.call(stored || {}, 'installedAt')) {
localSettings.installedAt = Date.now();
}
chrome.storage.local.set(localSettings);
});
}
});