webext-patterns
TypeScript icon, indicating that this package has built-in type declarations

1.3.0 • Public • Published

webext-patterns

Tool to convert the patterns of your WebExtension manifest to regex

This might be incomplete. Please help me test it by adding more pattern and URLs to the tests.

Install

You can download the standalone bundle and include it in your manifest.json.

Or use npm:

npm install webext-patterns
// This module is only offered as a ES Module
import {patternToRegex} from 'webext-patterns';

Usage

patternToRegex('http://*/*');
// Returns /^http:[/][/][^/]+[/].+$/

globToRegex('*.example.com');
// Returns /\.example\.com$/

excludeDuplicatePatterns(['https://*.google.com/*', 'https://google.com/*']);
// Returns ['https://*.google.com/*']

Note Firefox and Chrome handle patterns very slighly differently. webext-patterns defaults to Chrome’s logic, but if it detects a Firefox userAgent it will produce a Firefox-compatible regex.

API

patternToRegex(pattern1, pattern2, etc)

Accepts any number of string arguments and returns a single regex to match all of them.

Match patterns are used in the manifest’s permissions and content scripts’ matches and exclude_matches array.

patternToRegex('http://*/*');
// Returns /^http:[/][/][^/]+[/].+$/

const gmailRegex = patternToRegex('*://mail.google.com/*');
gmailRegex.test('https://mail.google.com/a/b/c'); // -> true
gmailRegex.test('https://photos.google.com/a/b/c'); // -> false

// Also accepts multiple patterns and returns a single regex
const googleRegex = patternToRegex(
	'https://google.com/*',
	'https://google.it/*'
);
googleRegex.test('https://google.it/search'); // -> true
googleRegex.test('https://google.de/search'); // -> false

globToRegex(pattern1, pattern2, etc)

Accepts any number of string arguments and returns a single regex to match all of them.

Globs are used in the manifest’s content scripts’ include_globs and exclude_globs arrays.

globToRegex('*.example.co?');
// Returns /\.example\.co.?$/ in Firefox
// Returns /\.example\.co.$/ everywhere else

const gmailRegex = globToRegex('*://mai?.google.com/*');
gmailRegex.test('https://mail.google.com/a/b/c'); // -> true
gmailRegex.test('https://photos.google.com/a/b/c'); // -> false

// Also accepts multiple globs and returns a single regex
const googleRegex = globToRegex(
	'*google.com*',
	'*google.it*'
);
googleRegex.test('https://google.it/search'); // -> true
googleRegex.test('https://google.de/search'); // -> false

excludeDuplicatePatterns([pattern1, pattern2, etc])

Accepts an array of patterns and returns a filtered array without the patterns that are already covered by others. For example "https://*/*" already covers all "https" URLs, so having "https://google.com/*" in the array won't make any difference and therefore it's dropped.

excludeDuplicatePatterns([
	"https://*/*",
	"https://google.com/*",
	"https://*.example.com/*",
]);
// Returns ["https://*/*"]

Related

Permissions

Others

License

MIT © Federico Brigante

Install

npm i webext-patterns

DownloadsWeekly Downloads

3,670

Version

1.3.0

License

MIT

Unpacked Size

11.4 kB

Total Files

5

Last publish

Collaborators

  • fregante