Path: blob/main/plugins/default-browser-emulator/injected-scripts/navigator.plugins.ts
1029 views
function installNamedNodeItems(1nativeObj: PluginArray | MimeTypeArray,2list: (Plugin | MimeType)[],3prop: 'type' | 'name',4) {5const props = [];6for (let i = 0; i < list.length; i += 1) {7const item = list[i];8props.push(String(i));9Object.defineProperty(nativeObj, i, {10value: item,11enumerable: true,12writable: false,13configurable: true,14});15}16for (const item of list) {17props.push(item[prop]);18Object.defineProperty(nativeObj, item[prop], {19value: item,20enumerable: false,21writable: false,22configurable: true,23});24}25return props;26}2728function createNamedNodeMap(29protoClass: PluginArray | MimeTypeArray,30list: (Plugin | MimeType)[],31prop: 'type' | 'name',32) {33const nativeObj = Object.create(protoClass) as PluginArray | MimeTypeArray;3435installNamedNodeItems(nativeObj, list, prop);3637proxyFunction(38nativeObj,39'item',40(target, thisArg, argArray) => {41if (!argArray || !argArray.length) return ProxyOverride.callOriginal;42return thisArg[argArray[0]];43},44true,45);46proxyFunction(47nativeObj,48'namedItem',49(target, thisArg, argArray) => {50if (!argArray || !argArray.length) return ProxyOverride.callOriginal;51return thisArg[argArray[0]];52},53true,54);55if (nativeObj instanceof PluginArray) {56proxyFunction(nativeObj, 'refresh', () => undefined, true);57}58// @ts-ignore - seems to be missing from ts def59// proxyFunction(nativeObj, Symbol.iterator, () => [...list], true);60proxyGetter(nativeObj, 'length', () => list.length, true);61return nativeObj;62}6364function createMime(fakeMime) {65const mime: MimeType = Object.create(MimeType.prototype);66proxyGetter(mime, 'type', () => fakeMime.type, true);67proxyGetter(mime, 'suffixes', () => fakeMime.suffixes, true);68proxyGetter(mime, 'description', () => fakeMime.description, true);69proxyGetter(mime, 'enabledPlugin', () => navigator.plugins[fakeMime.__pluginName], true);70return mime;71}7273const mimeList: MimeType[] = args.mimeTypes.map(createMime);74const mimes = createNamedNodeMap(MimeTypeArray.prototype, mimeList, 'type') as MimeTypeArray;7576const pluginList: Plugin[] = args.plugins.map(fakeP => {77let plugin: Plugin = Object.create(Plugin.prototype);7879const pluginMimes = args.mimeTypes.filter(m => m.__pluginName === fakeP.name);80const mimeProps = installNamedNodeItems(plugin, pluginMimes.map(createMime), 'type');8182proxyGetter(plugin, 'name', () => fakeP.name, true);83proxyGetter(plugin, 'description', () => fakeP.description, true);84proxyGetter(plugin, 'filename', () => fakeP.filename, true);85proxyGetter(plugin, 'length', () => pluginMimes.length, true);86proxyFunction(87plugin,88'item',89(target, thisArg, argArray) => {90if (!argArray || !argArray.length) return ProxyOverride.callOriginal;91const entry = pluginMimes[argArray[0]];92if (entry) return createMime(entry);93},94true,95);96proxyFunction(97plugin,98'namedItem',99(_, __, argArray) => {100if (!argArray || !argArray.length) return ProxyOverride.callOriginal;101const match = pluginMimes.find(x => x.type === argArray[0]);102if (match) return createMime(match);103},104true,105);106107plugin = new Proxy(plugin, {108get(target, p) {109if (mimeProps.includes(p)) {110let fakeMime = pluginMimes.find(x => x.type === p);111if (new RegExp(/^\d+$/).test(String(p))) {112fakeMime = pluginMimes[p];113}114return createMime(fakeMime);115}116117return target[p];118},119});120121return plugin;122});123124const navigatorPlugins = createNamedNodeMap(125PluginArray.prototype,126pluginList,127'name',128) as PluginArray;129130proxyGetter(navigator, 'mimeTypes', () => mimes, true);131proxyGetter(navigator, 'plugins', () => navigatorPlugins, true);132133134