-
Notifications
You must be signed in to change notification settings - Fork 56
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
Proposal: Match pattern matches #580
Comments
Until this is accepted/merged, I've been maintaining a package that does exactly that: https://github.com/fregante/webext-patterns The challenge has been that matching is browser- and context-dependent. Does |
|
I prefer @Jack-Works' proposed name of I think it would also be useful to expose some matching options, since sometimes things like the path or scheme should be ignored. WebKit has these options we could expose in an options dictionary to this API. We should also allow matching Another possible API around patterns would be creating a pattern from a Going even further, you could have an API to check if a pattern is valid or supported. Safari does not support ftp or file patterns currently, and having an API to check this would be useful. (Even though ftp and file patterns are not supported, they are still valid (parsable).) |
In yesterday's meeting browser engineers asked for use cases in order to better understand how and why this capability is necessary. @Jack-Works and @fregante, can you share more about your use cases? @xeenon, in abstract I agree that it may make sense to expose these options, since different parts of the platform use match patterns in different ways, such as web accessable resources vs. content scripts. For ease of reference, the As I write this, a use case came to mind: checking whether or not the extension can inject a content script on a given domain based on current permission grants. Ostensibly let {origins: matchPatterns} = await browser.permissions.getAll();
if (browser.runtime.patternMatches(matchPatterns, "https://example.com/page")) {
browser.scripting.executeScript(/*...*/);
} else {
showPermissionMissing();
} While devs can work around not having array support, that approach it is notably less ergonomic. let {origins} = await browser.permissions.getAll();
let hasPermission = false;
for (let matchPattern of origins) {
if (browser.runtime.patternMatches(matchPattern, "https://example.com/page")) {
hasPermission = true;
break;
}
}
if (hasPermission) {
browser.scripting.executeScript(/*...*/);
} else {
showPermissionMissing();
} |
For the sake of making the shape of the API I'm currently imagining a bit more concrete, I took a pass at writing up an interface definition in WebIDL. partial interface WebExtensionsRuntime {
dictionary MatchesPatternSettings {
boolean ignoreSchemes = false;
boolean ignorePaths = false;
};
boolean matchesPattern((DOMString or sequence<DOMString>) pattern,
(DOMString or URL) url,
optional MatchesPatternSettings settings);
}; |
Yes, @dotproto your case is exactly what happened in our extension and that's why I opened this issue. |
@dotproto FWIW, Safari only reports active granted permissions in |
This sounds like a bug to me. I would expect I generally use A task could be "extract the page title from all the Wikipedia.org tabs currently open", so I'd probably query the list of open tabs, and filter them by glob ( |
Having such API would also mean authors are assured of the matching algorithm used by the browser. For example, Safari seems to not match on the query string. See: https://bugs.webkit.org/show_bug.cgi?id=273069 |
FWIW, that's an anti-example because a more efficient solution is to use |
Agreed! However one would still need to filter out tabs without an URL in some older Safari versions (Before Tech Preview 192). The pattern could still be useful in situations where multiple match patterns are used and you want to know which pattern matched the URL. This could be part of this API design. |
@carlosjeurissen That bug is no longer an issue in WebKit and Safari Technology Preview 192. |
@xeenon Wonderful! Updated the comment to reflect this. |
This has been requested before and denied. I'm glad that it is seeing new traction. It would be incredibly useful. https://bugzilla.mozilla.org/show_bug.cgi?id=1395278 |
Sometimes I need to know if an origin matches a match pattern but there is no API so I need to write my own.
API Example:
The text was updated successfully, but these errors were encountered: