将用户特定的URL列表与当前的URL进行比较? [英] Comparing user-specific URL list with current URL?
问题描述
我有一个白名单,用户可以输入特定的URL / URL模式(只针对 http
和 https
。
我想转换并比较这些网址/网址格式,以便通配符选择符( *
用户输入:
示例。* / test
我想将它转换为:
* // *。example 。* / test
,以便它匹配:
http:// www。 example.com/test
,https://example.co.uk/test
另一个例子:
用户输入:
http://www.*.com/*
我想将其转换为:
http://www.*.com/*
,以便它匹配:
http://www.blah.com/test
,http ://www.other.com/null.html
和
用户输入:
www.example.com / *
我想将其转换为:
,以便它匹配:
http://www.example.com/testtwo
,https://www.example.com/arfg
我想要的理由插入一个领先的协议(如果它没有被用户包括在内)是因为我使用这个来比较当前标签的URL。
我得到这个数组URL字符串,并希望将它们与当前网址进行比较,但在匹配所有用例时遇到问题:
isNotWhitelisted: function(){
var whitelist = MyObject.userLists.whitelist;
var currentUrl = document.location.href;
for(var i = 0; i< whitelist.length; i ++){
var regexListItem = new RegExp(whitelist [i] .toString()。replace(。,\\ 。)。replace(*,。+));
if(currentUrl.match(regexListItem)){
return false;
}
}
返回true;
},
-
首先,正则表达式转换匹配最终用例(例如
example.com / *
,但不包含例子。* / about
-
这是Chrome扩展的一部分,是否没有更好/更简单的方法来使用内置方法?
感谢您提前提供任何帮助。 解决方案
贪婪的匹配,我想你需要请求用户以这种格式输入模式: *:// * / *
<你可以用这种方式检查它:
var special_char_rgx = /((.\\.|\[|\] | \ {| \} | \(| \)| \ + | \ | | \\ | \ / | \ $ | \ ^ | \ |)/ g; / /我认为所有...
var asterisk_rgx = / \ * / g;
var pattern_rgx = / ^([^:\ /] +):\ / \ /([ ^ \ /] +)\ /(.*)$/ g;
function addPatern(pattern,whitelist){
var str_pattern = p如果(isMatch){
pattern = pattern.replace(special_char_rgx,')', '\\ $ 1')。replace(asterisk_rgx,'。+');
whitelist.push(new RegExp('^'+ pattern +'$'));
}
pattern_rgx.lastIndex = 0; //否则RegExp.test保存该值并销毁测试!
返回isMatch;
$ b如果你想用不同的方式处理协议/域/路径,你可以做这样:
if(isMatch){
var protocol = RegExp。$ 1;
var domain = RegExp。$ 2;
var path_query = RegExp。$ 3;
//您的逻辑...
}
I have a whitelist where users can enter specific URLs/URL patterns (only targetting http
and https
.
I would like to transfrom and compare these URL/URL patterns so that the wildcard selector (*
) can be used like so
user enters: example.*/test
I want to transform this to: *//*.example.*/test
so that it matches: http://www.example.com/test
, https://example.co.uk/test
Another example:
user enters: http://www.*.com/*
I want to transform this to: http://www.*.com/*
so that it matches: http://www.blah.com/test
, http://www.other.com/null.html
and
user enters: www.example.com/*
I want to transform this to: *//www.example.com/*
so that it matches: http://www.example.com/testtwo
, https://www.example.com/arfg
The reason I want to insert a leading protocol (if it wasn't included by the user) is because I am using this to compare against the current tab URL.
I get this array of URL strings and would like to compare them with the current url, but am having trouble matching all use cases:
"isNotWhitelisted" : function(){
var whitelist = MyObject.userLists.whitelist;
var currentUrl = document.location.href;
for(var i=0; i<whitelist.length; i++){
var regexListItem = new RegExp(whitelist[i].toString().replace(".", "\\.").replace("*", ".+"));
if(currentUrl.match(regexListItem)) {
return false;
}
}
return true;
},
Firstly, the regex conversion matches end cases (e.g. example.com/*
but not kinds like example.*/about
This is part of a Chrome extension, is there not a better/easier way to do this maybe using inbuilt methods?
Thanks for any help in advance.
解决方案 If you want a greedy matching, I think you need request the user enter the pattern in this format: *://*/*
You can check this in this way:
var special_char_rgx = /(\.|\[|\]|\{|\}|\(|\)|\+|\?|\\|\/|\$|\^|\|)/g; // I think that all...
var asterisk_rgx = /\*/g;
var pattern_rgx = /^([^:\/]+):\/\/([^\/]+)\/(.*)$/g;
function addPatern(pattern, whitelist) {
var str_pattern = pattern.replace(asterisk_rgx,'\\*');
var isMatch = pattern_rgx.test(str_pattern);
if (isMatch) {
pattern = pattern.replace(special_char_rgx,'\\$1').replace(asterisk_rgx, '.+');
whitelist.push(new RegExp('^'+pattern + '$'));
}
pattern_rgx.lastIndex = 0; // Otherwise RegExp.test save this value and destroy the tests!
return isMatch;
}
If you want handle the protocol/ domain/ path in different ways you can do it that way:
if (isMatch) {
var protocol = RegExp.$1;
var domain= RegExp.$2;
var path_query = RegExp.$3;
// Your logic...
}
这篇关于将用户特定的URL列表与当前的URL进行比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!