webext-patterns 1.3.0 → 1.5.0

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.
Files changed (4) hide show
  1. package/index.d.ts +4 -0
  2. package/index.js +39 -13
  3. package/package.json +13 -10
  4. package/readme.md +49 -15
package/index.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  export declare const patternValidationRegex: RegExp;
2
2
  export declare const allStarsRegex: RegExp;
3
3
  export declare const allUrlsRegex: RegExp;
4
+ export declare function assertValidPattern(matchPattern: string): void;
5
+ export declare function isValidPattern(matchPattern: string): boolean;
6
+ export declare function doesUrlMatchPatterns(url: string, ...patterns: string[]): boolean;
7
+ export declare function findMatchingPatterns(url: string, ...patterns: string[]): string[];
4
8
  export declare function patternToRegex(...matchPatterns: readonly string[]): RegExp;
5
9
  export declare function globToRegex(...globs: readonly string[]): RegExp;
6
10
  export declare function excludeDuplicatePatterns(matchPatterns: readonly string[]): string[];
package/index.js CHANGED
@@ -1,28 +1,54 @@
1
1
  import escapeStringRegexp from 'escape-string-regexp';
2
2
  // Copied from https://github.com/mozilla/gecko-dev/blob/073cc24f53d0cf31403121d768812146e597cc9d/toolkit/components/extensions/schemas/manifest.json#L487-L491
3
3
  export const patternValidationRegex = /^(https?|wss?|file|ftp|\*):\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^file:\/\/\/.*$|^resource:\/\/(\*|\*\.[^*/]+|[^*/]+)\/.*$|^about:/;
4
- const isFirefox = typeof navigator === 'object' && navigator.userAgent.includes('Firefox/');
4
+ const isFirefox = globalThis.navigator?.userAgent.includes('Firefox/');
5
5
  export const allStarsRegex = isFirefox
6
6
  ? /^(https?|wss?):[/][/][^/]+([/].*)?$/
7
7
  : /^https?:[/][/][^/]+([/].*)?$/;
8
8
  export const allUrlsRegex = /^(https?|file|ftp):[/]+/;
9
- function getRawPatternRegex(matchPattern) {
10
- if (!patternValidationRegex.test(matchPattern)) {
11
- throw new Error(matchPattern + ' is an invalid pattern, it must match ' + String(patternValidationRegex));
9
+ export function assertValidPattern(matchPattern) {
10
+ if (!isValidPattern(matchPattern)) {
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
+ }
13
+ }
14
+ export function isValidPattern(matchPattern) {
15
+ return matchPattern === '<all_urls>' || patternValidationRegex.test(matchPattern);
16
+ }
17
+ export function doesUrlMatchPatterns(url, ...patterns) {
18
+ if (patterns.includes('<all_urls>') && allUrlsRegex.test(url)) {
19
+ return true;
20
+ }
21
+ if (patterns.includes('*://*/*') && allStarsRegex.test(url)) {
22
+ return true;
12
23
  }
13
- let [, protocol, host, pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);
24
+ for (const pattern of patterns) {
25
+ if (patternToRegex(pattern).test(url)) {
26
+ return true;
27
+ }
28
+ }
29
+ return false;
30
+ }
31
+ export function findMatchingPatterns(url, ...patterns) {
32
+ return patterns.filter(pattern => doesUrlMatchPatterns(url, pattern));
33
+ }
34
+ function getRawPatternRegex(matchPattern) {
35
+ assertValidPattern(matchPattern);
36
+ // Host undefined for file:///
37
+ let [, protocol, host = '', pathname] = matchPattern.split(/(^[^:]+:[/][/])([^/]+)?/);
14
38
  protocol = protocol
15
39
  .replace('*', isFirefox ? '(https?|wss?)' : 'https?') // Protocol wildcard
16
- .replace(/[/]/g, '[/]'); // Escape slashes
17
- host = (host !== null && host !== void 0 ? host : '') // Undefined for file:///
40
+ .replaceAll(/[/]/g, '[/]'); // Escape slashes
41
+ if (host === '*') {
42
+ host = '[^/]+';
43
+ }
44
+ host &&= host
18
45
  .replace(/^[*][.]/, '([^/]+.)*') // Initial wildcard
19
- .replace(/^[*]$/, '[^/]+') // Only wildcard
20
- .replace(/[.]/g, '[.]') // Escape dots
21
- .replace(/[*]$/g, '[^.]+'); // Last wildcard
46
+ .replaceAll(/[.]/g, '[.]') // Escape dots
47
+ .replace(/[*]$/, '[^.]+'); // Last wildcard
22
48
  pathname = pathname
23
- .replace(/[/]/g, '[/]') // Escape slashes
24
- .replace(/[.]/g, '[.]') // Escape dots
25
- .replace(/[*]/g, '.*'); // Any wildcard
49
+ .replaceAll(/[/]/g, '[/]') // Escape slashes
50
+ .replaceAll(/[.]/g, '[.]') // Escape dots
51
+ .replaceAll(/[*]/g, '.*'); // Any wildcard
26
52
  return '^' + protocol + host + '(' + pathname + ')?$';
27
53
  }
28
54
  export function patternToRegex(...matchPatterns) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "webext-patterns",
3
- "version": "1.3.0",
3
+ "version": "1.5.0",
4
4
  "description": "Tool to convert the patterns and globs of your WebExtension manifest to regex",
5
5
  "keywords": [
6
6
  "browser",
@@ -21,8 +21,8 @@
21
21
  "license": "MIT",
22
22
  "author": "Federico Brigante <me@fregante.com> (https://fregante.com)",
23
23
  "type": "module",
24
- "main": "index.js",
25
- "module": "index.js",
24
+ "exports": "./index.js",
25
+ "types": "./index.d.ts",
26
26
  "files": [
27
27
  "index.js",
28
28
  "index.d.ts"
@@ -48,13 +48,16 @@
48
48
  "escape-string-regexp": "^5.0.0"
49
49
  },
50
50
  "devDependencies": {
51
- "@sindresorhus/tsconfig": "^3.0.1",
52
- "@types/chrome": "0.0.210",
53
- "ava": "^5.1.1",
54
- "sinon": "^15.0.1",
55
- "type-fest": "^3.5.3",
56
- "typescript": "^4.9.4",
57
- "xo": "^0.53.1"
51
+ "@sindresorhus/tsconfig": "^5.0.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
+ },
59
+ "engines": {
60
+ "node": ">=18"
58
61
  },
59
62
  "webExt": {
60
63
  "sourceDir": "demo-extension",
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
- > Tool to convert the [patterns](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_scripts#globs) of your WebExtension manifest to regex
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,8 +16,14 @@ npm install webext-patterns
18
16
  ```
19
17
 
20
18
  ```js
21
- // This module is only offered as a ES Module
22
- import {patternToRegex} from 'webext-patterns';
19
+ import {
20
+ patternToRegex,
21
+ globToRegex,
22
+ excludeDuplicatePatterns
23
+ doesUrlMatchPatterns,
24
+ assertValidPattern,
25
+ isValidPattern,
26
+ } from 'webext-patterns';
23
27
  ```
24
28
 
25
29
  ## Usage
@@ -33,6 +37,12 @@ globToRegex('*.example.com');
33
37
 
34
38
  excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
35
39
  // Returns ['https://*.google.com/*']
40
+
41
+ assertValidPattern('https://google.*/*');
42
+ // Throws an error because the pattern is invalid
43
+
44
+ isValidPattern('https://*.google.com/*');
45
+ // Returns true
36
46
  ```
37
47
 
38
48
  > **Note**
@@ -100,21 +110,45 @@ excludeDuplicatePatterns([
100
110
  // Returns ["https://*/*"]
101
111
  ```
102
112
 
103
- ## Related
113
+ #### doesUrlMatchPatterns(url, ...patterns)
114
+
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.
116
+
117
+ ```js
118
+ doesUrlMatchPatterns('https://google.com/', 'https://*.google.com/*', '*://example.com/*');
119
+ // Returns true
120
+ ```
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.
104
125
 
105
- ### Permissions
126
+ #### assertValidPattern(pattern)
106
127
 
107
- - [webext-additional-permissions](https://github.com/fregante/webext-additional-permissions) - Get any optional permissions that users have granted you.
108
- - [webext-dynamic-content-scripts](https://github.com/fregante/webext-dynamic-content-scripts) - Automatically registers your content_scripts on domains added via permission.request
128
+ Accepts a pattern and throws an error if it's invalid.
109
129
 
110
- ### Others
130
+ ```js
131
+ assertValidPattern('https://google.*/*');
132
+ // Throws an error because the pattern is invalid
133
+ ```
134
+
135
+ #### isValidPattern(pattern)
136
+
137
+ Accepts a pattern and returns `true` if it's valid.
138
+
139
+ ```js
140
+ isValidPattern('https://google.*/*');
141
+ // Returns false
142
+ ```
143
+
144
+ ## Related
111
145
 
112
- - [webext-options-sync](https://github.com/fregante/webext-options-sync) - Helps you manage and autosave your extension's options. Chrome and Firefox.
113
- - [webext-storage-cache](https://github.com/fregante/webext-storage-cache) - Map-like promised cache storage with expiration. Chrome and Firefox
114
- - [webext-detect-page](https://github.com/fregante/webext-detect-page) - Detects where the current browser extension code is being run. Chrome and Firefox.
115
- - [webext-content-script-ping](https://github.com/fregante/webext-content-script-ping) - One-file interface to detect whether your content script have loaded.
116
- - [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-page](https://github.com/fregante/webext-detect-page) - Detects where the current browser extension code is being run.
117
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)
118
152
 
119
153
  ## License
120
154