Skip to content

Commit 5cdde0c

Browse files
committed
优化 代码
1 parent 18a13ca commit 5cdde0c

6 files changed

Lines changed: 476 additions & 484 deletions

3dm-Enhanced.user.js

Lines changed: 83 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -194,29 +194,6 @@
194194
}
195195

196196

197-
// 自动翻页
198-
function pageLoading() {
199-
if (curSite.SiteTypeID > 0){
200-
windowScroll(function (direction, e) {
201-
if (direction === "down") { // 下滑才准备翻页
202-
let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
203-
let scrollDelta = 666;
204-
if (document.documentElement.scrollHeight <= document.documentElement.clientHeight + scrollTop + scrollDelta) {
205-
if (curSite.SiteTypeID === SiteType.FORUM) { // 如果是各版块帖子列表则直接点下一页就行了
206-
let autopbn = document.querySelector('#autopbn');
207-
if (autopbn && autopbn.innerText == "下一页 »"){ // 如果已经在加载中了,就忽略
208-
autopbn.click();
209-
}
210-
}else{
211-
ShowPager.loadMorePage();
212-
}
213-
}
214-
}
215-
});
216-
}
217-
}
218-
219-
220197
// 自动回复
221198
function autoReply(){
222199
if (loginStatus){
@@ -271,58 +248,73 @@
271248
}
272249

273250

251+
// 自动翻页
252+
function pageLoading() {
253+
if (curSite.SiteTypeID > 0){
254+
windowScroll(function (direction, e) {
255+
if (direction === 'down') { // 下滑才准备翻页
256+
let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
257+
let scrollDelta = 666;
258+
if (document.documentElement.scrollHeight <= document.documentElement.clientHeight + scrollTop + scrollDelta) {
259+
if (curSite.SiteTypeID === SiteType.FORUM) { // 如果是各版块帖子列表则直接点下一页就行了
260+
let autopbn = document.querySelector('#autopbn');
261+
if (autopbn && autopbn.innerText == "下一页 »"){ // 如果已经在加载中了,就忽略
262+
autopbn.click();
263+
}
264+
}else{
265+
ShowPager.loadMorePage();
266+
}
267+
}
268+
}
269+
});
270+
}
271+
}
272+
273+
274274
// 滚动条事件
275275
function windowScroll(fn1) {
276276
var beforeScrollTop = document.documentElement.scrollTop,
277277
fn = fn1 || function () {};
278278
setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
279-
window.addEventListener("scroll", function (e) {
279+
window.addEventListener('scroll', function (e) {
280280
var afterScrollTop = document.documentElement.scrollTop,
281281
delta = afterScrollTop - beforeScrollTop;
282282
if (delta == 0) return false;
283-
fn(delta > 0 ? "down" : "up", e);
283+
fn(delta > 0 ? 'down' : 'up', e);
284284
beforeScrollTop = afterScrollTop;
285285
}, false);
286286
}, 1000)
287287
}
288288

289289

290-
var ShowPager = { // 修改自 https://greasyfork.org/scripts/14178
290+
// 修改自 https://greasyfork.org/scripts/14178 , https://github.com/machsix/Super-preloader
291+
var ShowPager = {
291292
getFullHref: function (e) {
292293
if(e == null) return '';
293-
"string" != typeof e && (e = e.getAttribute("href"));
294+
'string' != typeof e && (e = e.getAttribute('href'));
294295
var t = this.getFullHref.a;
295-
return t || (this.getFullHref.a = t = document.createElement("a")), t.href = e, t.href;
296+
return t || (this.getFullHref.a = t = document.createElement('a')), (t.href = e), t.href;
296297
},
297298
createDocumentByString: function (e) {
298299
if (e) {
299-
if ("HTML" !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, "application/xhtml+xml");
300+
if ('HTML' !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, 'application/xhtml+xml');
300301
var t;
301-
try {
302-
t = (new DOMParser).parseFromString(e, "text/html");
303-
} catch (e) {
304-
}
302+
try { t = (new DOMParser).parseFromString(e, 'text/html');} catch (e) {}
305303
if (t) return t;
306-
if (document.implementation.createHTMLDocument) t = document.implementation.createHTMLDocument("ADocument"); else try {
307-
(t = document.cloneNode(!1)).appendChild(t.importNode(document.documentElement, !1)),
308-
t.documentElement.appendChild(t.createElement("head")), t.documentElement.appendChild(t.createElement("body"));
309-
} catch (e) {
304+
if (document.implementation.createHTMLDocument) {
305+
t = document.implementation.createHTMLDocument('ADocument');
306+
} else {
307+
try {((t = document.cloneNode(!1)).appendChild(t.importNode(document.documentElement, !1)), t.documentElement.appendChild(t.createElement('head')), t.documentElement.appendChild(t.createElement('body')));} catch (e) {}
310308
}
311309
if (t) {
312-
var r = document.createRange();
310+
var r = document.createRange(),
311+
n = r.createContextualFragment(e);
313312
r.selectNodeContents(document.body);
314-
var n = r.createContextualFragment(e);
315313
t.body.appendChild(n);
316-
for (var a, o = {
317-
TITLE: !0,
318-
META: !0,
319-
LINK: !0,
320-
STYLE: !0,
321-
BASE: !0
322-
}, i = t.body, s = i.childNodes, c = s.length - 1; c >= 0; c--) o[(a = s[c]).nodeName] && i.removeChild(a);
314+
for (var a, o = { TITLE: !0, META: !0, LINK: !0, STYLE: !0, BASE: !0}, i = t.body, s = i.childNodes, c = s.length - 1; c >= 0; c--) o[(a = s[c]).nodeName] && i.removeChild(a);
323315
return t;
324316
}
325-
} else console.error("没有找到要转成DOM的字符串");
317+
} else console.error('没有找到要转成 DOM 的字符串');
326318
},
327319
loadMorePage: function () {
328320
if (curSite.pager) {
@@ -371,45 +363,53 @@
371363
}
372364
},
373365
};
374-
375-
376-
function getElementByXpath(e, t, r) {
377-
r = r || document, t = t || r;
378-
try {
379-
return r.evaluate(e, t, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
380-
} catch (t) {
381-
return void console.error("无效的xpath");
382-
}
366+
function getElementByCSS(css, contextNode = document) {
367+
return contextNode.querySelector(css);
383368
}
384-
385-
386-
function getAllElements(e, t, r, n, o) {
387-
let getAllElementsByXpath = function(e, t, r) {
388-
return r = r || document, t = t || r, r.evaluate(e, t, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
389-
}
390-
391-
var i, s = [];
392-
if (!e) return s;
393-
if (r = r || document, n = n || window, o = o || void 0, t = t || r, "string" == typeof e) i = 0 === e.search(/^css;/i) ? function getAllElementsByCSS(e, t) {
394-
return (t || document).querySelectorAll(e);
395-
}(e.slice(4), t) : getAllElementsByXpath(e, t, r); else {
396-
if (!(i = e(r, n, o))) return s;
397-
if (i.nodeType) return s[0] = i, s;
398-
}
399-
return function makeArray(e) {
400-
var t, r, n, o = [];
401-
if (e.pop) {
402-
for (t = 0, r = e.length; t < r; t++) (n = e[t]) && (n.nodeType ? o.push(n) : o = o.concat(makeArray(n)));
403-
return a()(o);
369+
function getAllElementsByCSS(css, contextNode = document) {
370+
return [].slice.call(contextNode.querySelectorAll(css));
371+
}
372+
function getElementByXpath(xpath, contextNode, doc = document) {
373+
contextNode = contextNode || doc;
374+
try {
375+
const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
376+
// 应该总是返回一个元素节点
377+
return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
378+
} catch (err) {
379+
throw new Error(`Invalid xpath: ${xpath}`);
404380
}
405-
if (e.item) {
406-
for (t = e.length; t;) o[--t] = e[t];
407-
return o;
381+
}
382+
function getAllElementsByXpath(xpath, contextNode, doc = document) {
383+
contextNode = contextNode || doc;
384+
const result = [];
385+
try {
386+
const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
387+
for (let i = 0; i < query.snapshotLength; i++) {
388+
const node = query.snapshotItem(i);
389+
// 如果是 Element 节点
390+
if (node.nodeType === 1) result.push(node);
391+
}
392+
} catch (err) {
393+
throw new Error(`无效 Xpath: ${xpath}`);
408394
}
409-
if (e.iterateNext) {
410-
for (t = e.snapshotLength; t;) o[--t] = e.snapshotItem(t);
411-
return o;
395+
return result;
396+
}
397+
function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
398+
if (!selector) return [];
399+
contextNode = contextNode || doc;
400+
if (typeof selector === 'string') {
401+
if (selector.search(/^css;/i) === 0) {
402+
return getAllElementsByCSS(selector.slice(4), contextNode);
403+
} else {
404+
return getAllElementsByXpath(selector, contextNode, doc);
405+
}
406+
} else {
407+
const query = selector(doc, win, _cplink);
408+
if (!Array.isArray(query)) {
409+
throw new Error('getAllElements 返回错误类型');
410+
} else {
411+
return query;
412+
}
412413
}
413-
}(i);
414414
}
415415
})();

0 commit comments

Comments
 (0)