mirror of
https://github.com/XIU2/UserScript.git
synced 2026-07-24 11:03:23 +00:00
优化 代码
This commit is contained in:
+69
-62
@@ -997,59 +997,54 @@
|
||||
var beforeScrollTop = document.documentElement.scrollTop,
|
||||
fn = fn1 || function () {};
|
||||
setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
|
||||
window.addEventListener("scroll", function (e) {
|
||||
window.addEventListener('scroll', function (e) {
|
||||
var afterScrollTop = document.documentElement.scrollTop,
|
||||
delta = afterScrollTop - beforeScrollTop;
|
||||
if (delta == 0) return false;
|
||||
fn(delta > 0 ? "down" : "up", e);
|
||||
fn(delta > 0 ? 'down' : 'up', e);
|
||||
beforeScrollTop = afterScrollTop;
|
||||
}, false);
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
|
||||
var ShowPager = { // 修改自 https://greasyfork.org/scripts/14178
|
||||
// 修改自 https://greasyfork.org/scripts/14178 , https://github.com/machsix/Super-preloader
|
||||
var ShowPager = {
|
||||
getFullHref: function (e) {
|
||||
if(e == null) return '';
|
||||
"string" != typeof e && (e = e.getAttribute("href"));
|
||||
'string' != typeof e && (e = e.getAttribute('href'));
|
||||
var t = this.getFullHref.a;
|
||||
return t || (this.getFullHref.a = t = document.createElement("a")), t.href = e, t.href;
|
||||
if (t) {return t}
|
||||
((this.getFullHref.a = t = document.createElement('a')), t.href = e, t.href)
|
||||
if (t.href) {return t.href}
|
||||
//return t || ((this.getFullHref.a = t = document.createElement('a')), t.href = e, t.href);
|
||||
},
|
||||
createDocumentByString: function (e) {
|
||||
if (e) {
|
||||
if ("HTML" !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, "application/xhtml+xml");
|
||||
if ('HTML' !== document.documentElement.nodeName) return (new DOMParser).parseFromString(e, 'application/xhtml+xml');
|
||||
var t;
|
||||
try {
|
||||
t = (new DOMParser).parseFromString(e, "text/html");
|
||||
} catch (e) {
|
||||
}
|
||||
try { t = (new DOMParser).parseFromString(e, 'text/html');} catch (e) {}
|
||||
if (t) return t;
|
||||
if (document.implementation.createHTMLDocument) t = document.implementation.createHTMLDocument("ADocument"); else 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) {
|
||||
if (document.implementation.createHTMLDocument) {
|
||||
t = document.implementation.createHTMLDocument('ADocument');
|
||||
} else {
|
||||
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) {}
|
||||
}
|
||||
if (t) {
|
||||
var r = document.createRange();
|
||||
var r = document.createRange(),
|
||||
n = r.createContextualFragment(e);
|
||||
r.selectNodeContents(document.body);
|
||||
var n = r.createContextualFragment(e);
|
||||
t.body.appendChild(n);
|
||||
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);
|
||||
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);
|
||||
return t;
|
||||
}
|
||||
} else console.error("没有找到要转成DOM的字符串");
|
||||
} else console.error('没有找到要转成 DOM 的字符串');
|
||||
},
|
||||
loadMorePage: function () {
|
||||
if (curSite.pager) {
|
||||
let curPageEle = getElementByXpath(curSite.pager.nextLink);
|
||||
var url = this.getFullHref(curPageEle);
|
||||
console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
|
||||
//console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
|
||||
if (url === '') return;
|
||||
if (curSite.pageUrl === url) return;// 避免重复加载相同的页面
|
||||
curSite.pageUrl = url;
|
||||
@@ -1063,8 +1058,9 @@
|
||||
try {
|
||||
//console.log('最终 URL:' + response.finalUrl, '返回内容:' + response.responseText)
|
||||
var newBody = ShowPager.createDocumentByString(response.responseText);
|
||||
let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody);
|
||||
let toElement = getAllElements(curSite.pager.HT_insert[0])[0];
|
||||
let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody),
|
||||
toElement = getAllElements(curSite.pager.HT_insert[0])[0];
|
||||
|
||||
if (pageElems.length >= 0) {
|
||||
// 如果有插入前函数就执行函数
|
||||
if (curSite.function && curSite.function.before) {
|
||||
@@ -1074,12 +1070,14 @@
|
||||
pageElems = curSite.function.before(pageElems);
|
||||
}
|
||||
}
|
||||
|
||||
// 插入位置
|
||||
let addTo1 = addTo(curSite.pager.HT_insert[1]);
|
||||
// 插入新页面元素
|
||||
pageElems.forEach(function (one) {
|
||||
toElement.insertAdjacentElement(addTo1, one);
|
||||
});
|
||||
|
||||
// 替换待替换元素
|
||||
try {
|
||||
let oriE = getAllElements(curSite.pager.replaceE);
|
||||
@@ -1110,44 +1108,53 @@
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
function getElementByXpath(e, t, r) {
|
||||
r = r || document, t = t || r;
|
||||
try {
|
||||
return r.evaluate(e, t, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
|
||||
} catch (t) {
|
||||
return void console.error("无效的xpath");
|
||||
}
|
||||
function getElementByCSS(css, contextNode = document) {
|
||||
return contextNode.querySelector(css);
|
||||
}
|
||||
|
||||
|
||||
function getAllElements(e, t, r, n, o) {
|
||||
let getAllElementsByXpath = function(e, t, r) {
|
||||
return r = r || document, t = t || r, r.evaluate(e, t, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
||||
}
|
||||
|
||||
var i, s = [];
|
||||
if (!e) return s;
|
||||
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) {
|
||||
return (t || document).querySelectorAll(e);
|
||||
}(e.slice(4), t) : getAllElementsByXpath(e, t, r); else {
|
||||
if (!(i = e(r, n, o))) return s;
|
||||
if (i.nodeType) return s[0] = i, s;
|
||||
}
|
||||
return function makeArray(e) {
|
||||
var t, r, n, o = [];
|
||||
if (e.pop) {
|
||||
for (t = 0, r = e.length; t < r; t++) (n = e[t]) && (n.nodeType ? o.push(n) : o = o.concat(makeArray(n)));
|
||||
return a()(o);
|
||||
function getAllElementsByCSS(css, contextNode = document) {
|
||||
return [].slice.call(contextNode.querySelectorAll(css));
|
||||
}
|
||||
function getElementByXpath(xpath, contextNode, doc = document) {
|
||||
contextNode = contextNode || doc;
|
||||
try {
|
||||
const result = doc.evaluate(xpath, contextNode, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
|
||||
// 应该总是返回一个元素节点
|
||||
return result.singleNodeValue && result.singleNodeValue.nodeType === 1 && result.singleNodeValue;
|
||||
} catch (err) {
|
||||
throw new Error(`Invalid xpath: ${xpath}`);
|
||||
}
|
||||
if (e.item) {
|
||||
for (t = e.length; t;) o[--t] = e[t];
|
||||
return o;
|
||||
}
|
||||
function getAllElementsByXpath(xpath, contextNode, doc = document) {
|
||||
contextNode = contextNode || doc;
|
||||
const result = [];
|
||||
try {
|
||||
const query = doc.evaluate(xpath, contextNode, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
|
||||
for (let i = 0; i < query.snapshotLength; i++) {
|
||||
const node = query.snapshotItem(i);
|
||||
// 如果是 Element 节点
|
||||
if (node.nodeType === 1) result.push(node);
|
||||
}
|
||||
} catch (err) {
|
||||
throw new Error(`无效 Xpath: ${xpath}`);
|
||||
}
|
||||
if (e.iterateNext) {
|
||||
for (t = e.snapshotLength; t;) o[--t] = e.snapshotItem(t);
|
||||
return o;
|
||||
return result;
|
||||
}
|
||||
function getAllElements(selector, contextNode = undefined, doc = document, win = window, _cplink = undefined) {
|
||||
if (!selector) return [];
|
||||
contextNode = contextNode || doc;
|
||||
if (typeof selector === 'string') {
|
||||
if (selector.search(/^css;/i) === 0) {
|
||||
return getAllElementsByCSS(selector.slice(4), contextNode);
|
||||
} else {
|
||||
return getAllElementsByXpath(selector, contextNode, doc);
|
||||
}
|
||||
} else {
|
||||
const query = selector(doc, win, _cplink);
|
||||
if (!Array.isArray(query)) {
|
||||
throw new Error('getAllElements 返回错误类型');
|
||||
} else {
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}(i);
|
||||
}
|
||||
})();
|
||||
Reference in New Issue
Block a user