Skip to content

Commit

Permalink
merge master, resolve conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
slavaleleka committed May 16, 2023
2 parents 0efb80e + 6af803c commit ef04555
Show file tree
Hide file tree
Showing 59 changed files with 265 additions and 251 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@adguard/scriptlets",
"version": "1.9.14",
"version": "1.9.16",
"description": "AdGuard's JavaScript library of Scriptlets and Redirect resources",
"scripts": {
"build": "babel-node bundler.js",
Expand Down
5 changes: 1 addition & 4 deletions scripts/check-sources-updates.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ const getDiff = (oldList, newList) => {
removed: [],
};

diff.removed = oldList.filter((item) => (
!newList.includes(item)
&& item.indexOf(REMOVED_MARKER) === -1
));
diff.removed = oldList.filter((item) => !newList.includes(item) && !item.includes(REMOVED_MARKER));
diff.added = newList.filter((item) => !oldList.includes(item));

return (diff.removed.length || diff.added.length) ? diff : null;
Expand Down
23 changes: 11 additions & 12 deletions src/helpers/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ const validateRemoveAttrClassArgs = (parsedArgs) => {
const lastArg = restArgs.pop();
let applying;
// check the last parsed arg for matching possible 'applying' vale
if (REMOVE_ATTR_CLASS_APPLYING.some((el) => lastArg.indexOf(el) > -1)) {
if (REMOVE_ATTR_CLASS_APPLYING.some((el) => lastArg.includes(el))) {
applying = lastArg;
} else {
restArgs.push(lastArg);
Expand Down Expand Up @@ -160,19 +160,18 @@ export const convertUboScriptletToAdg = (rule) => {
const domains = getBeforeRegExp(rule, validator.UBO_SCRIPTLET_MASK_REG);
const mask = rule.match(validator.UBO_SCRIPTLET_MASK_REG)[0];
let template;
if (mask.indexOf('@') > -1) {
if (mask.includes('@')) {
template = ADGUARD_SCRIPTLET_EXCEPTION_TEMPLATE;
} else {
template = ADGUARD_SCRIPTLET_TEMPLATE;
}
const argsStr = getStringInBraces(rule);
let parsedArgs = splitArgs(argsStr);
const scriptletName = parsedArgs[0].indexOf(UBO_SCRIPTLET_JS_ENDING) > -1
const scriptletName = parsedArgs[0].includes(UBO_SCRIPTLET_JS_ENDING)
? `ubo-${parsedArgs[0]}`
: `ubo-${parsedArgs[0]}${UBO_SCRIPTLET_JS_ENDING}`;

if (((REMOVE_ATTR_ALIASES.indexOf(scriptletName) > -1)
|| (REMOVE_CLASS_ALIASES.indexOf(scriptletName) > -1))) {
if (REMOVE_ATTR_ALIASES.includes(scriptletName) || REMOVE_CLASS_ALIASES.includes(scriptletName)) {
parsedArgs = validateRemoveAttrClassArgs(parsedArgs);
}

Expand Down Expand Up @@ -205,7 +204,7 @@ export const convertUboScriptletToAdg = (rule) => {
*/
export const convertAbpSnippetToAdg = (rule) => {
const SEMICOLON_DIVIDER = /;(?=(?:(?:[^"]*"){2})*[^"]*$)/g;
const mask = rule.indexOf(validator.ABP_SCRIPTLET_MASK) > -1
const mask = rule.includes(validator.ABP_SCRIPTLET_MASK)
? validator.ABP_SCRIPTLET_MASK
: validator.ABP_SCRIPTLET_EXCEPTION_MASK;
const template = mask === validator.ABP_SCRIPTLET_MASK
Expand Down Expand Up @@ -315,7 +314,7 @@ export const convertAdgScriptletToUbo = (rule) => {
|| parsedParams[0] === ADG_PREVENT_FETCH_EMPTY_STRING)) {
preparedParams = [UBO_NO_FETCH_IF_WILDCARD];
} else if ((parsedName === ADG_REMOVE_ATTR_NAME || parsedName === ADG_REMOVE_CLASS_NAME)
&& parsedParams[1] && parsedParams[1].indexOf(COMMA_SEPARATOR) > -1) {
&& parsedParams[1] && parsedParams[1].includes(COMMA_SEPARATOR)) {
preparedParams = [
parsedParams[0],
replaceAll(parsedParams[1], COMMA_SEPARATOR, ESCAPED_COMMA_SEPARATOR),
Expand All @@ -333,7 +332,7 @@ export const convertAdgScriptletToUbo = (rule) => {
return { name, aliases };
})
.find((el) => (el.name === parsedName
|| el.aliases.indexOf(parsedName) >= 0));
|| el.aliases.includes(parsedName)));

const { aliases } = adgScriptletObject;

Expand All @@ -345,7 +344,7 @@ export const convertAdgScriptletToUbo = (rule) => {
if (uboAlias) {
const mask = rule.match(ADGUARD_SCRIPTLET_MASK_REG)[0];
let template;
if (mask.indexOf('@') > -1) {
if (mask.includes('@')) {
template = UBO_SCRIPTLET_EXCEPTION_TEMPLATE;
} else {
template = UBO_SCRIPTLET_TEMPLATE;
Expand Down Expand Up @@ -445,11 +444,11 @@ export const isValidScriptletRule = (ruleText) => {
*/
const getMarkerData = (modifiers, redirectsData, rule) => {
let marker;
let index = modifiers.findIndex((m) => m.indexOf(redirectsData.redirectRuleMarker) > -1);
let index = modifiers.findIndex((m) => m.includes(redirectsData.redirectRuleMarker));
if (index > -1) {
marker = redirectsData.redirectRuleMarker;
} else {
index = modifiers.findIndex((m) => m.indexOf(redirectsData.redirectMarker) > -1);
index = modifiers.findIndex((m) => m.includes(redirectsData.redirectMarker));
if (index > -1) {
marker = redirectsData.redirectMarker;
} else {
Expand Down Expand Up @@ -501,7 +500,7 @@ export const convertAbpRedirectToAdg = (rule) => {
const abpModifiers = validator.parseModifiers(rule);
const adgModifiers = abpModifiers
.map((modifier) => {
if (modifier.indexOf(validator.REDIRECT_RULE_TYPES.ABP.redirectMarker) > -1) {
if (modifier.includes(validator.REDIRECT_RULE_TYPES.ABP.redirectMarker)) {
const abpName = substringAfter(
modifier,
validator.REDIRECT_RULE_TYPES.ABP.redirectMarker,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/create-on-error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function createOnErrorHandler(rid) {
// eslint-disable-next-line consistent-return
const nativeOnError = window.onerror;
return function onError(error, ...args) {
if (typeof error === 'string' && error.indexOf(rid) !== -1) {
if (typeof error === 'string' && error.includes(rid)) {
return true;
}
if (nativeOnError instanceof Function) {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export const hit = (source) => {
const AG_SCRIPTLET_MARKER = '#%#//';
const UBO_SCRIPTLET_MARKER = '##+js';
let ruleStartIndex;
if (source.ruleText.indexOf(AG_SCRIPTLET_MARKER) > -1) {
if (source.ruleText.includes(AG_SCRIPTLET_MARKER)) {
ruleStartIndex = source.ruleText.indexOf(AG_SCRIPTLET_MARKER);
} else if (source.ruleText.indexOf(UBO_SCRIPTLET_MARKER) > -1) {
} else if (source.ruleText.includes(UBO_SCRIPTLET_MARKER)) {
ruleStartIndex = source.ruleText.indexOf(UBO_SCRIPTLET_MARKER);
}
// delete all domains from ruleText and leave just rule part
Expand Down
34 changes: 0 additions & 34 deletions src/helpers/object-utils.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,3 @@
/**
* Converts object to array of pairs.
* Object.entries() polyfill because it is not supported by IE
* https://caniuse.com/?search=Object.entries
*
* @param {Object} object arbitrary object
* @returns {Array} array of pairs
*/
export const getObjectEntries = (object) => {
const keys = Object.keys(object);
const entries = [];
keys.forEach((key) => entries.push([key, object[key]]));
return entries;
};

/**
* Converts array of pairs to object.
* Object.fromEntries() polyfill because it is not supported by IE
* https://caniuse.com/?search=Object.fromEntries
*
* @param {Array} entries - array of pairs
* @returns {Object} result object
*/
export const getObjectFromEntries = (entries) => {
const output = entries
.reduce((acc, el) => {
const key = el[0];
const value = el[1];
acc[key] = value;
return acc;
}, {});
return output;
};

/**
* Checks whether the obj is an empty object
*
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/parse-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ export const parseFlags = (flags) => {

const passedFlags = flags.trim()
.split(FLAGS_DIVIDER)
.filter((f) => VALID_FLAGS.indexOf(f) !== -1);
.filter((f) => VALID_FLAGS.includes(f));

return {
ASAP: ASAP_FLAG,
COMPLETE: COMPLETE_FLAG,
STAY: STAY_FLAG,
hasFlag(flag) {
return passedFlags.indexOf(flag) !== -1;
return passedFlags.includes(flag);
},
};
};
6 changes: 2 additions & 4 deletions src/helpers/prevent-window-open-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import {
trueFunc,
} from './noop-utils';
import {
startsWith,
endsWith,
substringBefore,
substringAfter,
} from './string-utils';
Expand All @@ -16,11 +14,11 @@ export const handleOldReplacement = (replacement) => {
result = noopFunc;
} else if (replacement === 'trueFunc') {
result = trueFunc;
} else if (replacement.indexOf('=') > -1) {
} else if (replacement.includes('=')) {
// We should return noopFunc instead of window.open
// but with some property if website checks it (examples 5, 6)
// https://github.com/AdguardTeam/Scriptlets/issues/71
const isProp = startsWith(replacement, '{') && endsWith(replacement, '}');
const isProp = replacement.startsWith('{') && replacement.endsWith('}');
if (isProp) {
const propertyPart = replacement.slice(1, -1);
const propertyName = substringBefore(propertyPart, '=');
Expand Down
5 changes: 2 additions & 3 deletions src/helpers/request-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { toRegExp, isValidStrPattern } from './string-utils';
import { getObjectFromEntries } from './object-utils';

/**
* Returns array of request props that are supported by fetch/xhr scriptlets.
Expand Down Expand Up @@ -37,7 +36,7 @@ export const getRequestData = (request) => {
const value = request[key];
return [key, value];
});
return getObjectFromEntries(entries);
return Object.fromEntries(entries);
};

/**
Expand Down Expand Up @@ -110,7 +109,7 @@ export const parseMatchProps = (propsToMatchStr) => {
const dividerInd = prop.indexOf(PAIRS_MARKER);

const key = prop.slice(0, dividerInd);
const hasLegalMatchProp = LEGAL_MATCH_PROPS.indexOf(key) !== -1;
const hasLegalMatchProp = LEGAL_MATCH_PROPS.includes(key);

if (hasLegalMatchProp) {
const value = prop.slice(dividerInd + 1);
Expand Down
14 changes: 6 additions & 8 deletions src/helpers/script-source-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { startsWith } from './string-utils';

/**
* Determines if type of script is inline or injected
* and when it's one of them then return true, otherwise false
Expand All @@ -13,8 +11,8 @@ export const shouldAbortInlineOrInjectedScript = (stackMatch, stackTrace) => {
const INLINE_SCRIPT_STRING = 'inlineScript';
const INJECTED_SCRIPT_STRING = 'injectedScript';
const INJECTED_SCRIPT_MARKER = '<anonymous>';
const isInlineScript = (stackMatch) => stackMatch.indexOf(INLINE_SCRIPT_STRING) > -1;
const isInjectedScript = (stackMatch) => stackMatch.indexOf(INJECTED_SCRIPT_STRING) > -1;
const isInlineScript = (stackMatch) => stackMatch.includes(INLINE_SCRIPT_STRING);
const isInjectedScript = (stackMatch) => stackMatch.includes(INJECTED_SCRIPT_STRING);

if (!(isInlineScript(stackMatch) || isInjectedScript(stackMatch))) {
return false;
Expand All @@ -39,15 +37,15 @@ export const shouldAbortInlineOrInjectedScript = (stackMatch, stackTrace) => {
const getStackTraceURL = /(.*?@)?(\S+)(:\d+):\d+\)?$/.exec(line);
if (getStackTraceURL) {
let stackURL = getStackTraceURL[2];
if (startsWith(stackURL, '(')) {
if (stackURL?.startsWith('(')) {
stackURL = stackURL.slice(1);
}
if (startsWith(stackURL, INJECTED_SCRIPT_MARKER)) {
if (stackURL?.startsWith(INJECTED_SCRIPT_MARKER)) {
stackURL = INJECTED_SCRIPT_STRING;
let stackFunction = getStackTraceURL[1] !== undefined
? getStackTraceURL[1].slice(0, -1)
: line.slice(0, getStackTraceURL.index).trim();
if (startsWith(stackFunction, 'at')) {
if (stackFunction?.startsWith('at')) {
stackFunction = stackFunction.slice(2).trim();
}
stack = `${stackFunction} ${stackURL}`.trim();
Expand All @@ -65,7 +63,7 @@ export const shouldAbortInlineOrInjectedScript = (stackMatch, stackTrace) => {
return true;
}
if (isInjectedScript(stackMatch)
&& startsWith(stackLines[index], INJECTED_SCRIPT_STRING)) {
&& stackLines[index].startsWith(INJECTED_SCRIPT_STRING)) {
return true;
}
}
Expand Down
51 changes: 14 additions & 37 deletions src/helpers/string-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isEmptyObject, getObjectEntries } from './object-utils';
import { isEmptyObject } from './object-utils';
import {
nativeIsFinite,
nativeIsNaN,
Expand Down Expand Up @@ -48,7 +48,14 @@ export const toRegExp = (input = '') => {
if (input[0] === FORWARD_SLASH && input[input.length - 1] === FORWARD_SLASH) {
return new RegExp(input.slice(1, -1));
}
const escaped = input.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

const escaped = input
// remove quotes' escapes for cases where scriptlet rule argument has own escaped quotes
// e.g #%#//scriptlet('prevent-setTimeout', '.css(\'display\',\'block\');')
.replace(/\\'/g, '\'')
.replace(/\\"/g, '"')
// escape special characters for following RegExp construction
.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
return new RegExp(escaped);
};

Expand Down Expand Up @@ -87,36 +94,6 @@ export const getBeforeRegExp = (str, rx) => {
return str.substring(0, index);
};

/**
* Checks whether the string starts with the substring
*
* @deprecated use String.prototype.startsWith() instead. AG-18883
*
* @param {string} str full string
* @param {string} prefix substring
* @returns {boolean} if string start with the substring
*/
export const startsWith = (str, prefix) => {
// if str === '', (str && false) will return ''
// that's why it has to be !!str
return !!str && str.indexOf(prefix) === 0;
};

/**
* Checks whether the string ends with the substring
*
* @deprecated use String.prototype.endsWith() instead. AG-18883
*
* @param {string} str full string
* @param {string} ending substring
* @returns {boolean} string ends with the substring
*/
export const endsWith = (str, ending) => {
// if str === '', (str && false) will return ''
// that's why it has to be !!str
return !!str && str.lastIndexOf(ending) === str.length - ending.length;
};

export const substringAfter = (str, separator) => {
if (!str) {
return str;
Expand Down Expand Up @@ -203,7 +180,7 @@ export const convertRtcConfigToString = (config) => {
export const isValidMatchStr = (match) => {
const INVERT_MARKER = '!';
let str = match;
if (startsWith(match, INVERT_MARKER)) {
if (match?.startsWith(INVERT_MARKER)) {
str = match.slice(1);
}
return isValidStrPattern(str);
Expand All @@ -219,7 +196,7 @@ export const isValidMatchStr = (match) => {
export const isValidMatchNumber = (match) => {
const INVERT_MARKER = '!';
let str = match;
if (startsWith(match, INVERT_MARKER)) {
if (match?.startsWith(INVERT_MARKER)) {
str = match.slice(1);
}
const num = parseFloat(str);
Expand All @@ -243,7 +220,7 @@ export const isValidMatchNumber = (match) => {
export const parseMatchArg = (match) => {
const INVERT_MARKER = '!';
// In case if "match" is "undefined" return "false"
const isInvertedMatch = match ? match.startsWith(INVERT_MARKER) : false;
const isInvertedMatch = match ? match?.startsWith(INVERT_MARKER) : false;
const matchValue = isInvertedMatch ? match.slice(1) : match;
const matchRegexp = toRegExp(matchValue);
return { isInvertedMatch, matchRegexp, matchValue };
Expand All @@ -264,7 +241,7 @@ export const parseMatchArg = (match) => {
*/
export const parseDelayArg = (delay) => {
const INVERT_MARKER = '!';
const isInvertedDelayMatch = startsWith(delay, INVERT_MARKER);
const isInvertedDelayMatch = delay?.startsWith(INVERT_MARKER);
let delayValue = isInvertedDelayMatch ? delay.slice(1) : delay;
delayValue = parseInt(delayValue, 10);
const delayMatch = nativeIsNaN(delayValue) ? null : delayValue;
Expand All @@ -285,7 +262,7 @@ export const objectToString = (obj) => {
}
return isEmptyObject(obj)
? '{}'
: getObjectEntries(obj)
: Object.entries(obj)
.map((pair) => {
const key = pair[0];
const value = pair[1];
Expand Down
Loading

0 comments on commit ef04555

Please sign in to comment.