summaryrefslogtreecommitdiff
path: root/source/options-storage.ts
blob: 6330ac8d845217807b42b7275cd358e724986082 (plain) (blame)
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
import OptionsSync from 'webext-options-sync';
import {isBackgroundPage} from 'webext-detect-page';
import {getAdditionalPermissions} from 'webext-additional-permissions';

export interface RGHOptions {
	customCSS: string;
	personalToken: string;
	logging: boolean;
	minimizedUsers: string;
	[featureName: string]: string | boolean;
}

function featureWasRenamed(from: string, to: string): any { // TODO: any should probably be `Migration` after `webext-options-sync`'s types are fixed
	return (options: RGHOptions) => {
		if (typeof options[`feature:${from}`] === 'boolean') {
			options[`feature:${to}`] = options[`feature:${from}`];
		}
	};
}

const defaults: RGHOptions = {
	customCSS: '',
	personalToken: '',
	logging: false,
	minimizedUsers: ''
};

// This variable is replaced at build time with the list
for (const feature of __featuresList__) {
	defaults[`feature:${feature}`] = true;
}

const migrations = [
	featureWasRenamed('make-discussion-sidebar-sticky', 'sticky-discussion-sidebar'), // Merged on August 1st

	// Removed features will be automatically removed from the options as well
	OptionsSync.migrations.removeUnused
];

// Keep this function "dumb". Don't move more "smart" domain selection logic in here
function getStorageName(host: string): string {
	if (/(^|\.)github\.com$/.test(host)) {
		return 'options';
	}

	return `options-${host}`;
}

function getOptions(host: string): OptionsSync<RGHOptions> {
	return new OptionsSync({storageName: getStorageName(host), migrations, defaults});
}

// This should return the options for the current domain or, if called from an extension page, for `github.com`
export default getOptions(location.protocol.startsWith('http') ? location.host : 'github.com');

export async function getAllOptions(): Promise<Map<string, OptionsSync<RGHOptions>>> {
	const optionsByDomain = new Map<string, OptionsSync<RGHOptions>>();
	optionsByDomain.set('github.com', getOptions('github.com'));

	const {origins} = await getAdditionalPermissions();
	for (const origin of origins) {
		const {host} = new URL(origin);
		optionsByDomain.set(host, getOptions(host));
	}

	return optionsByDomain;
}

async function initializeAllOptions(): Promise<void> {
	// Run migrations for every domain
	const {origins} = await getAdditionalPermissions();
	for (const origin of origins) {
		getOptions(new URL(origin).host);
	}

	// Add new domains
	browser.permissions.onAdded!.addListener(({origins}) => {
		if (origins) {
			for (const origin of origins) {
				getOptions(new URL(origin).host);
			}
		}
	});

	// Remove old domains
	browser.permissions.onRemoved!.addListener(({origins}) => {
		if (origins) {
			const optionKeysToRemove = origins.map(origin => getStorageName(new URL(origin).host));
			browser.storage.sync.remove(optionKeysToRemove);
		}
	});
}

if (isBackgroundPage()) {
	initializeAllOptions();
}