2020-04-21 20:23:00 +00:00
|
|
|
/**
|
|
|
|
* Utilities to parse comma-separated key value pairs used in browser APIs.
|
|
|
|
* For example: "x=100,y=200,width=500,height=500"
|
|
|
|
*/
|
2021-11-10 16:54:51 +00:00
|
|
|
import { BrowserWindowConstructorOptions } from 'electron/main';
|
2020-04-21 20:23:00 +00:00
|
|
|
|
|
|
|
type RequiredBrowserWindowConstructorOptions = Required<BrowserWindowConstructorOptions>;
|
|
|
|
type IntegerBrowserWindowOptionKeys = {
|
|
|
|
[K in keyof RequiredBrowserWindowConstructorOptions]:
|
|
|
|
RequiredBrowserWindowConstructorOptions[K] extends number ? K : never
|
|
|
|
}[keyof RequiredBrowserWindowConstructorOptions];
|
|
|
|
|
|
|
|
// This could be an array of keys, but an object allows us to add a compile-time
|
|
|
|
// check validating that we haven't added an integer property to
|
|
|
|
// BrowserWindowConstructorOptions that this module doesn't know about.
|
|
|
|
const keysOfTypeNumberCompileTimeCheck: { [K in IntegerBrowserWindowOptionKeys] : true } = {
|
|
|
|
x: true,
|
|
|
|
y: true,
|
|
|
|
width: true,
|
|
|
|
height: true,
|
|
|
|
minWidth: true,
|
|
|
|
maxWidth: true,
|
|
|
|
minHeight: true,
|
|
|
|
maxHeight: true,
|
|
|
|
opacity: true
|
|
|
|
};
|
|
|
|
// Note `top` / `left` are special cases from the browser which we later convert
|
|
|
|
// to y / x.
|
2023-08-03 12:29:57 +00:00
|
|
|
const keysOfTypeNumber = new Set(['top', 'left', ...Object.keys(keysOfTypeNumberCompileTimeCheck)]);
|
2020-04-21 20:23:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Note that we only allow "0" and "1" boolean conversion when the type is known
|
|
|
|
* not to be an integer.
|
|
|
|
*
|
|
|
|
* The coercion of yes/no/1/0 represents best effort accordance with the spec:
|
|
|
|
* https://html.spec.whatwg.org/multipage/window-object.html#concept-window-open-features-parse-boolean
|
|
|
|
*/
|
|
|
|
type CoercedValue = string | number | boolean;
|
|
|
|
function coerce (key: string, value: string): CoercedValue {
|
2023-08-03 12:29:57 +00:00
|
|
|
if (keysOfTypeNumber.has(key)) {
|
2020-11-02 09:55:59 +00:00
|
|
|
return parseInt(value, 10);
|
2020-04-21 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (value) {
|
|
|
|
case 'true':
|
|
|
|
case '1':
|
|
|
|
case 'yes':
|
|
|
|
case undefined:
|
|
|
|
return true;
|
|
|
|
case 'false':
|
|
|
|
case '0':
|
|
|
|
case 'no':
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 22:46:54 +00:00
|
|
|
export function parseCommaSeparatedKeyValue (source: string) {
|
2020-04-21 20:23:00 +00:00
|
|
|
const parsed = {} as { [key: string]: any };
|
|
|
|
for (const keyValuePair of source.split(',')) {
|
|
|
|
const [key, value] = keyValuePair.split('=').map(str => str.trim());
|
2021-04-19 22:46:54 +00:00
|
|
|
if (key) { parsed[key] = coerce(key, value); }
|
2020-04-21 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
2021-04-19 22:46:54 +00:00
|
|
|
return parsed;
|
2020-04-21 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function parseWebViewWebPreferences (preferences: string) {
|
2021-04-19 22:46:54 +00:00
|
|
|
return parseCommaSeparatedKeyValue(preferences);
|
2020-04-21 20:23:00 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 01:12:40 +00:00
|
|
|
const allowedWebPreferences = ['zoomFactor', 'nodeIntegration', 'javascript', 'contextIsolation', 'webviewTag'] as const;
|
2020-04-21 20:23:00 +00:00
|
|
|
type AllowedWebPreference = (typeof allowedWebPreferences)[number];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parses a feature string that has the format used in window.open().
|
|
|
|
*/
|
2021-04-19 22:46:54 +00:00
|
|
|
export function parseFeatures (features: string) {
|
|
|
|
const parsed = parseCommaSeparatedKeyValue(features);
|
2020-04-21 20:23:00 +00:00
|
|
|
|
|
|
|
const webPreferences: { [K in AllowedWebPreference]?: any } = {};
|
|
|
|
allowedWebPreferences.forEach((key) => {
|
|
|
|
if (parsed[key] === undefined) return;
|
|
|
|
webPreferences[key] = parsed[key];
|
|
|
|
delete parsed[key];
|
|
|
|
});
|
|
|
|
|
|
|
|
if (parsed.left !== undefined) parsed.x = parsed.left;
|
|
|
|
if (parsed.top !== undefined) parsed.y = parsed.top;
|
|
|
|
|
|
|
|
return {
|
2020-11-10 17:06:03 +00:00
|
|
|
options: parsed as Omit<BrowserWindowConstructorOptions, 'webPreferences'>,
|
2021-04-19 22:46:54 +00:00
|
|
|
webPreferences
|
2020-04-21 20:23:00 +00:00
|
|
|
};
|
|
|
|
}
|