|
22 | 22 |
|
23 | 23 | import { |
24 | 24 | collateFetchArgumentsFn, |
| 25 | + lookupElementsFn, |
25 | 26 | matchObjectPropertiesFn, |
26 | 27 | parsePropertiesToMatchFn, |
27 | 28 | } from './utils.js'; |
@@ -413,6 +414,115 @@ registerScriptlet(trustedEditThisObject, { |
413 | 414 | /******************************************************************************/ |
414 | 415 | /******************************************************************************/ |
415 | 416 |
|
| 417 | +function editElementObjectFn( |
| 418 | + trusted = false, |
| 419 | + selector = '', |
| 420 | + jsonq = '', |
| 421 | + timeout = '' |
| 422 | +) { |
| 423 | + if ( selector === '' ) { return; } |
| 424 | + const safe = safeSelf(); |
| 425 | + const logPrefix = safe.makeLogPrefix( |
| 426 | + `${trusted ? 'trusted-' : ''}edit-element-object`, |
| 427 | + selector, jsonq, timeout |
| 428 | + ); |
| 429 | + const jsonp = JSONPath.create(jsonq); |
| 430 | + if ( jsonp.valid === false || jsonp.value !== undefined && trusted !== true ) { |
| 431 | + return safe.uboLog(logPrefix, 'Bad JSONPath query'); |
| 432 | + } |
| 433 | + const process = elems => { |
| 434 | + for ( const elem of elems ) { |
| 435 | + const r = jsonp.apply(elem); |
| 436 | + if ( r === undefined ) { continue; } |
| 437 | + safe.uboLog(logPrefix, 'Edited'); |
| 438 | + } |
| 439 | + }; |
| 440 | + const result = lookupElementsFn(selector, |
| 441 | + timeout !== '' ? Date.now() + parseInt(timeout, 10) : 0 |
| 442 | + ); |
| 443 | + if ( Array.isArray(result) ) { |
| 444 | + process(result); |
| 445 | + } else if ( result instanceof Promise ) { |
| 446 | + result.then(elems => process(elems)); |
| 447 | + } |
| 448 | +} |
| 449 | +registerScriptlet(editElementObjectFn, { |
| 450 | + name: 'edit-element-object.fn', |
| 451 | + dependencies: [ |
| 452 | + JSONPath, |
| 453 | + lookupElementsFn, |
| 454 | + safeSelf, |
| 455 | + ], |
| 456 | +}); |
| 457 | + |
| 458 | +/******************************************************************************/ |
| 459 | +/** |
| 460 | + * @scriptlet edit-element-object.js |
| 461 | + * |
| 462 | + * @description |
| 463 | + * Prune properties of one or more elements matching a specific selector. |
| 464 | + * Properties can only be removed. |
| 465 | + * |
| 466 | + * @param selector |
| 467 | + * The selector used to locate the elements on which the JSONPath query will |
| 468 | + * be applied. |
| 469 | + * |
| 470 | + * @param jsonq |
| 471 | + * A uBO-flavored JSONPath query. |
| 472 | + * |
| 473 | + * @param timeout |
| 474 | + * The maximum time (in milliseconds) to wait for matching elements before |
| 475 | + * stopping. Defaults to 0, which executes a single lookup attempt without |
| 476 | + * waiting. |
| 477 | + * |
| 478 | + * */ |
| 479 | + |
| 480 | +function editElementObject(...args) { |
| 481 | + editElementObjectFn(false, ...args); |
| 482 | +} |
| 483 | +registerScriptlet(editElementObject, { |
| 484 | + name: 'edit-element-object.js', |
| 485 | + dependencies: [ |
| 486 | + editElementObjectFn, |
| 487 | + ], |
| 488 | +}); |
| 489 | + |
| 490 | +/******************************************************************************/ |
| 491 | +/** |
| 492 | + * @scriptlet trusted-element-this-object.js |
| 493 | + * |
| 494 | + * @description |
| 495 | + * Edit properties of one or more elements matching a specific selector. |
| 496 | + * Properties can be assigned new values. |
| 497 | + * |
| 498 | + * @param selector |
| 499 | + * The selector used to locate the elements on which the JSONPath query will |
| 500 | + * be applied. |
| 501 | + * |
| 502 | + * @param jsonq |
| 503 | + * A uBO-flavored JSONPath query. |
| 504 | + * |
| 505 | + * @param timeout |
| 506 | + * The maximum time (in milliseconds) to wait for matching elements before |
| 507 | + * stopping. Defaults to 0, which executes a single lookup attempt without |
| 508 | + * waiting. |
| 509 | + * |
| 510 | + * */ |
| 511 | + |
| 512 | +function trustedEditElementObject(...args) { |
| 513 | + editElementObjectFn(true, ...args); |
| 514 | +} |
| 515 | +registerScriptlet(trustedEditElementObject, { |
| 516 | + name: 'trusted-edit-element-object.js', |
| 517 | + requiresTrust: true, |
| 518 | + dependencies: [ |
| 519 | + editElementObjectFn, |
| 520 | + ], |
| 521 | +}); |
| 522 | + |
| 523 | +/******************************************************************************/ |
| 524 | +/******************************************************************************/ |
| 525 | + |
416 | 526 | function jsonEditXhrResponseFn(trusted, jsonq = '') { |
417 | 527 | const safe = safeSelf(); |
418 | 528 | const logPrefix = safe.makeLogPrefix( |
|
0 commit comments