Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/plugins/default-browser-emulator/lib/utils/parseNavigatorPlugins.ts
1030 views
1
export default function parseNavigatorPlugins(navigator: any) {
2
const mimesJson = readDomOutput(navigator.mimeTypes);
3
const mimeTypes: any[] = Object.entries(mimesJson)
4
.filter(x => x[0].match(/\d+/))
5
.map(x => x[1]);
6
7
const firstMimeType = (mimeTypes[0] as any).type;
8
const mimesListHasRefForTypeEntry =
9
typeof mimesJson[firstMimeType] === 'string' &&
10
(mimesJson[firstMimeType] as string).startsWith('REF: ');
11
12
const pluginJson = readDomOutput(navigator.plugins);
13
const plugins: any[] = Object.entries(pluginJson)
14
.filter(x => x[0].match(/\d+/))
15
.map(x => x[1]);
16
17
for (const plugin of plugins) {
18
delete plugin.length;
19
for (const [pluginKey, pluginProp] of Object.entries(plugin)) {
20
if (pluginKey.match(/\d+/)) {
21
const mimeType = (pluginProp as any).type as string;
22
delete plugin[pluginKey];
23
delete plugin[mimeType];
24
mimeTypes.find(x => x.type === mimeType).__pluginName = plugin.name;
25
}
26
}
27
}
28
29
return {
30
mimeTypes,
31
mimesListHasRefForTypeEntry,
32
plugins,
33
};
34
}
35
36
function readDomOutput(entry) {
37
if (entry._$type === 'object') {
38
const obj = {};
39
const props = Object.entries(entry);
40
for (const [prop, value] of props) {
41
if (prop.startsWith('_$')) continue;
42
obj[prop] = readDomOutput(value);
43
}
44
return obj;
45
}
46
if (entry._$value !== undefined) {
47
return entry._$value;
48
}
49
return entry;
50
}
51
52