Skip to content

Commit

Permalink
improve compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Jan 12, 2023
1 parent 71986f3 commit ff07fb5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 186 deletions.
8 changes: 2 additions & 6 deletions scripts/build-funcs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ import { checkCompatibility } from '../src/helpers';
* @returns {string}
*/
function prepareCompatibilityGuard() {
const compatibilityGuard = checkCompatibility;
const compatibilityGuardString = attachDependencies(compatibilityGuard);
return `${compatibilityGuardString}\n${compatibilityGuard.name}()`;
return `${checkCompatibility.toString()}\n${checkCompatibility.name}()`;
}

/**
Expand All @@ -40,10 +38,8 @@ function prepareCompatibilityGuard() {
* @returns {string}
*/
const getScriptletFunctionsString = () => {
const compatibilityGuardString = prepareCompatibilityGuard();

function wrapInFunc(name, code) {
return `function ${name}(source, args){\n${compatibilityGuardString}\n${code}\n}`;
return `function ${name}(source, args){\n${prepareCompatibilityGuard()}\n${code}\n}`;
}
// we require scriptlets list dynamically, because scriptletsList file can be not built in the
// moment of this script execution
Expand Down
25 changes: 25 additions & 0 deletions src/helpers/check-compatibility.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Checks if current browser is supported
* To be injected to every scriptlet at build time
*
* @throws error on unsupported browser
*/
export function checkCompatibility() {
const isBrowserSupported = () => {
// arr.every() is not used because missing global method
// would throw ReferenceError when added to array
return typeof fetch !== 'undefined'
&& typeof Proxy !== 'undefined'
&& typeof Reflect !== 'undefined'
&& typeof Response !== 'undefined'
&& typeof Array.prototype.includes !== 'undefined'
&& typeof String.prototype.includes !== 'undefined'
&& typeof String.prototype.startsWith !== 'undefined'
&& typeof String.prototype.endsWith !== 'undefined'
&& typeof Element.prototype.attachShadow !== 'undefined';
};

if (!isBrowserSupported()) {
throw new Error('Browser is not supported by AdGuard Scriptlets.');
}
}
2 changes: 1 addition & 1 deletion src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export * from './parse-flags';
export * from './parse-keyword-value';
export * from './random-id';
export * from './throttle';
export * from './is-browser-supported';
export * from './check-compatibility';
179 changes: 0 additions & 179 deletions src/helpers/is-browser-supported.js

This file was deleted.

0 comments on commit ff07fb5

Please sign in to comment.