summaryrefslogtreecommitdiff
path: root/data/extensions/jsr@javascriptrestrictor/fp_levels.js
blob: 91f9aa921c9e76cc696f0e6cbe554ae267767a10 (plain)
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
/** \file
 * \brief FPD module initial configuration 
 *
 *  \author Copyright (C) 2021  Marek Salon
 *  \author Copyright (C) 2023  Martin Zmitko
 *
 *  \license SPDX-License-Identifier: GPL-3.0-or-later
 */
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

/** \file
 *
 * \brief This file is part of Fingerprint Detector (FPD) and contains the function for loading configuration files.
 *
 * \ingroup FPD
 */

//DEF_FPD_FILES_S
var fp_config_files = ["groups-lvl_0", "groups-lvl_1", "wrappers-lvl_0_1"]
//DEF_FPD_FILES_E

/**
 *  Object containing parsed input from JSON configuration files of FPD module.
 */
var fp_levels = {};

/// \cond (Exclude this section from the doxygen documentation. If this section is not excluded, it is documented as a separate function.)
{
	// parse input files into fp_levels for each level, generate wrapping code and initialize FPD module
	let loadFpdConfig = async () => {
		for (let file of fp_config_files) {
			try {
				let config = JSON.parse(await readFile(browser.runtime.getURL(`fp_config/${file}.json`)));
				let file_splitted = file.split("-");
				let file_levels = file_splitted[1].split("_").filter(x => x != 'lvl');
				fp_levels[file_splitted[0]] = fp_levels[file_splitted[0]] || {};
				for (let level of file_levels) {
					fp_levels[file_splitted[0]][level] = config;
				}
			}
			catch (e) {
				console.error(e);
			}
		}

		// merge duplicit entries of the same resource to be wrapped only once
		let mergeWrappers = (sameResources) => {
			let mergeGroups = () => {
				let accArray = [];
				for (let resource of sameResources) {
					accArray.push(...resource.groups);
				}
				return accArray;
			}

			return {
				resource: sameResources[0].resource,
				type: sameResources[0].type,
				groups: mergeGroups()
			}
		}

		fp_levels.page_wrappers = [];
		for (let level in fp_levels.wrappers) {
			let tmpWrappers = {};
			for (let wrapper of fp_levels.wrappers[level]) {
				if (!Object.keys(tmpWrappers).includes(wrapper.resource)) {
					let sameResources = fp_levels.wrappers[level].filter(x => x.resource == wrapper.resource);
					tmpWrappers[wrapper.resource] = mergeWrappers(sameResources);
				}
			}
			fp_levels.wrappers[level] = Object.values(tmpWrappers);

			// Save FPD wrappers for page configuration
			fp_levels.page_wrappers[level] = fp_levels.wrappers[level].map(w => [
				// Wrapped object name
				w.resource,
				// Wrapped object type (0 - function, 1 - property)
				w.type === "property" ? 1 : 0,
				// Array specifying property types to wrap (getter, setter or both)
				Array.from(new Set(w.groups.map(x => x.property != undefined ? x.property : "get"))),
				// Specify if arguments should be sent for evaulation
				w.groups.some(group => "arguments" in group) ? 1 : 0,
			]);
		}

		// initialize FPD module (background script and event listeners)
		initFpd();

		fp_levels_initialised = true;
		if (levels_initialised) { // Wait for both levels_initialised and fp_levels_initialised
			var orig_levels_updated_callbacks = levels_updated_callbacks;
			levels_updated_callbacks = [];
			orig_levels_updated_callbacks.forEach((it) => it());
			await updateUserScripts();
		}
	}
	loadFpdConfig();
}
/// \endcond