Skip to content

Commit

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

/**
Expand Down
48 changes: 19 additions & 29 deletions src/helpers/check-compatibility.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,3 @@
/**
* Checks if current browser is supported
* To be injected to every scriptlet at build time
*
* isBrowserSupported is included to simplify injection
* by skipping dependencies attachment
*
* @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.');
}
}

/**
* Checks if current browser is supported
* For usage in tests
Expand All @@ -46,3 +17,22 @@ export const isBrowserSupported = () => {
&& typeof String.prototype.endsWith !== 'undefined'
&& typeof Element.prototype.attachShadow !== 'undefined';
};

/**
* Checks if current browser is supported
* To be injected to every scriptlet at build time
*
* isBrowserSupported is included to simplify injection
* by skipping dependencies attachment
*
* @throws error on unsupported browser
*/
export function checkCompatibility() {
if (!isBrowserSupported()) {
throw new Error('Browser is not supported by AdGuard Scriptlets.');
}
}

checkCompatibility.injections = [
isBrowserSupported,
];

0 comments on commit 94a4d06

Please sign in to comment.