Skip to content

Commit 42bcca0

Browse files
committed
Improve jsonl-edit-fetch-response/jsonl-edit-xhr-response scriptlets
1 parent effbf26 commit 42bcca0

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

‎src/js/resources/json-edit.js‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,14 +1270,20 @@ registerScriptlet(trustedJsonEditFetchRequest, {
12701270
/******************************************************************************/
12711271
/******************************************************************************/
12721272

1273-
function jsonlEditFn(jsonp, text = '') {
1273+
function jsonlEditFn(jsonp, text = '', jsonExtract) {
12741274
const safe = safeSelf();
12751275
const lineSeparator = /\r?\n/.exec(text)?.[0] || '\n';
12761276
const linesBefore = text.split('\n');
12771277
const linesAfter = [];
12781278
for ( const lineBefore of linesBefore ) {
1279+
const match = jsonExtract.exec(lineBefore);
1280+
if ( match === null ) {
1281+
linesAfter.push(lineBefore);
1282+
continue;
1283+
}
1284+
const jsonBefore = match[1];
12791285
let obj;
1280-
try { obj = safe.JSON_parse(lineBefore); } catch { }
1286+
try { obj = safe.JSON_parse(jsonBefore); } catch { }
12811287
if ( typeof obj !== 'object' || obj === null ) {
12821288
linesAfter.push(lineBefore);
12831289
continue;
@@ -1287,7 +1293,12 @@ function jsonlEditFn(jsonp, text = '') {
12871293
linesAfter.push(lineBefore);
12881294
continue;
12891295
}
1290-
const lineAfter = safe.JSON_stringify(objAfter);
1296+
const jsonAfter = safe.JSON_stringify(objAfter);
1297+
const lineAfter = [
1298+
lineBefore.slice(0, match.index),
1299+
jsonAfter,
1300+
lineBefore.slice(match.index + jsonBefore.length),
1301+
].join('');
12911302
linesAfter.push(lineAfter);
12921303
}
12931304
return linesAfter.join(lineSeparator);
@@ -1315,6 +1326,9 @@ function jsonlEditXhrResponseFn(trusted, jsonq = '', ...varargs) {
13151326
}
13161327
const extraArgs = safe.parseVarargs(varargs);
13171328
const propNeedles = parsePropertiesToMatchFn(extraArgs.propsToMatch, 'url');
1329+
const jsonExtract = extraArgs.jsonExtract
1330+
? new RegExp(extraArgs.jsonExtract)
1331+
: /^(.*)$/;
13181332
self.XMLHttpRequest = class extends self.XMLHttpRequest {
13191333
open(method, url, ...args) {
13201334
const xhrDetails = { method, url };
@@ -1347,7 +1361,7 @@ function jsonlEditXhrResponseFn(trusted, jsonq = '', ...varargs) {
13471361
if ( typeof innerResponse !== 'string' ) {
13481362
return (xhrDetails.response = innerResponse);
13491363
}
1350-
const outerResponse = jsonlEditFn(jsonp, innerResponse);
1364+
const outerResponse = jsonlEditFn(jsonp, innerResponse, jsonExtract);
13511365
if ( outerResponse !== innerResponse ) {
13521366
safe.uboLog(logPrefix, 'Pruned');
13531367
}
@@ -1442,6 +1456,9 @@ function jsonlEditFetchResponseFn(trusted, jsonq = '', ...varargs) {
14421456
}
14431457
const extraArgs = safe.parseVarargs(varargs);
14441458
const propNeedles = parsePropertiesToMatchFn(extraArgs.propsToMatch, 'url');
1459+
const jsonExtract = extraArgs.jsonExtract
1460+
? new RegExp(extraArgs.jsonExtract)
1461+
: /^(.*)$/;
14451462
const logall = jsonq === '';
14461463
proxyApplyFn('fetch', function(context) {
14471464
const args = context.callArgs;
@@ -1462,7 +1479,7 @@ function jsonlEditFetchResponseFn(trusted, jsonq = '', ...varargs) {
14621479
safe.uboLog(logPrefix, textBefore);
14631480
return responseBefore;
14641481
}
1465-
const textAfter = jsonlEditFn(jsonp, textBefore);
1482+
const textAfter = jsonlEditFn(jsonp, textBefore, jsonExtract);
14661483
if ( textAfter === textBefore ) { return responseBefore; }
14671484
safe.uboLog(logPrefix, 'Pruned');
14681485
const responseAfter = new Response(textAfter, {

0 commit comments

Comments
 (0)