Call the "match patterns" API?

The JavaScript JavaScript API maps patterns in different places. In a Chrome extension, is there a way to manually call any functions that Google uses to check if a URL is being saved with a given match pattern? I have a bunch of urls that I would like to test.

+3


source to share


1 answer


No no. However, it is not that hard to replicate using regular expressions.

Here is the implementation. It checks for normal constraints on match patterns, but does not validate the host of the pattern / test url.



function patternToRegExp(pattern){
  if(pattern == "<all_urls>") return /^(?:http|https|file|ftp):\/\/.*/;

  var split = /^(\*|http|https|file|ftp):\/\/(.*)$/.exec(pattern);
  if(!split) throw Error("Invalid schema in " + pattern);
  var schema = split[1];
  var fullpath = split[2];

  var split = /^([^\/]*)\/(.*)$/.exec(fullpath);
  if(!split) throw Error("No path specified in " + pattern);
  var host = split[1];
  var path = split[2];

  // File 
  if(schema == "file" && host != "")
    throw Error("Non-empty host for file schema in " + pattern);

  if(schema != "file" && host == "")
    throw Error("No host specified in " + pattern);  

  if(!(/^(\*|\*\.[^*]+|[^*]*)$/.exec(host)))
    throw Error("Illegal wildcard in host in " + pattern);

  var reString = "^";
  reString += (schema == "*") ? "https*" : schema;
  reString += ":\\/\\/";
  // Not overly concerned with intricacies
  //   of domain name restrictions and IDN
  //   as we're not testing domain validity
  reString += host.replace(/\*\.?/, "[^\\/]*");
  reString += "(:\\d+)?";
  reString += "\\/";
  reString += path.replace("*", ".*");
  reString += "$";

  return RegExp(reString);
}

// Usage example
// Precompile the expression for performance reasons
var re = patternToRegExp("*://*.example.com/*.css");
// Test against it
re.test("http://example.com/"); // false
re.test("https://example.com/css/example.css"); // true
re.test("http://www.example.com/test.css"); // true

      

+3


source







All Articles