How to use _onMessage method in Puppeteer

Best JavaScript code snippet using puppeteer

Connections.js

Source:Connections.js Github

copy

Full Screen

1// Copyright (c) 2015 The Chromium Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4import * as Common from '../common/common.js';5import * as Host from '../host/host.js';6import * as ProtocolClient from '../protocol_client/protocol_client.js';7import {TargetManager} from './SDKModel.js';8/**9 * @implements {ProtocolClient.InspectorBackend.Connection}10 */11export class MainConnection {12 constructor() {13 this._onMessage = null;14 this._onDisconnect = null;15 this._messageBuffer = '';16 this._messageSize = 0;17 this._eventListeners = [18 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(19 Host.InspectorFrontendHostAPI.Events.DispatchMessage, this._dispatchMessage, this),20 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(21 Host.InspectorFrontendHostAPI.Events.DispatchMessageChunk, this._dispatchMessageChunk, this),22 ];23 }24 /**25 * @override26 * @param {function((!Object|string))} onMessage27 */28 setOnMessage(onMessage) {29 this._onMessage = onMessage;30 }31 /**32 * @override33 * @param {function(string)} onDisconnect34 */35 setOnDisconnect(onDisconnect) {36 this._onDisconnect = onDisconnect;37 }38 /**39 * @override40 * @param {string} message41 */42 sendRawMessage(message) {43 if (this._onMessage) {44 Host.InspectorFrontendHost.InspectorFrontendHostInstance.sendMessageToBackend(message);45 }46 }47 /**48 * @param {!Common.EventTarget.EventTargetEvent} event49 */50 _dispatchMessage(event) {51 if (this._onMessage) {52 this._onMessage.call(null, /** @type {string} */ (event.data));53 }54 }55 /**56 * @param {!Common.EventTarget.EventTargetEvent} event57 */58 _dispatchMessageChunk(event) {59 const messageChunk = /** @type {string} */ (event.data['messageChunk']);60 const messageSize = /** @type {number} */ (event.data['messageSize']);61 if (messageSize) {62 this._messageBuffer = '';63 this._messageSize = messageSize;64 }65 this._messageBuffer += messageChunk;66 if (this._messageBuffer.length === this._messageSize) {67 this._onMessage.call(null, this._messageBuffer);68 this._messageBuffer = '';69 this._messageSize = 0;70 }71 }72 /**73 * @override74 * @return {!Promise}75 */76 disconnect() {77 const onDisconnect = this._onDisconnect;78 Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners);79 this._onDisconnect = null;80 this._onMessage = null;81 if (onDisconnect) {82 onDisconnect.call(null, 'force disconnect');83 }84 return Promise.resolve();85 }86}87/**88 * @implements {ProtocolClient.InspectorBackend.Connection}89 */90export class WebSocketConnection {91 /**92 * @param {string} url93 * @param {function()} onWebSocketDisconnect94 */95 constructor(url, onWebSocketDisconnect) {96 this._socket = new WebSocket(url);97 this._socket.onerror = this._onError.bind(this);98 this._socket.onopen = this._onOpen.bind(this);99 this._socket.onmessage = messageEvent => {100 if (this._onMessage) {101 this._onMessage.call(null, /** @type {string} */ (messageEvent.data));102 }103 };104 this._socket.onclose = this._onClose.bind(this);105 this._onMessage = null;106 this._onDisconnect = null;107 this._onWebSocketDisconnect = onWebSocketDisconnect;108 this._connected = false;109 this._messages = [];110 }111 /**112 * @override113 * @param {function((!Object|string))} onMessage114 */115 setOnMessage(onMessage) {116 this._onMessage = onMessage;117 }118 /**119 * @override120 * @param {function(string)} onDisconnect121 */122 setOnDisconnect(onDisconnect) {123 this._onDisconnect = onDisconnect;124 }125 _onError() {126 this._onWebSocketDisconnect.call(null);127 // This is called if error occurred while connecting.128 this._onDisconnect.call(null, 'connection failed');129 this._close();130 }131 _onOpen() {132 this._socket.onerror = console.error;133 this._connected = true;134 for (const message of this._messages) {135 this._socket.send(message);136 }137 this._messages = [];138 }139 _onClose() {140 this._onWebSocketDisconnect.call(null);141 this._onDisconnect.call(null, 'websocket closed');142 this._close();143 }144 /**145 * @param {function()=} callback146 */147 _close(callback) {148 this._socket.onerror = null;149 this._socket.onopen = null;150 this._socket.onclose = callback || null;151 this._socket.onmessage = null;152 this._socket.close();153 this._socket = null;154 this._onWebSocketDisconnect = null;155 }156 /**157 * @override158 * @param {string} message159 */160 sendRawMessage(message) {161 if (this._connected) {162 this._socket.send(message);163 } else {164 this._messages.push(message);165 }166 }167 /**168 * @override169 * @return {!Promise}170 */171 disconnect() {172 let fulfill;173 const promise = new Promise(f => fulfill = f);174 this._close(() => {175 if (this._onDisconnect) {176 this._onDisconnect.call(null, 'force disconnect');177 }178 fulfill();179 });180 return promise;181 }182}183/**184 * @implements {ProtocolClient.InspectorBackend.Connection}185 */186export class StubConnection {187 constructor() {188 this._onMessage = null;189 this._onDisconnect = null;190 }191 /**192 * @override193 * @param {function((!Object|string))} onMessage194 */195 setOnMessage(onMessage) {196 this._onMessage = onMessage;197 }198 /**199 * @override200 * @param {function(string)} onDisconnect201 */202 setOnDisconnect(onDisconnect) {203 this._onDisconnect = onDisconnect;204 }205 /**206 * @override207 * @param {string} message208 */209 sendRawMessage(message) {210 setTimeout(this._respondWithError.bind(this, message), 0);211 }212 /**213 * @param {string} message214 */215 _respondWithError(message) {216 const messageObject = JSON.parse(message);217 const error = {218 message: 'This is a stub connection, can\'t dispatch message.',219 code: ProtocolClient.InspectorBackend.DevToolsStubErrorCode,220 data: messageObject221 };222 if (this._onMessage) {223 this._onMessage.call(null, {id: messageObject.id, error: error});224 }225 }226 /**227 * @override228 * @return {!Promise}229 */230 disconnect() {231 if (this._onDisconnect) {232 this._onDisconnect.call(null, 'force disconnect');233 }234 this._onDisconnect = null;235 this._onMessage = null;236 return Promise.resolve();237 }238}239/**240 * @implements {ProtocolClient.InspectorBackend.Connection}241 */242export class ParallelConnection {243 /**244 * @param {!ProtocolClient.InspectorBackend.Connection} connection245 * @param {string} sessionId246 */247 constructor(connection, sessionId) {248 this._connection = connection;249 this._sessionId = sessionId;250 this._onMessage = null;251 this._onDisconnect = null;252 }253 /**254 * @override255 * @param {function(!Object)} onMessage256 */257 setOnMessage(onMessage) {258 this._onMessage = onMessage;259 }260 /**261 * @override262 * @param {function(string)} onDisconnect263 */264 setOnDisconnect(onDisconnect) {265 this._onDisconnect = onDisconnect;266 }267 /**268 * @override269 * @param {string} message270 */271 sendRawMessage(message) {272 const messageObject = JSON.parse(message);273 // If the message isn't for a specific session, it must be for the root session.274 if (!messageObject.sessionId) {275 messageObject.sessionId = this._sessionId;276 }277 this._connection.sendRawMessage(JSON.stringify(messageObject));278 }279 /**280 * @override281 * @return {!Promise}282 */283 disconnect() {284 if (this._onDisconnect) {285 this._onDisconnect.call(null, 'force disconnect');286 }287 this._onDisconnect = null;288 this._onMessage = null;289 return Promise.resolve();290 }291}292/**293 * @param {function():!Promise<undefined>} createMainTarget294 * @param {function()} websocketConnectionLost295 * @return {!Promise}296 */297export async function initMainConnection(createMainTarget, websocketConnectionLost) {298 ProtocolClient.InspectorBackend.Connection.setFactory(_createMainConnection.bind(null, websocketConnectionLost));299 await createMainTarget();300 Host.InspectorFrontendHost.InspectorFrontendHostInstance.connectionReady();301 Host.InspectorFrontendHost.InspectorFrontendHostInstance.events.addEventListener(302 Host.InspectorFrontendHostAPI.Events.ReattachMainTarget, () => {303 TargetManager.instance().mainTarget().router().connection().disconnect();304 createMainTarget();305 });306 return Promise.resolve();307}308/**309 * @param {function()} websocketConnectionLost310 * @return {!ProtocolClient.InspectorBackend.Connection}311 */312export function _createMainConnection(websocketConnectionLost) {313 const wsParam = Root.Runtime.queryParam('ws');314 const wssParam = Root.Runtime.queryParam('wss');315 if (wsParam || wssParam) {316 const ws = wsParam ? `ws://${wsParam}` : `wss://${wssParam}`;317 return new WebSocketConnection(ws, websocketConnectionLost);318 }319 if (Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {320 return new StubConnection();321 }322 return new MainConnection();...

Full Screen

Full Screen

fileUploader.ts

Source:fileUploader.ts Github

copy

Full Screen

...12 const reader = new FileReader();13 reader.onload = () => {14 fileItem.url = reader.result;15 fileItem.status = FileStatus.Ready;16 this._onMessage('ready', fileItem);17 };18 reader.readAsDataURL(file);19 this.fileList.push(fileItem);20 }21 }22 if (this.params.autoUpload) {23 this.uploadAll();24 }25 }26 uploadAll() {27 const items = this.fileList.filter(item => item.status === FileStatus.Ready);28 if (!items.length) {29 return;30 }31 this.uploadFile(items[0]);32 }33 private _isValidFile(item: FileItem): boolean {34 item.id = item.file.size + item.file.name;35 if (item.file.size > this.params.maxLength) {36 item.status = FileStatus.Fail;37 item.error = `文件大小不能超过${this.params.maxLength / 1024 / 1024}M38 <br>文件名:${item.file.name}`;39 this._onMessage('error', item);40 return false;41 }42 if (this.params.exts) {43 const exts = this.params.exts.split(',');44 let reg = '(';45 let i = 0;46 for (const e of exts) {47 if (i++ < 1) {48 reg += '.' + e;49 } else {50 reg += '|.' + e;51 }52 }53 reg += ')$';54 if (!new RegExp(reg).test(item.file.name)) {55 item.status = FileStatus.Fail;56 item.error = '图片格式必须为:' + this.params.exts;57 this._onMessage('error', item);58 return false;59 }60 }61 return true;62 }63 private uploadFile(fileItem: FileItem): void {64 fileItem.status = FileStatus.Uploading;65 const xhr = new XMLHttpRequest();66 const form = new FormData();67 form.append(this.params.field, fileItem.file, fileItem.file.name);68 if (this.params.fields) {69 // tslint:disable-next-line:forin70 for (const k in this.params.fields) {71 form.append(k, this.params.fields[k]);72 }73 }74 xhr.upload.onprogress = event => {75 fileItem.progress = Math.round(event.lengthComputable ? (event.loaded * 100) / event.total : 0);76 this._onMessage('progress', fileItem);77 };78 xhr.upload.onabort = e => {79 fileItem.status = FileStatus.Cancel;80 this._onMessage('cancel', fileItem);81 this._onNext();82 };83 xhr.upload.onerror = e => {84 fileItem.status = FileStatus.Fail;85 fileItem.error = '文件上传错误';86 this._onMessage('error', fileItem);87 this._onNext();88 };89 xhr.onreadystatechange = () => {90 if (xhr.readyState === XMLHttpRequest.DONE) {91 fileItem.status = FileStatus.Success;92 try {93 // tslint:disable-next-line:triple-equals94 if (xhr.status == 404) {95 fileItem.status = FileStatus.Fail;96 fileItem.error = '文件上传错误:404';97 this._onMessage('error', fileItem);98 // tslint:disable-next-line:triple-equals99 } else if (xhr.status == 413) {100 fileItem.status = FileStatus.Fail;101 fileItem.error = '上传文件太大';102 try {103 const response = JSON.parse(xhr.responseText);104 if (response && response.error) {105 fileItem.error = '上传文件太大:' + response.error;106 }107 } catch (e) {}108 this._onMessage('error', fileItem);109 } else {110 const response = JSON.parse(xhr.responseText);111 // tslint:disable-next-line:triple-equals112 if (xhr.status == 200) {113 if (response && response.error) {114 fileItem.status = FileStatus.Fail;115 fileItem.error = '文件上传错误:' + response.error;116 this._onMessage('error', fileItem);117 } else {118 fileItem.url = response.url;119 fileItem.status = FileStatus.Success;120 this._onMessage('complete', fileItem);121 }122 } else {123 fileItem.status = FileStatus.Fail;124 if (response && response.error) {125 fileItem.error = '文件上传错误:' + response.error;126 }127 this._onMessage('error', fileItem);128 }129 }130 this._onNext();131 } catch (e) {132 fileItem.status = FileStatus.Fail;133 fileItem.error = '文件上传错误';134 this._onMessage('error', fileItem);135 }136 }137 };138 xhr.open('POST', this.params.url, true);139 if (this.params.withCredentials) {140 xhr.withCredentials = true;141 }142 if (this.params.headers) {143 Object.keys(this.params.headers).forEach(key => {144 xhr.setRequestHeader(key, this.params.headers[key]);145 });146 }147 xhr.send(form);148 }149 private _onNext() {150 const items = this.fileList.filter(item => item.status === FileStatus.Ready);151 if (!items.length) {152 return this._onMessage('completeAll', null);153 }154 this.uploadFile(items[0]);155 }156 private _onMessage(event: string, fileItem: FileItem) {157 this.emitter.emit({158 event,159 fileItem160 });161 }...

Full Screen

Full Screen

message.ts

Source:message.ts Github

copy

Full Screen

...53 return (fn: CallbackMap[T]) => {54 callbackMap[key] = fn || noop55 }56 }57 const onMessage = _onMessage('message')58 const onDanMuMsg = _onMessage('danMuMsg')59 const onSendGift = _onMessage('sendGift')60 const onSuperChart = _onMessage('superChartMsg')61 const onInteractWord = _onMessage('interact')62 const onEntryEffect = _onMessage('entryEffect')63 const onGuardBuy = _onMessage('guardBuy')64 const onLiveStart = _onMessage('liveStart')65 const onLiveEnd = _onMessage('liveEnd')66 const onOperation = _onMessage('operation')67 const onCuteOff = _onMessage('cuteOff')68 const onOpen = _onMessage('open')69 const onClose = _onMessage('close')70 const onError = _onMessage('error')71 // eslint-disable-next-line @typescript-eslint/no-explicit-any72 function messageListener(_event: unknown, cmd: string, data: any, rawData: any) {73 callbackMap.message(cmd, data, rawData)74 switch (cmd) {75 case LiveCmd.DANMU_MSG:76 callbackMap.danMuMsg(data as DanMuMsgOption, rawData)77 break78 case LiveCmd.SEND_GIFT:79 callbackMap.sendGift(data as SendGiftOption, rawData)80 break81 case LiveCmd.SUPER_CHAT_MESSAGE:82 case LiveCmd.SUPER_CHAT_MESSAGE_JP:83 callbackMap.superChartMsg(data as SuperChatMessageOption, rawData)84 break...

Full Screen

Full Screen

sitehandler-social-global.js

Source:sitehandler-social-global.js Github

copy

Full Screen

1var getLogger = require('../lib/logger');2/**3 * Site Handler for Social4 * - This creates a single global buddylist for 5 * all active WebSocket connections6 **/7function GlobalSocialSiteHandler(appid) {8 "use strict";9 this.appid = appid;10 this.logger = getLogger(appid);11 this.clients = {}; //Store active connections12}13/**14 * Add a new WebSocket client to the global buddylist15 * Set the appropriate listeners on the WebSocket16 **/17GlobalSocialSiteHandler.prototype.addConnection = function(username, ws) {18 "use strict";19 this.logger.debug(username+'.addConnection: enter');20 // Store new client21 this.clients[username] = ws;22 ws.on('message', this._onMessage.bind(this, username));23 ws.on('close', this._onClose.bind(this, username));24 // Send back the global buddy list25 ws.send(JSON.stringify({26 'cmd': "state",27 'userId': username,28 'roster': this.getAllUsers()29 }));30 // Inform others of the new guy31 this.broadcastStatus(username, true);32 this.logger.trace(username+'.addConnection: exit');33};34/**35 * Retrieve an array of all active users36 **/37GlobalSocialSiteHandler.prototype.getAllUsers = function() {38 "use strict";39 this.logger.trace('getAllUsers: enter');40 var ret = [];41 for (var k in this.clients) {42 if (this.clients.hasOwnProperty(k)) {43 ret.push(k);44 }45 }46 this.logger.debug('getAllUsers: returns ' + JSON.stringify(ret));47 this.logger.trace('getAllUsers: exit');48 return ret;49};50/**51 * Send a message to all users, informing them that the target52 * user is now online/offline53 **/ 54GlobalSocialSiteHandler.prototype.broadcastStatus = function(username, online) {55 "use strict";56 this.logger.trace('broadcastStatus: enter');57 this.logger.debug('broadcastStatus: '+username+' online='+online);58 for (var k in this.clients) {59 if (this.clients.hasOwnProperty(k)) {60 try {61 this.clients[k].send(JSON.stringify({62 'cmd': 'roster',63 'userId': username,64 'online': online65 }));66 } catch (e) {67 this.logger.warn('broadcastStatus: failed to send message to ' + k);68 this.logger.warn(e);69 }70 }71 }72 this.logger.trace('broadcastStatus: exit');73};74/**75 * Handler for incoming message on a WebSocket connection76 **/77GlobalSocialSiteHandler.prototype._onMessage = function(username, msg) {78 "use strict";79 this.logger.debug(username+'._onMessage: enter');80 try {81 var parsedMsg = JSON.parse(msg);82 if (!parsedMsg.hasOwnProperty("cmd")) {83 this.logger.warn(username+"._onMessage: malformed message: "+msg);84 } else if (parsedMsg.cmd === "ping") {85 this.clients[username].send(JSON.stringify({ cmd: "pong" }));86 } else if (parsedMsg.cmd === "send") {87 if (this.clients.hasOwnProperty(parsedMsg.to)) {88 this.clients[parsedMsg.to].send(JSON.stringify({89 'cmd': 'message',90 'from': username,91 'msg': parsedMsg.msg92 }));93 this.logger.debug(username+'._onMessage: message forwarded to ' + parsedMsg.to);94 } else {95 this.logger.error(username+'._onMessage: message not sent, no connection to ' + parsedMsg.to);96 }97 }98 } catch (e) {99 this.logger.error(username+'._onMessage: failed handling message: '+msg);100 this.logger.error(e);101 }102 this.logger.trace(username+'._onMessage: exit');103};104/**105 * Handler for 'close' event from a WebSocket106 **/107GlobalSocialSiteHandler.prototype._onClose = function(username) {108 "use strict";109 this.logger.debug(username+'._onClose: enter');110 delete this.clients[username];111 this.broadcastStatus(username, false);112 this.logger.trace('_onClose: exit');113};...

Full Screen

Full Screen

AgoricIframeMessenger.js

Source:AgoricIframeMessenger.js Github

copy

Full Screen

...47 const ev = new CustomEvent('open', { detail: { send: this.send } });48 this._origin = new URL(this.src).origin;49 this.dispatchEvent(ev);50 }51 _onMessage(event) {52 // console.log('iframe message', event);53 if (event.source !== this._contentWindow) {54 return;55 }56 event.preventDefault();57 const ev = new CustomEvent('message', {58 detail: { data: event.data, send: this.send },59 });60 this.dispatchEvent(ev);61 }62 _onError(event) {63 event.preventDefault();64 const ev = new CustomEvent('error', { detail: { error: event.error } });65 this.dispatchEvent(ev);...

Full Screen

Full Screen

useMessages.js

Source:useMessages.js Github

copy

Full Screen

1import { useEffect, useCallback } from 'react';2import { MessageService } from '@message/api/MessageService';3import { useInfiniteQuery, useMutation, queryCache } from 'react-query';4import { constants } from '@c4/shared';5import { useWebsockets } from '@core/hooks/useWebsockets';6const { EVENTS } = constants;7const ITEMS_PER_PAGE = 30;8export function useMessages(id) {9 const { on } = useWebsockets({ connectOnMount: false });10 // TODO Maybe write in the cache when we get the response ?11 // We would need the server not to push the websocket event to sender the then12 const [createMessage] = useMutation(MessageService.createMessage, {13 throwOnError: true14 });15 const messages = useInfiniteQuery(16 ['messages', id],17 (key, _, offset) => {18 if (id) return MessageService.getGameMessages(id, { offset });19 return MessageService.getLobbyMessages({ offset });20 },21 {22 retry: false,23 refetchOnWindowFocus: false,24 getFetchMore: (lastPage, allPages) => {25 if (lastPage.length < ITEMS_PER_PAGE) return null;26 return allPages.reduce((total, current) => total + current.length, 0);27 }28 }29 );30 const _onMessage = useCallback(async dto => {31 if (dto.gameId !== id) return;32 33 const currentData = queryCache.getQueryData(['messages', id]);34 currentData[0].unshift(await MessageService.processDTO(dto));35 queryCache.setQueryData(['messages', id], currentData);36 }, [id]);37 useEffect(() => {38 const unsub = on(EVENTS.NEW_GAME_MESSAGE, _onMessage);39 return unsub;40 }, [on, id, messages, _onMessage]);41 useEffect(() => {42 const unsub = on(EVENTS.NEW_LOBBY_MESSAGE, _onMessage);43 return unsub;44 }, [on, id, messages, _onMessage]);45 return {46 messages,47 createMessage,48 get allMessages() {49 return messages.data?.flat();50 }51 };...

Full Screen

Full Screen

websocket_client.js

Source:websocket_client.js Github

copy

Full Screen

1const RECONNECT_TIMEOUT_MS = 50002export default class WebSocketClient {3 constructor (wsEndpoint, onEventFn) {4 this.wsEndpoint = wsEndpoint5 this.onEventFn = onEventFn6 this.ws = null7 this._connect = this._connect.bind(this)8 this._reconnect = this._reconnect.bind(this)9 this._onClose = this._onClose.bind(this)10 this._onMessage = this._onMessage.bind(this)11 this._onError = this._onError.bind(this)12 this._connect()13 }14 _connect () {15 this.ws = new WebSocket(this.wsEndpoint)16 this.ws.addEventListener('message', this._onMessage)17 this.ws.addEventListener('error', this._onError)18 this.ws.addEventListener('close', this._onClose)19 }20 _reconnect() {21 // eslint-disable-next-line22 console.log('WebSocket reconnecting')23 this.ws.removeEventListener('message', this._onMessage)24 this.ws.removeEventListener('error', this._onError)25 this.ws.removeEventListener('close', this._onClose)26 this._connect()27 }28 _onClose () {29 const randomReconnectTimeoutMs = RECONNECT_TIMEOUT_MS + Math.floor(Math.random() * RECONNECT_TIMEOUT_MS)30 setTimeout(this._reconnect, randomReconnectTimeoutMs)31 }32 _onMessage (e) {33 this.onEventFn(JSON.parse(e.data))34 }35 _onError (e) {36 // eslint-disable-next-line37 console.error('WebSocket error', e)38 this.ws.close()39 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PuppeteerCrawler } = require('apify');2const puppeteer = require('puppeteer');3const crawler = new PuppeteerCrawler({4 handlePageFunction: async ({ request, page }) => {5 await page.goto(request.url);6 await page.waitForSelector('a');7 const links = await page.$$eval('a', as => as.map(a => a.href));8 await Apify.pushData({ url: request.url, links });9 },10 _onMessage: async ({ request, page, puppeteerPool }) => {11 if (request.userData.label === 'GOTO') {12 await page.goto(request.url);13 await page.waitForSelector('a');14 const links = await page.$$eval('a', as => as.map(a => a.href));15 await Apify.pushData({ url: request.url, links });16 }17 if (request.userData.label === 'CLICK') {18 await page.click(request.userData.selector);19 await page.waitForSelector('a');20 const links = await page.$$eval('a', as => as.map(a => a.href));21 await Apify.pushData({ url: request.url, links });22 }23 if (request.userData.label === 'RELOAD') {24 await page.reload();25 await page.waitForSelector('a');26 const links = await page.$$eval('a', as => as.map(a => a.href));27 await Apify.pushData({ url: request.url, links });28 }29 if (request.userData.label === 'GOTO_AND_CLICK') {30 await page.goto(request.url);31 await page.click(request.userData.selector);32 await page.waitForSelector('a');33 const links = await page.$$eval('a', as => as.map(a => a.href));34 await Apify.pushData({ url: request.url, links });35 }36 },37 _handleFailedRequestFunction: async ({ request }) => {38 console.log(`Request ${request.url} failed too many times`);39 },40});41await crawler.run();42const { Apify } = require('apify');43Apify.main(async () => {44 const requestList = await Apify.openRequestList('my-list', [45 { url:

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PuppeteerCrawler } = require('apify');2const crawler = new PuppeteerCrawler({3 handlePageFunction: async ({ page, request }) => {4 const title = await page.title();5 console.log(`Title of ${request.url}: ${title}`);6 },7 _onMessage: async ({ page, request, message }) => {8 console.log(`Message received from page ${request.url}: ${message}`);9 },10});11await crawler.run();12const { Apify } = require('apify');13Apify.main(async () => {14 const requestQueue = await Apify.openRequestQueue();15 const crawler = new Apify.PuppeteerCrawler({16 handlePageFunction: async ({ page, request }) => {17 await page.evaluate(() => {18 window.postMessage('Hello from the page!', '*');19 });20 },21 _onMessage: async ({ page, request, message }) => {22 console.log(`Message received from page ${request.url}: ${message}`);23 },24 });25 await crawler.run();26 console.log('Crawler finished.');27});

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PuppeteerCrawler } = require('apify');2const cheerio = require('cheerio');3const crawler = new PuppeteerCrawler({4 handlePageFunction: async ({ page, request }) => {5 const html = await page.content();6 const $ = cheerio.load(html);7 const title = $('title').text();8 console.log(`Title of ${request.url}: ${title}`);9 },10 handleFailedRequestFunction: async ({ request }) => {11 console.log(`Request ${request.url} failed too many times`);12 },13 launchPuppeteerOptions: {14 },15 sessionPoolOptions: {16 sessionOptions: {17 },18 },19 proxyConfiguration: {20 },21 _onMessage: async ({ page, request, response, body, contentType, json }, session, browserController) => {22 console.log('Received message from page.evaluate()');23 },24});25await crawler.run();26const { PlaywrightCrawler } = require('apify');27const crawler = new PlaywrightCrawler({28 handlePageFunction: async ({ page, request }) => {29 const title = await page.title();30 console.log(`Title of ${request.url}: ${title}`);31 },32 handleFailedRequestFunction: async ({ request }) => {33 console.log(`Request ${request.url} failed too many times`);34 },35 launchPuppeteerOptions: {36 },37 sessionPoolOptions: {

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PuppeteerCrawler } = require('apify');2const crawler = new PuppeteerCrawler({3 handlePageFunction: async ({ request, page }) => {4 },5 handleFailedRequestFunction: async ({ request }) => {6 },7 _onMessage: async (msg) => {8 console.log(`Received message: ${msg}`);9 },10});11await crawler.run();12const { CheerioCrawler } = require('apify');13const crawler = new CheerioCrawler({14 handlePageFunction: async ({ request, $ }) => {15 },16 handleFailedRequestFunction: async ({ request }) => {17 },18 _onMessage: async (msg) => {19 console.log(`Received message: ${msg}`);20 },21});22await crawler.run();23const { BasicCrawler } = require('apify');24const crawler = new BasicCrawler({25 handleRequestFunction: async ({ request }) => {26 },27 handleFailedRequestFunction: async ({ request }) => {28 },29 _onMessage: async (msg) => {30 console.log(`Received message: ${msg}`);31 },32});33await crawler.run();34const { AutoscaledPool } = require('apify');35const pool = new AutoscaledPool({36 runTaskFunction: async (task) => {37 },38 isTaskReadyFunction: async () => {39 },40 isFinishedFunction: async () => {41 },42 _onMessage: async (msg) => {43 console.log(`Received message: ${msg}`);44 },45});46await pool.run();47const { RequestList } = require('apify');48const requestList = new RequestList({49 _onMessage: async (msg) => {50 console.log(`Received message: ${msg}`);51 },52});53await requestList.initialize();

Full Screen

Using AI Code Generation

copy

Full Screen

1 const crawler = new Apify.PuppeteerCrawler({2 handlePageFunction: async ({ page, request }) => {3 await Apify.utils.puppeteer.injectJQuery(page);4 await page.waitForSelector('.content');5 await Apify.utils.puppeteer.blockRequests(page, {6 });7 const data = await page.evaluate(() => {8 const $ = window.$;9 const result = {};10 result.title = $('.title').text();11 result.description = $('.description').text();12 result.url = $('.url').text();13 result.image = $('.image').text();14 result.date = $('.date').text();15 result.tags = $('.tags').text();16 result.content = $('.content').text();17 return result;18 });19 await Apify.pushData(data);20 },21 handleFailedRequestFunction: async ({ request }) => {22 console.log(`Request ${request.url} failed too many times`);23 await Apify.pushData({24 '#debug': Apify.utils.createRequestDebugInfo(request),25 });26 },27 });28 await crawler.run();29 console.log('Crawl finished.');30});31const crawler = new Apify.CheerioCrawler({32 handlePageFunction: async ({ request, $ }) => {33 const data = {};34 data.title = $('title').text();35 data.description = $('meta[name="description"]').attr('content');36 data.url = request.url;37 data.image = $('meta[property="og:image"]').attr('content');38 data.date = $('meta[property="article:published_time"]').attr('content');39 data.tags = $('meta[property="article:tag"]').attr('content');40 data.content = $('article').text();41 await Apify.pushData(data);42 },43 handleFailedRequestFunction: async ({ request }) => {44 console.log(`Request ${request.url} failed too many times`);45 await Apify.pushData({46 '#debug': Apify.utils.createRequestDebugInfo(request),

Full Screen

Using AI Code Generation

copy

Full Screen

1const { PuppeteerCrawler } = require('apify');2const crawler = new PuppeteerCrawler({3 handlePageFunction: async ({ request, response, page }) => {4 console.log(`Response status: ${response.status()}`);5 },6 handleFailedRequestFunction: async ({ request }) => {7 console.log(`Request ${request.url} failed too many times`);8 },9});10await crawler.run();11const { PuppeteerCrawler } = require('apify');12const crawler = new PuppeteerCrawler({13 handlePageFunction: async ({ request, response, page }) => {14 console.log(`Response status: ${response.status()}`);15 },16 handleFailedRequestFunction: async ({ request }) => {17 console.log(`Request ${request.url} failed too many times`);18 },19});20await crawler.run();

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const {PuppeteerExtraPlugin} = require('puppeteer-extra-plugin');3const pluginName = 'recaptchaPlugin';4class recaptchaPlugin extends PuppeteerExtraPlugin {5 constructor(opts = {}) {6 super(opts);7 }8 get name() {9 return pluginName;10 }11 async onPageCreated(page) {12 await page.exposeFunction('recaptchaCallback', () => {13 console.log('Recaptcha callback called');14 });15 await page.evaluateOnNewDocument(() => {16 window.recaptchaCallback = recaptchaCallback;17 });18 }19 async onPageLoad(page) {20 await page.evaluateOnNewDocument(() => {21 window.recaptchaCallback = recaptchaCallback;22 });23 }24 async _onMessage(message, ...args) {25 if (message === 'recaptchaCallback') {26 console.log('Recaptcha callback called');27 }28 }29}30module.exports = function (pluginConfig) {31 return new recaptchaPlugin(pluginConfig);32};33const puppeteer = require('puppeteer');34const recaptchaPlugin = require('./test');35(async () => {36 const browser = await puppeteer.launch({

Full Screen

Using AI Code Generation

copy

Full Screen

1const puppeteer = require('puppeteer');2const { RemoteControl } = require('puppeteer-remote-control');3const remoteControl = new RemoteControl();4(async () => {5 const browser = await puppeteer.launch();6 const page = await browser.newPage();7 await remoteControl._onMessage({8 params: {9 }10 }, page);11 await browser.close();12})();13#### `constructor(options)`

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Puppeteer automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful