Skip to content

Commit

Permalink
replace the use of regexp
Browse files Browse the repository at this point in the history
  • Loading branch information
jellizaveta committed Aug 21, 2024
1 parent d421ce8 commit 8f43a26
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/scriptlets/trusted-click-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export function trustedClickElement(
const RELOAD_ON_FINAL_CLICK_MARKER = 'reloadAfterClick';
const SELECTORS_DELIMITER = ',';
const COOKIE_STRING_DELIMITER = ';';
const COLON = ':';
// Regex to split match pairs by commas, avoiding the ones included in regexes
const EXTRA_MATCH_DELIMITER = /(,\s*){1}(?=!?cookie:|!?localStorage:|containsText:)/;

Expand Down Expand Up @@ -321,34 +322,34 @@ export function trustedClickElement(
let shouldReloadAfterClick: boolean = false;
// Value used for reload timing
let reloadDelayMs: number = STATIC_RELOAD_DELAY_MS;
// Define a regular expression to match 'reloadAfterClick' or 'reloadAfterClick:<number>'
const RELOAD_PATTERN = new RegExp(`^${RELOAD_ON_FINAL_CLICK_MARKER}(?::(\\d+))?$`);

if (reload) {
const match = reload.match(RELOAD_PATTERN);
// Check if reload value starts with the right marker
if (!match) {
// split reload option by colon
const [reloadMarker, reloadValue] = reload.split(COLON);

if (reloadMarker !== RELOAD_ON_FINAL_CLICK_MARKER) {
logMessage(source, `Passed reload option '${reload}' is invalid`);
return;
}

// Check if reload value contains numbers, that indicates a custom reload value
if (match[1]) {
const passedReload = Number(match[1]);
// if reload value is set, will be used as a delay
// if reload value is not set, default value will be used
if (reloadValue) {
const passedReload = Number(reloadValue);

// Validate the passed reload value
// check if passed reload value is a number
if (Number.isNaN(passedReload)) {
logMessage(source, `Passed reload delay value '${passedReload}' is invalid`);
return;
}

// check if passed reload value is less than 10s
if (passedReload > OBSERVER_TIMEOUT_MS) {
// eslint-disable-next-line max-len
logMessage(source, `Passed reload delay value '${passedReload}' is bigger than maximum ${OBSERVER_TIMEOUT_MS} ms`);
return;
}

// Use the passed reload value
reloadDelayMs = passedReload;
}

Expand Down

0 comments on commit 8f43a26

Please sign in to comment.