Skip to content

Commit

Permalink
improve description & add common delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Oct 7, 2022
1 parent a98f115 commit 19b4903
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/scriptlets/trusted-click-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
*
* - `selectors` — required, string with query selectors delimited by comma
* - 'delay' - optional, time in ms to delay scriptlet execution after elements are loaded and targeted on the page, defaults to 0
* - `extraMatch` - optional, extra values to match on a page as keyword:value pairs delimited by commas. Possible keywords:
* - `extraMatch` — optional, extra condition to check on a page; allows to match `cookie` and `localStorage`; can be set as `name:key[=value]` where `value` is optional.
* Multiple conditions are allowed inside one `extraMatch` but they should be delimited by comma and each of them should match the syntax. Possible `name`s:
* - `cookie` - test substring against document.cookie string
* - `localStorage` - check if localStorage item is present
* **Examples**
Expand All @@ -39,7 +40,7 @@ import {
*
* 4. Match by cookie strings and click multiple elements by selector
* ```
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"], input[type="submit"][value="akkoord"]', '', 'cookie:userConsentCommunity, cookie:cmpconsent')
* example.com#%#//scriptlet('trusted-click-element', 'button[name="agree"], input[type="submit"][value="akkoord"]', '', 'cookie:userConsentCommunity, cookie:cmpconsent=1')
* ```
*
* 5. Match by localStorage item 'promo' key
Expand All @@ -61,16 +62,15 @@ export function trustedClickElement(source, selectors, delay, extraMatch = '') {
const log = console.log.bind(console);

const OBSERVER_TIMEOUT = 10000;
const SELECTORS_DELIMITER = ',';
const EXTRA_MATCH_DELIMITER = ',';
const COOKIE_MATCH_MARKER = 'cookie:';
const COMMON_DELIMITER = ',';
const LOCAL_STORAGE_MATCH_MARKER = 'localStorage:';

let parsedDelay;
if (delay) {
parsedDelay = parseInt(delay, 10);
if (Number.isNaN(parsedDelay) || parsedDelay > OBSERVER_TIMEOUT) {
log(`Passed delay '${delay}' is invalid or bigger than ${OBSERVER_TIMEOUT}ms`);
log(`Passed delay '${delay}' is invalid or bigger than ${OBSERVER_TIMEOUT} ms`);
return;
}
}
Expand All @@ -83,7 +83,7 @@ export function trustedClickElement(source, selectors, delay, extraMatch = '') {
if (extraMatch) {
// Get all match marker:value pairs from argument
const parsedExtraMatch = extraMatch
.split(EXTRA_MATCH_DELIMITER)
.split(COMMON_DELIMITER)
.map((matchStr) => matchStr.trim());

// Filter match pairs by marker
Expand Down Expand Up @@ -121,7 +121,7 @@ export function trustedClickElement(source, selectors, delay, extraMatch = '') {
* and prevent selectors from being queried multiple times
*/
let selectorsSequence = selectors
.split(SELECTORS_DELIMITER)
.split(COMMON_DELIMITER)
.map((selector) => selector.trim());

/**
Expand Down

0 comments on commit 19b4903

Please sign in to comment.