Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve engine events to expose internal workflow #3881

Open
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

seia-soto
Copy link
Member

fixes #3876

Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few things to address:

  1. To have a better overview we should also emit information of the exceptions (unHide), similarly to how it is being done for the network matches.
  2. how about we have one new event, lets call it match that would fire for both network and cosmetic filters. It's params should look alike for both types. (don't remove old event, they important for backwards compatibility)

I see it like this:

const context = { tabId: 1, frameId: 2 };
engine.getCosmeticsFilters(oldParams, withMetadata, context);
engine.match(request, withMetadata, context);
engine.on('match', (context, result) => {
  console.log(context, result.match, result.exception, result.metadata); 
});

Notice withMetadata for getCosmeticsFilters this is another capability to remember about. The result for the cosmetic filters should include the metadata in the same way the network result does.

packages/adblocker/src/engine/bucket/cosmetic.ts Outdated Show resolved Hide resolved
@seia-soto
Copy link
Member Author

@remusao I'd like to get a feedback about the way passing "context" to matching functions. It might help a lot for extension developers often used to involve tabId.

@chrmod This is actually an off-topic but if we involve devTools API, we can avoid involving tabId. The devtools apis are already provided in scope of the page, so we don't need to take care about the tabs.

Passing an external context can solve many complex situations but I'd like to go simple as possible. For example, we could decorate the passed Request object and many other things. Also, we're already passing request as an argument for network filter matches.

console.log('extended-rule-matched', rule, context);
});

blocker.on('style-rule-matched', (rule, context) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we could make it clearer in the naming that this is a hostname-based march of a cosmetic rule. Also, that it does not mean it marched in the page, just that it was selected by the engine as a candidate to be injected (I think it’s important to make this clear to avoid confusion)

@chrmod chrmod added the PR: New Feature 🚀 Increment minor version when merged label Apr 15, 2024
packages/adblocker-playwright-example/index.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/bucket/cosmetic.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if we need that many new events. How about we introduce one filter-matched which would emit:

  • a filter, cosmetic or network
  • a function name used to acquire the filter, like match, getHtmlFilters, getCosmeticsFilters
  • function arguments, which can include additional info like the context: any. For network filters it is most important to get the request.

Additionally, would like to set a naming convention for the public interfaces for the filters. Sometimes we call those things rules (for instance in context of Cosmetic bucket), but on the interface level I would strony suggest we always call both network and cosmetic "rule" with a name of filters.
This will be a contrast to the DNR, where after conversion the filters become the rules.

@seia-soto
Copy link
Member Author

In the last changes, the events were narrowed down to two: filter-matched and filter-excluded. Note that this idea was built top on the idea of adblocker debugger ui implementation.

Also, the commit addresses the followings:

  • Proper naming of EngineEventContext (renamed to MatchingContext): this variable name is expected to cause a lot of confusions.
  • MatchingContext was updated to the union type with a proper enum typing to support the sole and integrated event kinds: filter-matched and filter-excluded.
  • The additional argument can be passed to methods was renamed to reference instead of context to reduce confusion.

@seia-soto seia-soto changed the title feat: emit cosmetic rules on match Improve engine events to expose internal workflow Apr 30, 2024
Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add filter-matched for network filters as well? that would be in engine.match and engine.matchAll

packages/adblocker/src/engine/engine.ts Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/events.ts Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like while we develop this feature it started to change direction. All good, but it also got a bit complex. Lets try to simplify a bit before moving forward.

To recap the goal: We want to introduce new events that would allow us to implement a logger that is able to list matched filters and exceptions in a context of a given tab.

packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
@@ -1035,6 +1195,8 @@ export default class FilterEngine extends EventEmitter<

// Emit events if we found a match
if (result.exception !== undefined) {
this.emit('filter-matched', result.exception, context);

this.emit('request-whitelisted', request, result);
} else if (result.redirect !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we emit redirects as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is treated as a normal filter (result.filter) before being registered as result.redirect. The event should be triggered right after the filter match.

packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker-electron-example/index.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
packages/adblocker/src/engine/engine.ts Outdated Show resolved Hide resolved
Copy link
Member

@chrmod chrmod left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, let me test it locally before merging.

@seia-soto seia-soto marked this pull request as ready for review May 15, 2024 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: New Feature 🚀 Increment minor version when merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve feedback from getCosmeticsFilters
3 participants