webext-patterns 1.4.0 → 1.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -1
- package/index.js +21 -12
- package/package.json +7 -7
- package/readme.md +11 -17
package/index.d.ts
CHANGED
|
@@ -3,7 +3,8 @@ export declare const allStarsRegex: RegExp;
|
|
|
3
3
|
export declare const allUrlsRegex: RegExp;
|
|
4
4
|
export declare function assertValidPattern(matchPattern: string): void;
|
|
5
5
|
export declare function isValidPattern(matchPattern: string): boolean;
|
|
6
|
-
export declare function doesUrlMatchPatterns(url: string, ...
|
|
6
|
+
export declare function doesUrlMatchPatterns(url: string, ...patterns: string[]): boolean;
|
|
7
|
+
export declare function findMatchingPatterns(url: string, ...patterns: string[]): string[];
|
|
7
8
|
export declare function patternToRegex(...matchPatterns: readonly string[]): RegExp;
|
|
8
9
|
export declare function globToRegex(...globs: readonly string[]): RegExp;
|
|
9
10
|
export declare function excludeDuplicatePatterns(matchPatterns: readonly string[]): string[];
|
package/index.js
CHANGED
|
@@ -8,26 +8,29 @@ export const allStarsRegex = isFirefox
|
|
|
8
8
|
export const allUrlsRegex = /^(https?|file|ftp):[/]+/;
|
|
9
9
|
export function assertValidPattern(matchPattern) {
|
|
10
10
|
if (!isValidPattern(matchPattern)) {
|
|
11
|
-
throw new Error(matchPattern + ' is an invalid pattern
|
|
11
|
+
throw new Error(matchPattern + ' is an invalid pattern. See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns for more info.');
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
14
|
export function isValidPattern(matchPattern) {
|
|
15
15
|
return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
|
|
16
16
|
}
|
|
17
|
-
export function doesUrlMatchPatterns(url, ...
|
|
18
|
-
if (
|
|
17
|
+
export function doesUrlMatchPatterns(url, ...patterns) {
|
|
18
|
+
if (patterns.includes('<all_urls>') && allUrlsRegex.test(url)) {
|
|
19
19
|
return true;
|
|
20
20
|
}
|
|
21
|
-
if (
|
|
21
|
+
if (patterns.includes('*://*/*') && allStarsRegex.test(url)) {
|
|
22
22
|
return true;
|
|
23
23
|
}
|
|
24
|
-
for (const pattern of
|
|
24
|
+
for (const pattern of patterns) {
|
|
25
25
|
if (patternToRegex(pattern).test(url)) {
|
|
26
26
|
return true;
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
return false;
|
|
30
30
|
}
|
|
31
|
+
export function findMatchingPatterns(url, ...patterns) {
|
|
32
|
+
return patterns.filter(pattern => doesUrlMatchPatterns(url, pattern));
|
|
33
|
+
}
|
|
31
34
|
function getRawPatternRegex(matchPattern) {
|
|
32
35
|
assertValidPattern(matchPattern);
|
|
33
36
|
// Host undefined for file:///
|
|
@@ -38,12 +41,10 @@ function getRawPatternRegex(matchPattern) {
|
|
|
38
41
|
if (host === '*') {
|
|
39
42
|
host = '[^/]+';
|
|
40
43
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
.replace(/[*]$/, '[^.]+'); // Last wildcard
|
|
46
|
-
}
|
|
44
|
+
host &&= host
|
|
45
|
+
.replace(/^[*][.]/, '([^/]+.)*') // Initial wildcard
|
|
46
|
+
.replaceAll(/[.]/g, '[.]') // Escape dots
|
|
47
|
+
.replace(/[*]$/, '[^.]+'); // Last wildcard
|
|
47
48
|
pathname = pathname
|
|
48
49
|
.replaceAll(/[/]/g, '[/]') // Escape slashes
|
|
49
50
|
.replaceAll(/[.]/g, '[.]') // Escape dots
|
|
@@ -106,5 +107,13 @@ export function excludeDuplicatePatterns(matchPatterns) {
|
|
|
106
107
|
if (matchPatterns.includes('*://*/*')) {
|
|
107
108
|
return ['*://*/*'];
|
|
108
109
|
}
|
|
109
|
-
|
|
110
|
+
// Cover identical patterns
|
|
111
|
+
const uniquePatterns = [...new Set(matchPatterns)];
|
|
112
|
+
return uniquePatterns.filter(possibleSubset =>
|
|
113
|
+
// Keep if there are no matches
|
|
114
|
+
!uniquePatterns.some(possibleSuperset =>
|
|
115
|
+
// Don't compare to self
|
|
116
|
+
possibleSubset !== possibleSuperset
|
|
117
|
+
// Drop if it's a subset
|
|
118
|
+
&& patternToRegex(possibleSuperset).test(possibleSubset)));
|
|
110
119
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "webext-patterns",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.1",
|
|
4
4
|
"description": "Tool to convert the patterns and globs of your WebExtension manifest to regex",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"browser",
|
|
@@ -49,12 +49,12 @@
|
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@sindresorhus/tsconfig": "^5.0.0",
|
|
52
|
-
"@types/chrome": "0.0.
|
|
53
|
-
"ava": "^6.
|
|
54
|
-
"sinon": "^
|
|
55
|
-
"type-fest": "^4.
|
|
56
|
-
"typescript": "^5.
|
|
57
|
-
"xo": "^0.
|
|
52
|
+
"@types/chrome": "0.0.268",
|
|
53
|
+
"ava": "^6.1.3",
|
|
54
|
+
"sinon": "^18.0.0",
|
|
55
|
+
"type-fest": "^4.20.0",
|
|
56
|
+
"typescript": "^5.4.5",
|
|
57
|
+
"xo": "^0.58.0"
|
|
58
58
|
},
|
|
59
59
|
"engines": {
|
|
60
60
|
"node": ">=18"
|
package/readme.md
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
[badge-gzip]: https://img.shields.io/bundlephobia/minzip/webext-patterns.svg?label=gzipped
|
|
4
4
|
[link-bundlephobia]: https://bundlephobia.com/result?p=webext-patterns
|
|
5
5
|
|
|
6
|
-
>
|
|
7
|
-
|
|
8
|
-
This might be incomplete. Please help me test it by adding more pattern and URLs to the [tests](./test.js).
|
|
6
|
+
> Utilities for [patterns](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts#matching_url_patterns) and globs for WebExtensions
|
|
9
7
|
|
|
10
8
|
## Install
|
|
11
9
|
|
|
@@ -18,7 +16,6 @@ npm install webext-patterns
|
|
|
18
16
|
```
|
|
19
17
|
|
|
20
18
|
```js
|
|
21
|
-
// This module is only offered as a ES Module
|
|
22
19
|
import {
|
|
23
20
|
patternToRegex,
|
|
24
21
|
globToRegex,
|
|
@@ -118,10 +115,14 @@ excludeDuplicatePatterns([
|
|
|
118
115
|
Accepts a URL and any number of patterns and returns `true` if the URL matches any of the patterns. This is a convenience method that wraps `patternToRegex` for single use. If you plan on testing multiple URLs to the same pattern, it's better to convert the patterns to a regex once and reuse that.
|
|
119
116
|
|
|
120
117
|
```js
|
|
121
|
-
doesUrlMatchPatterns('https://google.com/',
|
|
118
|
+
doesUrlMatchPatterns('https://google.com/', 'https://*.google.com/*', '*://example.com/*');
|
|
122
119
|
// Returns true
|
|
123
120
|
```
|
|
124
121
|
|
|
122
|
+
#### findMatchingPatterns(url, ...patterns)
|
|
123
|
+
|
|
124
|
+
Accepts a URL and any number of patterns and returns an array of the patterns that match the URL. It returns an empty array if none of the patterns match the URL.
|
|
125
|
+
|
|
125
126
|
#### assertValidPattern(pattern)
|
|
126
127
|
|
|
127
128
|
Accepts a pattern and throws an error if it's invalid.
|
|
@@ -142,19 +143,12 @@ isValidPattern('https://google.*/*');
|
|
|
142
143
|
|
|
143
144
|
## Related
|
|
144
145
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
- [webext-
|
|
148
|
-
- [webext-
|
|
149
|
-
|
|
150
|
-
### Others
|
|
151
|
-
|
|
152
|
-
- [webext-options-sync](https://github.com/fregante/webext-options-sync) - Helps you manage and autosave your extension's options. Chrome and Firefox.
|
|
153
|
-
- [webext-storage-cache](https://github.com/fregante/webext-storage-cache) - Map-like promised cache storage with expiration. Chrome and Firefox
|
|
154
|
-
- [webext-detect-page](https://github.com/fregante/webext-detect-page) - Detects where the current browser extension code is being run. Chrome and Firefox.
|
|
155
|
-
- [webext-content-script-ping](https://github.com/fregante/webext-content-script-ping) - One-file interface to detect whether your content script have loaded.
|
|
156
|
-
- [web-ext-submit](https://github.com/fregante/web-ext-submit) - Wrapper around Mozilla’s web-ext to submit extensions to AMO.
|
|
146
|
+
- [webext-permissions](https://github.com/fregante/webext-permissions) - Get any optional permissions that users have granted you.
|
|
147
|
+
- [webext-options-sync](https://github.com/fregante/webext-options-sync) - Helps you manage and autosave your extension's options.
|
|
148
|
+
- [webext-storage-cache](https://github.com/fregante/webext-storage-cache) - Map-like promised cache storage with expiration.
|
|
149
|
+
- [webext-detect](https://github.com/fregante/webext-detect) - Detects where the current browser extension code is being run.
|
|
157
150
|
- [Awesome-WebExtensions](https://github.com/fregante/Awesome-WebExtensions) - A curated list of awesome resources for WebExtensions development.
|
|
151
|
+
- [More…](https://github.com/fregante/webext-fun)
|
|
158
152
|
|
|
159
153
|
## License
|
|
160
154
|
|