// ==UserScript==
// @name 什么值得买SMZDM - 自动签到、翻页
// @version 0.4
// @author Siukei
// @description 值得买自动签到、自动无缝翻页
// @match *://*.smzdm.com/p/*
// @icon https://www.smzdm.com/favicon.ico
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_openInTab
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_notification
// @license GPL-3.0 License
// @run-at document-end
// @namespace https://www.smzdm.com
// ==/UserScript==
(function () {
// 默认 ID 为 0
var curSite = {
SiteTypeID: 1,
pager: {
nextLink: '//*[@id="commentTabBlockNew"]/ul[2]/li[@class="pagedown"]/a[@href]',
pageElement: 'css;div#commentTabBlockNew > ul > li[id^="li_comment_"]',
HT_insert: ['css;ul.comment_listBox', 2],
replaceE: '//*[@id="commentTabBlockNew"]/ul[2]',
}
, pageUrl: '' // 下一页url
};
pageLoading(); // 自动翻页
// 自动翻页
function pageLoading() {
if (curSite.SiteTypeID > 0) {
commentShowAll();
windowScroll(function (direction, e) {
if (direction === "down") { // 下滑才准备翻页
let page = document.querySelector('#commentTabBlockNew > ul.pagination');
if (isInViewPortOfOne(page)) {
ShowPager.loadMorePage();
}
}
});
}
}
//判断元素是否出现在窗口内
function isInViewPortOfOne(el) {
// viewPortHeight 兼容所有浏览器写法
let viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
let offsetTop = el.offsetTop
let scrollTop = document.documentElement.scrollTop
let top = offsetTop - scrollTop
// console.log('top', top)
// 这里有个+100是为了提前加载+ 100
return top <= viewPortHeight + 100
}
// 滚动条事件
function windowScroll(fn1) {
var beforeScrollTop = document.documentElement.scrollTop,
fn = fn1 || function () { };
setTimeout(function () { // 延时执行,避免刚载入到页面就触发翻页事件
window.addEventListener("scroll", function (e) {
var afterScrollTop = document.documentElement.scrollTop,
delta = afterScrollTop - beforeScrollTop;
if (delta == 0) return false;
fn(delta > 0 ? "down" : "up", e);
beforeScrollTop = afterScrollTop;
}, false);
}, 1000)
}
//自动展开隐藏评论
function commentShowAll() {
let showAll = document.querySelectorAll('.comment_showAll a');
for (var i = 0; i < showAll.length; i++) {
showAll[i].click();
}
}
var ShowPager = { // 修改自 https://gf.qytechs.cn/scripts/14178
getFullHref: function (e) {
if (e == null) return '';
"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;
},
createDocumentByString: function (e) {
if (e) {
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) {
}
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 (t) {
var r = document.createRange();
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);
return t;
}
} else console.error("没有找到要转成DOM的字符串");
},
loadMorePage: function () {
if (curSite.pager) {
let curPageEle = getElementByXpath(curSite.pager.nextLink);
// console.log(curPageEle);
var url = this.getFullHref(curPageEle);
//console.log(`${url} ${curPageEle} ${curSite.pageUrl}`);
if (url === '') return;
if (curSite.pageUrl === url) return;// 不会重复加载相同的页面
curSite.pageUrl = url;
console.log(url);
// 读取下一页的数据
// curSite.pager.startFilter && curSite.pager.startFilter();
GM_xmlhttpRequest({
url: url,
method: "GET",
timeout: 5000,
onload: function (response) {
try {
var newBody = ShowPager.createDocumentByString(response.responseText);
let pageElems = getAllElements(curSite.pager.pageElement, newBody, newBody);
let toElement = getAllElements(curSite.pager.HT_insert[0])[0];
if (pageElems.length >= 0) {
let addTo = "beforeend";
// 插入新页面元素
pageElems.forEach(function (one) {
toElement.insertAdjacentElement(addTo, one);
});
// 替换待替换元素
try {
let oriE = getAllElements(curSite.pager.replaceE);
let repE = getAllElements(curSite.pager.replaceE, newBody, newBody);
if (oriE.length === repE.length) {
for (var i = 0; i < oriE.length; i++) {
oriE[i].outerHTML = repE[i].outerHTML;
}
}
} catch (e) {
console.log(e);
}
commentShowAll();
}
} catch (e) {
console.log(e);
}
}
});
}
},
};
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 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);
}
if (e.item) {
for (t = e.length; t;) o[--t] = e[t];
return o;
}
if (e.iterateNext) {
for (t = e.snapshotLength; t;) o[--t] = e.snapshotItem(t);
return o;
}
}(i);
}
autoSign();
//自动签到
function autoSign() {
// 首页,主动点击按钮
if (/www\.smzdm\.com(\/|)$/.test(window.location.href)) {
var btn = document.getElementsByClassName('J_punch')[0];
if (/签到(得|领|拿)积分/.test(btn.text)) {
btn.click();
}
} else {
// 非首页,内嵌脚本签到
var url = 'https://zhiyou.smzdm.com/user/checkin/jsonp_checkin'
var embed_script = document.createElement('script');
embed_script.setAttribute('src', url);
// 把script标签加入head,此时调用开始
document.getElementsByTagName('head')[0].appendChild(embed_script);
}
}
})();