tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

WildcardToRegexp.sys.mjs (994B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 /**
      6 * Converts a URL-like string which might include the `*` character as a wildcard
      7 * to a regular expression. They are used to match against actual URLs for the
      8 * request blocking feature from DevTools.
      9 *
     10 * The returned regular expression is case insensitive.
     11 *
     12 * @param {string} url
     13 *     A URL-like string which can contain one or several `*` as wildcard
     14 *     characters.
     15 * @return {RegExp}
     16 *     A regular expression which can be used to match URLs compatible with the
     17 *     provided url "template".
     18 */
     19 export function wildcardToRegExp(url) {
     20  return new RegExp(url.split("*").map(regExpEscape).join(".*"), "i");
     21 }
     22 
     23 /**
     24 * Escapes all special RegExp characters in the given string.
     25 */
     26 const regExpEscape = s => {
     27  return s.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
     28 };