-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathutil.js
240 lines (222 loc) · 7.37 KB
/
util.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
'use strict';
pxer.util = pxer.util || {};
// 全局函数
pxer.util.blinkTitle = function (addMsg, spaceMsg) {
var addMsg = addMsg || '[ OK ] ';
var spaceMsg = spaceMsg || '[ ] ';
var timer = setInterval(() => {
if (document.title.indexOf(addMsg) !== -1) {
document.title = document.title.replace(addMsg, spaceMsg);
} else if (document.title.indexOf(spaceMsg) !== -1) {
document.title = document.title.replace(spaceMsg, addMsg);
} else {
document.title = addMsg + document.title;
}
}, 500);
window.addEventListener('mousemove', function _self() {
window.addEventListener('mousemove', _self);
clearInterval(timer);
document.title = document.title.replace(spaceMsg, "").replace(addMsg, "");
});
};
pxer.util.parseURL = function (url = document.URL) {
var arr = url.match(/^(?:(https?):)?\/\/([\w_\d.:\-]+?)((?:\/[^\/?]*)*)\/?(?:\?(.+))?$/);
var data = {
protocol: arr[1],
domain: arr[2],
path: arr[3],
query: arr[4],
};
if (data.query && data.query.indexOf('=') !== -1) {
data.query = {};
for (let item of arr[4].split('&')) {
let tmp = item.split('=');
data.query[tmp[0]] = tmp[1];
}
}
return data;
};
pxer.util.createScript = function (url) {
if (!/^(https?:)?\/\//.test(url)) url = window['PXER_URL'] + url;
var elt = document.createElement('script');
elt.charset = 'utf-8';
return function (resolve, reject) {
elt.addEventListener('load', resolve);
elt.addEventListener('load', function () {
if (window['PXER_MODE'] === 'dev') console.log('Loaded ' + url);
});
elt.addEventListener('error', reject);
elt.src = url;
document.documentElement.appendChild(elt);
return elt;
};
};
pxer.util.createResource = function (url) {
if (!/^(https?:)?\/\//.test(url)) url = window['PXER_URL'] + url;
let fx = url.match(/\.([^\.]+?)$/)[1];
let elt = document.createElement('link');
switch (fx) {
case 'css':
elt.rel = 'stylesheet';
break;
case 'ico':
elt.rel = 'shortcut icon';
elt.type = 'image/x-icon';
break;
default:
throw new Error(`unknown filename extension "${fx}"`)
}
return function (resolve, reject) {
elt.href = url;
document.documentElement.appendChild(elt);
if (window['PXER_MODE'] === 'dev') console.log('Linked ' + url);
resolve();
};
};
pxer.util.execPromise = function (taskList, call) {
var promise = Promise.resolve();
if (Array.isArray(taskList) && Array.isArray(taskList[0])) {
for (let array of taskList) {
promise = promise.then(() => Promise.all(array.map(item => new Promise(call(item)))));
}
} else if (Array.isArray(taskList)) {
for (let item of taskList) {
promise = promise.then(() => new Promise(call(item)));
}
} else {
promise = promise.then(() => new Promise(call(taskList)));
}
return promise;
};
/**
* 当前页面类型。可能的值
* - bookmark_user 自己/其他人关注的用户列表
* - bookmark_works 自己/其他人收藏的作品
* - member_info 自己/其他人的主页
* - works_medium 查看某个作品
* - works_manga 查看某个多张作品的多张页
* - works_big 查看某个作品的某张图片的大图
* - member_works 自己/其他人作品列表页
* - member_works_new 自己/其他人作品列表页
* - search 检索页
* - index 首页
* - discovery 探索
* - rank 排行榜
* - bookmark_new 关注的新作品
* - unknown 未知
* @param {Document} doc
* @return {string} - 页面类型
* */
pxer.util.getPageType = function (doc = document) {
const url = doc.URL;
var URLData = pxer.util.parseURL(url);
switch (true) {
case pxer.regexp.urlWorkDetail.test(URLData.path):
return 'works_medium';
}
var type = null;
var isnew = !(Boolean(document.querySelector(".count-badge")) || Boolean(document.querySelector(".profile")));
if (URLData.domain !== 'www.pixiv.net') return 'unknown';
if (pxer.regexp.isBookmarksUrl.test(URLData.path)) {
type = 'bookmark_works'
} else if (URLData.path.startsWith('/users/')) {
type = 'member_works_new'
} else if (URLData.path.startsWith('/tags/')) {
type = 'search_tag'
} else if (URLData.path === '/bookmark.php') {
if (URLData.query && URLData.query.type) {
switch (URLData.query.type) {
case 'user':
type = 'bookmark_user';
break;
default:
type = 'unknown';
}
} else {
type = 'bookmark_works';
}
} else if (URLData.path === '/bookmark_new_illust.php') {
type = 'bookmark_new';
} else if (URLData.path === '/member.php') {
type = isnew ? 'member_works_new' : "member_info";
} else if (URLData.path === '/ranking.php') {
type = 'rank';
} else if (URLData.path === '/member_illust.php') {
if (URLData.query && URLData.query.mode) {
switch (URLData.query.mode) {
case 'medium':
type = 'works_medium';
break;
case 'manga':
type = 'works_manga';
break;
case 'manga_big':
type = 'works_big';
break;
default:
type = 'unknown';
}
} else {
type = isnew ? 'member_works_new' : "member_works";
}
} else if (URLData.path === '/search.php') {
// TODO: Not all of search is carried in SPA
// But new version seems batter?
type = 'search_spa';
} else if (URLData.path === '/discovery') {
type = 'discovery';
} else if (URLData.path === '/') {
type = 'index';
} else {
type = 'unknown';
}
return type;
};
/**
* 查询对应页面类型每页作品数量
* @param {string} type - 作品类型
* @return {number} - 每页作品数
*/
pxer.util.getOnePageWorkCount = function (type) {
switch (type) {
case "search_spa":
return 48
case "search_tag":
return 60
case "search":
return 40
case "rank":
return 50
case "discovery":
return 3000
case "bookmark_works":
return 48
case "bookmark_new":
return 60
case "member_works_new":
return Number.MAX_SAFE_INTEGER
default:
return 20
}
}
pxer.util.getIDfromURL = function (key = 'id', url = document.URL) {
const urlInfo = new URL(url, document.URL);
var query = urlInfo.search;
var params = new URLSearchParams(query);
const result = params.get(key);
if (result) return result;
// read id from url
const matchResult = url.match(/\d+/);
return matchResult ? matchResult[0] : null;
};
pxer.util.fetchPixivApi = async function (url) {
return (
await (
await fetch(
url,
{credentials: 'include'},
)
).json()
).body;
};
Object.assign(window, pxer.util);