Skip to content

Commit

Permalink
fix logging in all request scriptlets
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Nov 1, 2022
1 parent 8d7d5a3 commit d9ab3c1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/scriptlets/prevent-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
return;
}

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

let strResponseBody;
if (responseBody === 'emptyObj') {
strResponseBody = '{}';
Expand All @@ -109,7 +112,7 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
// Skip disallowed response types
if (!(responseType === 'default' || responseType === 'opaque')) {
// eslint-disable-next-line no-console
console.log(`Invalid parameter: ${responseType}`);
log(`Invalid parameter: ${responseType}`);
return;
}

Expand All @@ -118,8 +121,8 @@ export function preventFetch(source, propsToMatch, responseBody = 'emptyObj', re
const fetchData = getFetchData(args);
if (typeof propsToMatch === 'undefined') {
// log if no propsToMatch given
const logMessage = `log: fetch( ${objectToString(fetchData)} )`;
hit(source, logMessage);
log(`fetch( ${objectToString(fetchData)} )`);
hit(source);
return Reflect.apply(target, thisArg, args);
}

Expand Down
10 changes: 6 additions & 4 deletions src/scriptlets/prevent-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ export function preventXHR(source, propsToMatch, customResponseText) {
return;
}

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

let shouldPrevent = false;
let response = '';
let responseText = '';
Expand All @@ -108,8 +111,8 @@ export function preventXHR(source, propsToMatch, customResponseText) {
responseUrl = xhrData.url;
if (typeof propsToMatch === 'undefined') {
// Log if no propsToMatch given
const logMessage = `log: xhr( ${objectToString(xhrData)} )`;
hit(source, logMessage);
log(`log: xhr( ${objectToString(xhrData)} )`);
hit(source);
} else {
shouldPrevent = matchRequestProps(propsToMatch);
}
Expand All @@ -135,8 +138,7 @@ export function preventXHR(source, propsToMatch, customResponseText) {
if (randomText) {
responseText = randomText;
} else {
// eslint-disable-next-line no-console
console.log(`Invalid range: ${customResponseText}`);
log(`Invalid range: ${customResponseText}`);
}
}
// Mock response object
Expand Down
3 changes: 1 addition & 2 deletions src/scriptlets/trusted-replace-fetch-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,7 @@ export function trustedReplaceFetchResponse(source, pattern = '', replacement =

if (shouldLog) {
// log if no propsToMatch given
const logMessage = `log: fetch( ${objectToString(fetchData)} )`;
log(source, logMessage);
log(`fetch( ${objectToString(fetchData)} )`);
hit(source);
return Reflect.apply(target, thisArg, args);
}
Expand Down
3 changes: 2 additions & 1 deletion src/scriptlets/trusted-replace-xhr-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ export function trustedReplaceXhrResponse(source, pattern = '', replacement = ''
if (shouldLog) {
// Log if no propsToMatch given
const logMessage = `log: xhr( ${objectToString(xhrData)} )`;
log(source, logMessage);
log(logMessage);
hit(source);
return Reflect.apply(target, thisArg, args);
}

Expand Down

0 comments on commit d9ab3c1

Please sign in to comment.