gpt4 book ai didi

google-chrome - 调用谷歌的 "match patterns"API?

转载 作者:行者123 更新时间:2023-12-04 01:42:47 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Can I allow the extension user to choose matching domains?

(1 个回答)


7年前关闭。




Chrome JavaScript API 使用 match patterns在各个地方。在 Chrome 扩展程序中,有没有办法手动调用谷歌用来测试 URL 是否遵守给定匹配模式的任何函数?我有一堆要测试的 URL。

最佳答案

不,那里没有。但是,使用正则表达式进行复制并不难。

这是一个实现。它检查匹配模式的正常限制,但不检查主机模式/测试 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

关于google-chrome - 调用谷歌的 "match patterns"API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26420269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com