Skip to content

Commit

Permalink
fix log condition
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 1, 2022
1 parent 545d6b3 commit d2f0562
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
18 changes: 11 additions & 7 deletions src/scriptlets/trusted-replace-fetch-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ import {
* - pattern - optional, argument for matching contents of responseText that should be replaced. If set, `replacement` is required;
* possible values:
* - '*' to match all text content
* - string
* - non-empty string
* - regular expression
* - replacement — optional, should be set if `pattern` is set. String to replace the response text content matched by `pattern`. Empty string to remove content.
* - replacement — optional, should be set if `pattern` is set. String to replace the response text content matched by `pattern`.
* Empty string to remove content. Defaults to empty string.
* - propsToMatch - optional, string of space-separated properties to match; possible props:
* - string or regular expression for matching the URL passed to fetch call; empty string, wildcard `*` or invalid regular expression will match all fetch calls
* - colon-separated pairs `name:value` where
Expand Down Expand Up @@ -74,7 +75,7 @@ import {
* ```
*/
/* eslint-enable max-len */
export function trustedReplaceFetchResponse(source, pattern = '', replacement = '', propsToMatch) {
export function trustedReplaceFetchResponse(source, pattern = '', replacement = '', propsToMatch = '') {
// do nothing if browser does not support fetch or Proxy (e.g. Internet Explorer)
// https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
Expand All @@ -84,14 +85,17 @@ export function trustedReplaceFetchResponse(source, pattern = '', replacement =
return;
}

const shouldLog = typeof pattern === 'undefined';
// eslint-disable-next-line no-console
const log = console.log.bind(console);

if (!shouldLog && (pattern === '' || typeof replacement === 'undefined')) {
// Only allow pattern as empty string for logging purposes
if (pattern === '' && replacement !== '') {
const logMessage = 'log: Pattern argument should not be empty string.';
log(source, logMessage);
return;
}
const shouldLog = pattern === '' && replacement === '';

// eslint-disable-next-line no-console
const log = console.log.bind(console);
const nativeFetch = fetch;

let shouldReplace = false;
Expand Down
16 changes: 11 additions & 5 deletions src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
* - pattern - optional, argument for matching contents of responseText that should be replaced. If set, `replacement` is required;
* possible values:
* - '*' to match all text content
* - string
* - non-empty string
* - regular expression
* - replacement — optional, should be set if `pattern` is set. String to replace matched content with. Empty string to remove content.
* - propsToMatch — optional, string of space-separated properties to match for extra condition; possible props:
Expand Down Expand Up @@ -77,12 +77,18 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
return;
}

if (typeof pattern === 'undefined' || typeof replacement === 'undefined') {
// eslint-disable-next-line no-console
const log = console.log.bind(console);

// Only allow pattern as empty string for logging purposes
if (pattern === '' && replacement !== '') {
const logMessage = 'log: Pattern argument should not be empty string.';
log(source, logMessage);
return;
}

// eslint-disable-next-line no-console
const log = console.log.bind(console);
const shouldLog = pattern === '' && replacement === '';

const nativeOpen = window.XMLHttpRequest.prototype.open;
const nativeSend = window.XMLHttpRequest.prototype.send;

Expand All @@ -93,7 +99,7 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
const openWrapper = (target, thisArg, args) => {
xhrData = getXhrData(...args);

if (pattern === '' && replacement === '') {
if (shouldLog) {
// Log if no propsToMatch given
const logMessage = `log: xhr( ${objectToString(xhrData)} )`;
log(source, logMessage);
Expand Down

0 comments on commit d2f0562

Please sign in to comment.