Scriptlets
Scriptlets copied to clipboard
Improve 'json-prune' or implement new scriptlet
there are few issues:
1 current json-prune does not match values if we need specify pruning, only keys — obligatoryProps — are checked
2 if specific array item has to be pruned and we can target such items by their extra fields
#%#//scriptlet('json-prune', 'recs_group.[].tiles.[].[].*', 'recs_group.[].tiles.[].[].ad_origin')
but whole recs_group.[].tiles.[].[] array items will be pruned, not just ones with ad_origin.
check on fontanka.ru
https://uploads.adguard.com/slbdzdrv7cfxo.png
@slavaleleka what do you think about implementing a scriptlet that supports jq-like syntax for that? At least a subset of it.
https://stedolan.github.io/jq/tutorial/
Alternatively, there is this: https://jmespath.org/
I do not suggest supporting it fully, but we could take that syntax as a base and build our limited subset of it. This way it'd be easier for the scriptlet users to understand the capabilities.
PS: we'll probably need to reassign this task to a later milestone.
@ngorskikh the spec should've been posted here I think: https://github.com/AdguardTeam/CoreLibs/issues/1447
We'll also need a filter expression for checking whether the value contains the specified string: ?(key-contains <key> <value>)
Currently, $jsonprune modifier can modify only response of a request.
However, during maintaining List-KR filter, I often need its scriptlet version.
Example:
Gmarket.co.kr
URL: https://gmarket.co.kr/
Description: Extended CSS can hide the elements having AD label. However, their network requests cannot be blocked with the previous modifiers and URL rules (including a RegExp)
Related Issue: https://github.com/List-KR/List-KR/issues/563
Targeted array items: $props.pageProps.serverData.topWideBannerSection.banners[?(@.isAd == true)]
Could you please explain what you're trying to do with that request?
Do you need to block a request that contains such a JSON path?
https://github.com/AdguardTeam/Scriptlets/issues/183#issuecomment-1336472219
Oh, I forgot to add what i want.
What i want is a feature removing a property with a JSON path from result of calling JSON.parse.
So you'd like a scriptlet alternative to $jsonprune?
Yes.
Makes sense, $jsonprune syntax is more powerful and the end goal was indeed to provide a scriptlet version of it.
Moved it to Scriptlets v1.9 for now. @slavaleleka @stanislav-atr what do you think about it? To understand better what's required please check out this issue in CL: https://github.com/AdguardTeam/CoreLibs/issues/1447
we shall return to the issue later
Later is now, @slavaleleka :)
the same is needed in scriptlets as well: https://github.com/AdguardTeam/CoreLibs/issues/1717
UPD: jsonp cannot be handled by the json-prune scriptlet
second issue in first issue post cannot be fixed in current syntax implementation — obligatoryProps is checked for the whole json and there is no way to check exact inner array item child (which is object) and conditionally prune it while other array items remains unchanged
@slavaleleka not sure what you mean, aren't filter expressions supposed to solve this?
I mean current syntax implementation, not jsonpath filter expression
I couldn't figure out how we can handle JSONP requests, apart from one method. For example, let's say there's a JSONP callback like this:
...
<script>
function handleResponse(data) {
console.log(JSON.stringify(data));
}
</script>
<script src="http://server.com/jsonp?callback=handleResponse"></script>
...
We can find the matching callback function from the page and proxify the call to it.
Probably jsonp shouldn't be in scope of this issue since there's no clear solution.
Please take a look for json-prune scriptlet version: https://github.com/gorhill/uBlock/commit/3152896d428c54c76cfd66c3da110bd4d6506cbc and https://github.com/gorhill/uBlock/commit/14d60ac5d3a1c5c2b9b8c062ea31e4ddcd13a98f.