-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbackground_script.ts
More file actions
221 lines (200 loc) · 7.88 KB
/
background_script.ts
File metadata and controls
221 lines (200 loc) · 7.88 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import 'webext-dynamic-content-scripts';
import { Creator, loadCreators as _loadCreators, creatorHasNebulaVideo, creatorHasYTVideo, existsNebulaVideo, normalizeString } from './background';
import { purgeCache, purgeCacheIfNecessary } from './background/ext';
import type { CreatorSettings } from './content/nebula/creator-settings';
import { BrowserMessage, getBase, getBrowserInstance, getFromStorage, isChrome, nebulavideo, parseTimeString, parseTypeObject, setToStorage, toTimeString } from './helpers/sharedExt';
import { repairPlayerSettings } from './options/player';
import { Settings } from './page/components';
const videoFetchYt = 50;
const videoFetchNebula = 50;
const { local, sync } = getBrowserInstance().storage;
if (getBrowserInstance().action) {
getBrowserInstance().action.onClicked.addListener(() => openOptions());
} else {
getBrowserInstance().browserAction.onClicked.addListener(() => openOptions());
}
// NOTE: not promise-based, because Chrome really doesn't like it, when there are multiple listeners
// need to return immediately, then call sendResponse manually, otherwise the other listener in offscreen.ts interferes
getBrowserInstance().runtime.onMessage.addListener((message: unknown, sender, sendResponse) => {
// Return early if this message isn't meant for the background script
if (message === undefined || message === null) return;
if ((typeof message !== 'string' && typeof message !== 'object') || (typeof message === 'object' && 'target' in message && message.target !== 'background')) {
return;
}
const keepAlive = setInterval(getBrowserInstance().runtime.getPlatformInfo, 25 * 1000);
let ret: Promise<any>;
try {
const msg = parseTypeObject<{ type: string, name?: string; }>(message);
if (__DEV__) console.log('Handling message', msg);
switch (msg.type) {
case BrowserMessage.INIT_PAGE:
ret = openChangelog();
break;
case BrowserMessage.LOAD_CREATORS:
ret = loadCreators();
break;
case BrowserMessage.GET_YTID:
ret = getYoutubeId(msg);
break;
case BrowserMessage.GET_VID:
ret = getNebulaVideo(msg);
break;
}
if (ret) {
ret.then(
res => {
if (__DEV__) console.log('Result for', msg.type, ':', res);
sendResponse({ res });
},
err => {
if (__DEV__) console.log('Error running', msg.type, ':', err);
sendResponse({ err });
},
);
return true;
}
} catch {
} finally {
clearInterval(keepAlive);
}
});
getBrowserInstance().runtime.onInstalled.addListener(async (details) => {
if (sync) {
await sync.set(await local.get());
await local.clear();
}
const { hiddenCreators, creatorSettings, playerSettings } = await getFromStorage({ hiddenCreators: [] as string[], creatorSettings: {} as Record<string, CreatorSettings>, playerSettings: {} as Partial<Settings> });
if (Array.isArray(hiddenCreators) && hiddenCreators.length) {
for (let c of hiddenCreators) {
if (c.endsWith('/')) c = c.slice(0, -1);
if (!(c in creatorSettings)) creatorSettings[c] = {};
creatorSettings[c].hideCompletely = true;
}
await setToStorage({ creatorSettings });
await (sync || local).remove('hiddenCreators');
}
repairPlayerSettings(playerSettings);
await setToStorage({ playerSettings });
if (details.reason === 'install') openOptions(true, 'show-changelogs');
});
let isHandlingChangelog = false;
const openChangelog = async () => {
if (isHandlingChangelog) return;
isHandlingChangelog = true;
const { showChangelogs: show, lastVersion: version } = await getFromStorage({ showChangelogs: true, lastVersion: '-1' });
console.debug({ show, version }, 'open changelogs?', show && version !== getBrowserInstance().runtime.getManifest().version);
const actualVersion = getBrowserInstance().runtime.getManifest().version;
if (show && version !== actualVersion) {
openOptions(false, 'show-changelogs');
await setToStorage({ lastVersion: actualVersion });
}
isHandlingChangelog = false;
};
let promise: Promise<Creator[]> = null;
const loadCreators = () => {
if (promise) return promise;
return promise = _loadCreators();
};
const getYoutubeId = async (message: { [key: string]: any; }) => {
await purgeCacheIfNecessary();
const { creator, title, nebula } = message;
const normalizedCreator = normalizeString(creator || '');
console.debug('creator:', creator, '\nnebula:', nebula, '\ntitle:', title);
if (!creator && !nebula) throw 'not enough information';
const creatorFinder = creator && nebula ?
(e: Creator) => e.name === creator || normalizeString(e.name) === normalizedCreator || e.nebula === nebula || e.nebulaAlt == nebula :
creator ?
(e: Creator) => e.name === creator || normalizeString(e.name) === normalizedCreator :
(e: Creator) => e.nebula === nebula || e.nebulaAlt == nebula;
try {
const creators = await loadCreators();
const uploads = creators.find(creatorFinder)?.uploads;
return creatorHasYTVideo(uploads, title, videoFetchYt);
} catch (err) {
console.error(err);
return Promise.reject(err);
}
};
const getNebulaVideo = async (message: { [key: string]: any; }): Promise<nebulavideo> => {
await purgeCacheIfNecessary();
const { channelID, channelName, videoTitle, channelNice } = message;
if (!channelID && !channelName) throw 'not enough information';
const creators = await loadCreators();
const matches = creators.filter(c => c.channel === channelID || c.name === channelName || c.name === channelNice || c.nebulaAlt === channelName);
console.debug('creators:', matches, '\nchannelID:', channelID, '\nvideoTitle:', videoTitle);
if (!matches) return;
for (const creator of matches) {
// try search the channel's newest videos locally
if (creator.nebula) {
try {
const video = await creatorHasNebulaVideo(creator.nebula, videoTitle, videoFetchNebula);
return {
is: 'video',
confidence: video.confidence,
link: video.video,
};
} catch (err) {
console.error(err);
}
}
}
for (const creator of matches) {
// try search the alternative channel's newest videos locally
if (creator.nebulaAlt && creator.nebula !== creator.nebulaAlt) {
try {
const video = await creatorHasNebulaVideo(creator.nebulaAlt, videoTitle, videoFetchNebula);
return {
is: 'video',
confidence: video.confidence,
link: video.video,
};
} catch (err) {
console.error(err);
}
}
}
// fall back to site-wide search
try {
const video = await existsNebulaVideo(videoTitle, videoFetchNebula);
return {
is: 'search',
confidence: video.confidence,
link: video.video,
};
} catch (err) {
console.error(err);
}
// last resort: link to channel
const creator = matches[0];
if (!creator.nebula && !creator.nebulaAlt) return;
return {
is: 'channel',
link: `https://${getBase()}/${creator.nebula || creator.nebulaAlt}`,
};
};
const openOptions = (active = true, ...args: string[]) => {
getBrowserInstance().tabs.create({
url: getBrowserInstance().runtime.getURL(`options.html#standalone ${args.join(' ')}`),
active,
});
};
(async () => {
const yt = (await getFromStorage({ youtube: false })).youtube;
if (!yt) return;
console.debug(await loadCreators());
// debug code
if (!__DEV__ || isChrome()) return;
(window as any).loadCreators = loadCreators;
Object.defineProperty(window, 'loadCreatorsPromise', {
get: () => promise, set(v) {
promise = v;
},
});
(window as any).getYoutubeId = getYoutubeId;
(window as any).getNebulaVideo = getNebulaVideo;
(window as any).openChangelog = openChangelog;
(window as any).openOptions = openOptions;
(window as any).purgeCache = purgeCache;
(window as any).parseTimeString = parseTimeString;
(window as any).toTimeString = toTimeString;
})();