This repository has been archived by the owner on Nov 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync-on-page-visit.ts
84 lines (66 loc) · 3.12 KB
/
sync-on-page-visit.ts
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
// This module handles syncs on page visits. Whenever the user visits either
// Leetify or Steam GCPD, we want to run a sync (if enabled in the options,
// and it's been a bit since the last sync).
import { getOptionDefaults, LEETIFY_FRONTEND_URL } from './constants';
import { MatchSync } from './match-sync';
import { SyncStorageKey } from '../types/enums';
class SyncOnPageVisit {
protected lastSyncOnPageVisit: Date | null = null;
protected syncOnVisitGcpdCachePromise: Promise<boolean>;
protected syncOnVisitLeetifyCachePromise: Promise<boolean>;
public constructor() {
this.onTabUpdated = this.onTabUpdated.bind(this);
const defaults = getOptionDefaults();
const onVisitsPromise: Promise<{
[SyncStorageKey.OPTION_SYNC_ON_VISIT_GCPD]?: boolean;
[SyncStorageKey.OPTION_SYNC_ON_VISIT_LEETIFY]?: boolean;
}> = chrome.storage.sync.get([
SyncStorageKey.OPTION_SYNC_ON_VISIT_GCPD,
SyncStorageKey.OPTION_SYNC_ON_VISIT_LEETIFY,
]);
this.syncOnVisitLeetifyCachePromise = onVisitsPromise.then((items) => items[SyncStorageKey.OPTION_SYNC_ON_VISIT_LEETIFY] ?? defaults[SyncStorageKey.OPTION_SYNC_ON_VISIT_LEETIFY]);
this.syncOnVisitGcpdCachePromise = onVisitsPromise.then((items) => items[SyncStorageKey.OPTION_SYNC_ON_VISIT_GCPD] ?? defaults[SyncStorageKey.OPTION_SYNC_ON_VISIT_GCPD]);
}
public async applyListener(): Promise<void> {
const [
shouldSyncOnVisitGcpd,
shouldSyncOnVisitLeetify,
] = await Promise.all([
this.syncOnVisitGcpdCachePromise,
this.syncOnVisitLeetifyCachePromise,
]);
// this does not cause duplicate listeners
if (shouldSyncOnVisitGcpd || shouldSyncOnVisitLeetify) this.enable();
else this.disable();
}
public updateSyncOnVisitLeetifyCache(shouldSync: boolean): void {
this.syncOnVisitLeetifyCachePromise = Promise.resolve(shouldSync);
}
public updateSyncOnVisitGcpdCache(shouldSync: boolean): void {
this.syncOnVisitGcpdCachePromise = Promise.resolve(shouldSync);
}
protected async onTabUpdated(tabId: number, changeInfo: { status?: string }, tab: chrome.tabs.Tab): Promise<void> {
if (!tab.url) return; // we don't have permissions for this tab
if (tab.status !== 'complete') return; // the page has not finished loading
if (changeInfo.status !== 'complete') return; // the event was fired for another reason than the page completing loading
// run sync max every 5 minutes from page visits
const fiveMinutesAgo = +new Date() - 5 * 60 * 1000;
if (this.lastSyncOnPageVisit && +this.lastSyncOnPageVisit >= fiveMinutesAgo) return;
this.lastSyncOnPageVisit = new Date();
if (tab.url.startsWith(`${LEETIFY_FRONTEND_URL}/`) || tab.url === LEETIFY_FRONTEND_URL) {
if (await this.syncOnVisitLeetifyCachePromise) await MatchSync.run();
} else if (/^https:\/\/steamcommunity\.com\/(id\/[^/]+|profiles\/\d+)\/gcpd\/730/.test(tab.url)) {
if (await this.syncOnVisitGcpdCachePromise) await MatchSync.run();
}
}
protected enable(): void {
chrome.tabs.onUpdated.addListener(this.onTabUpdated);
}
protected disable(): void {
chrome.tabs.onUpdated.removeListener(this.onTabUpdated);
}
}
const singleton = new SyncOnPageVisit();
export {
singleton as SyncOnPageVisit,
};