-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.url.link.user.js
117 lines (101 loc) · 3.47 KB
/
common.url.link.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// ==UserScript==
// @name 常用油猴脚本方法:链接
// @version 0.0.6-deprecated
// @author DuangXT
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/common.log.user.js
// @require https://raw.githubusercontent.com/DuangXT/some_monkeys_js/main/common.string.user.js
// ==/UserScript==
// common level 1
gmlog('公共库:链接');
const currentUrl = location.href.toString();
const hostname = location.hostname.toString();
/** 刷新页面至指定链接 */
const refesh = (url=currentUrl, replace) => {
if(!url) url = currentUrl;
if(!url.startsWith("http")) url = 'https://' + url;
if(replace && currentUrl !== url){
log('替换链接为:', url);
location.replace(url);
return;
}
log('跳转链接:', url);
location.href = url;
}
// 尝试禁用页面刷新
window.location.reload = ()=>log('页面刷新已被禁用');
/** 判断当前URL内是否包含匹配的字符串 */
const currentUrlIncludes = (...searchString) => currentUrl.contains(...searchString);
const currentUrlContain = currentUrlContains = currentUrlIncludes;
/** 判断域名内是否包含匹配字符串 */
const hostnameContains = (...matchs) => hostname.containsIgnoreCase(...matchs);
const hostnameHas = hostnameContains;
const hostnameIs = (...hostnames) => hostname.equalsIgnoreCase(...hostnames);
/** 如果主站点匹配则执行动作 */
function runIfHostIs(){
if(arguments.length < 2){
throw new TypeError('参数应至少传递两位,且最后一位为执行函数');
}
let callback = arguments[arguments.length-1];
if(!isFunction(callback)){
throw new TypeError('参数错误哦!最后一位参数应为执行函数');
}
let hosts = Array.prototype.slice.call(arguments);
hosts.splice(hosts.length - 1, 1);
if(hostnameIs(hosts)){
callback();
}
}
/** 查询参数是否包含 */
function searchParamsContains(...paramNames){
let params = getQueryParams();
for (let paramName of paramNames) {
if(params[paramName]) return true;
}
return false;
}
const queryParamsContains = searchParamsContains;
/** 获取当前页面链接上的url参数对象 */
function getLocationQueryVariables(){
let query = location.search.substring(1),
vars = query.split("&"), params = {};
vars.forEach(var1 => {
let pair = var1.split("=");
params[pair[0]] = pair[1];
});
return params;
}
/** 获取url中的参数对象
*
* @Deprecated 这个函数中的正则表达式使用 \w 进行匹配,会导致转义问题
*/
function getURLParams(url){
if(!url){
log("没有指定url,获取当前页面url的参数集");
return getLocationQueryVariables();
}
let params = {}, pattern = /(\w+)=(\w+)/ig; // 表达式只用\w是有问题的
url.replace(pattern, ($, $1, $2) => {
params[$1] = $2;
});
return params;
}
/**
* 获取查询参数
* //by http://stackoverflow.com/a/1099670
*/
function getQueryParams(qs = document.location.search) {
qs = qs.split('+').join(' ');
let params = {},
tokens,
re = /[?&]?([^=]+)=([^&]*)/g;
while ((tokens = re.exec(qs))) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
return params;
}
const getQueryParam = (name) => getQueryParams()[name];
/** 从url中获取一个指定的参数
*
* @Deprecated see getURLParams() documentation
*/
const getUrlParam = (name, url) => getURLParams(url)[name];