UNPKG

ayakashi

Version:

The next generation web scraping framework

148 lines (147 loc) 6.96 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); function default_1(ayakashiInstance) { ayakashiInstance.registerAction("scrollIntoView", function (prop) { return __awaiter(this, void 0, void 0, function* () { const myProp = this.prop(prop); if (!myProp) throw new Error("<scrollIntoView> needs a valid prop"); const matchCount = yield myProp.trigger(); if (matchCount === 0) throw new Error("<scrollIntoView> needs a prop with at least 1 match"); return this.evaluateAsync(function (scopedPropId) { const node = this.propTable[scopedPropId].matches[0]; return new Promise(resolve => { const observer = new IntersectionObserver(function (entries) { resolve(entries[0].intersectionRatio); observer.disconnect(); }); observer.observe(node); }).then(function (intersectionRatio) { if (intersectionRatio !== 1) { node.scrollIntoView({ block: "center", inline: "center" }); } }); }, myProp.id); }); }); ayakashiInstance.registerAction("scrollIn", function (prop, pixelsToScroll) { return __awaiter(this, void 0, void 0, function* () { const myProp = this.prop(prop); if (!myProp) throw new Error("<scrollIn> needs a valid prop"); const matchCount = yield myProp.trigger(); if (matchCount === 0) throw new Error("<scrollIn> needs a prop with at least 1 match"); yield this.scrollIntoView(myProp); return this.evaluate(function (scopedPropId, scopedpixelsToScroll) { const node = this.propTable[scopedPropId].matches[0]; if (scopedpixelsToScroll) { node.scrollTop += scopedpixelsToScroll; } else { node.scrollTop = node.scrollHeight; } return node.scrollTop; }, myProp.id, pixelsToScroll); }); }); ayakashiInstance.registerAction("scrollInUntilBottomIsReached", function (prop, scrollInterval, timeout) { return __awaiter(this, void 0, void 0, function* () { const myProp = this.prop(prop); if (!myProp) throw new Error("<scrollInUntilBottomIsReached> needs a valid prop"); const matchCount = yield myProp.trigger(); if (matchCount === 0) throw new Error("<scrollInUntilBottomIsReached> needs a prop with at least 1 match"); yield this.scrollIntoView(myProp); let previousHeight = -1; let currentHeight = 0; return new Promise(function (resolve, reject) { ayakashiInstance.waitUntil(function () { return __awaiter(this, void 0, void 0, function* () { try { if (previousHeight !== currentHeight) { previousHeight = currentHeight; currentHeight = yield ayakashiInstance.scrollIn(myProp); return false; } else { return true; } } catch (e) { reject(e); return true; } }); }, scrollInterval, timeout) .then(function () { resolve(); }).catch(function () { resolve(); }); }); }); }); ayakashiInstance.registerAction("infiniteScrollIn", function (prop, options) { return __awaiter(this, void 0, void 0, function* () { const myProp = this.prop(prop); if (!myProp) throw new Error("<infiniteScrollIn> needs a valid prop"); const matchCount = yield myProp.trigger(); if (matchCount === 0) throw new Error("<infiniteScrollIn> needs a prop with at least 1 match"); yield this.scrollIntoView(myProp); let previousHeight = -1; let currentHeight = 0; let pixelsToScroll; if (options && options.pixelsToScroll) pixelsToScroll = options.pixelsToScroll; let scrollInterval = 1000; if (options && options.scrollInterval) scrollInterval = options.scrollInterval; let stopScrollingAfter = 0; if (options && options.stopScrollingAfter) stopScrollingAfter = options.stopScrollingAfter; return new Promise(function (resolve, reject) { ayakashiInstance.waitUntil(function () { return __awaiter(this, void 0, void 0, function* () { try { if (options && typeof options.cb === "function") { yield options.cb(currentHeight); } if (previousHeight !== currentHeight) { previousHeight = currentHeight; currentHeight = yield ayakashiInstance.scrollIn(myProp, pixelsToScroll); return false; } else { return true; } } catch (e) { reject(e); return true; } }); }, scrollInterval, stopScrollingAfter) .then(function () { resolve(); }).catch(function () { resolve(); }); }); }); }); } exports.default = default_1;