t-comm
Version:
专业、稳定、纯粹的工具库
57 lines (53 loc) • 1.97 kB
JavaScript
import { b as __awaiter, c as __generator } from './tslib.es6-096fffdd.js';
function autoScroll(element, page, bottomTimes) {
if (bottomTimes === void 0) {
bottomTimes = 0;
}
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
return [4 /*yield*/, page.evaluate(function (_a) {
var element = _a.element,
bottomTimes = _a.bottomTimes;
return new Promise(function (resolve) {
var UNIT_DISTANCE = 100;
var ele = document.querySelector(element);
var totalHeight = 0;
var curBottomTimes = 0;
var lastScrollHeight = ele === null || ele === void 0 ? void 0 : ele.scrollHeight;
var doScroll = function doScroll(ele) {
totalHeight = ele.scrollTop + UNIT_DISTANCE;
ele.scrollTop = totalHeight;
};
var timer = setInterval(function () {
var scrollHeight = ele.scrollHeight;
doScroll(ele);
if (scrollHeight > lastScrollHeight) {
curBottomTimes += 1;
lastScrollHeight = scrollHeight;
}
console.log('[autoScroll] scrollHeight & totalHeight', scrollHeight, totalHeight);
if (totalHeight >= scrollHeight || bottomTimes && curBottomTimes > bottomTimes) {
console.log('[autoScroll] end');
clearInterval(timer);
resolve(1);
}
}, 100);
});
}, {
element: element,
bottomTimes: bottomTimes
})];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
}
var scroll = /*#__PURE__*/Object.freeze({
__proto__: null,
autoScroll: autoScroll
});
export { autoScroll as a, scroll as s };