Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
let result
let loaded = false
const loading = async (startTime = Date.now()) => {
console.log('loading...')
if (!loaded && Date.now() - startTime < LOAD_TIMEOUT) {
await sleep(100)
await loading(startTime)
}
}
console.log('here 1')
const tab = await Cdp.New({ host: '127.0.0.1' })
console.log('tab', tab)
const client = await Cdp({ host: '127.0.0.1', tab /* , remote: true*/ })
const { Network, Page } = client
console.log('here 2')
Cdp.Version((err, info) => {
console.log('CDP version info', err, info)
})
/* client.on('event', (message) => {
console.log('client event', message)
})*/
Network.requestWillBeSent((params) => {
// console.log(params.request.url)
})
Meteor.setTimeout(() => {
console.log("going to visit",url);
console.log(url);
const chromeParams = {};
if (Spiderable.useLocalChrome) {
chromeParams.port = chrome.port;
} else {
chromeParams.host = Spiderable.chromeIp;
chromeParams.port = Spiderable.chromePort;
}
CDP(chromeParams, (protocol) => {
// Extract the parts of the DevTools protocol we need for the task.
// See API docs: https://chromedevtools.github.io/debugger-protocol-viewer/
const {Page, Runtime} = protocol;
// First, need to enable the domains we're going to use.
Promise.all([
Page.enable(),
Runtime.enable()
]).then(() => {
Page.navigate({url});
// Wait for window.onload before doing stuff.
Page.loadEventFired(() => {
succeeded = true;
checkIfReady(Runtime, () => {
getHtmlContent(Runtime).then((result) => {
export default async function handler (event, context, callback) {
const {
queryStringParameters: { url = 'https://github.com/adieuadieu/serverless-chrome' },
} = event
const requestsMade = []
const [tab] = await Cdp.List()
const client = await Cdp({ host: '127.0.0.1', target: tab })
const { Network, Page } = client
Network.requestWillBeSent(params => requestsMade.push(params))
const loadEventFired = Page.loadEventFired()
// https://chromedevtools.github.io/devtools-protocol/tot/Network/#method-enable
await Network.enable()
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-enable
await Page.enable()
// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-navigate
await Page.navigate({ url })
async _attemptToConnect (port, host) {
return await remoteInterface({ host, port })
.then(client => {
this.client = client;
return true;
})
.catch(() => delay(connectionRetryDelay));
}
const setup = port => new Promise((resolve, reject) => {
chrome({ port }, protocol => {
const { Page, Runtime, Network, DOM, ServiceWorker } = protocol;
Promise.all([
Page.enable(),
Runtime.enable(),
Network.enable(),
DOM.enable(),
ServiceWorker.enable()
]).then(() => {
resolve(protocol);
})
.catch(reject);
}).on('error', err => reject(new Error('Cannot connect to Chrome:' + err)));
});
export async function captureScreenshotOfUrl (url) {
const LOAD_TIMEOUT = (config && config.chrome.pageLoadTimeout) || 1000 * 60
let result
let loaded = false
const loading = async (startTime = Date.now()) => {
if (!loaded && Date.now() - startTime < LOAD_TIMEOUT) {
await sleep(100)
await loading(startTime)
}
}
const [tab] = await Cdp.List()
const client = await Cdp({ host: '127.0.0.1', target: tab })
const { Network, Page } = client
Network.requestWillBeSent((params) => {
log('Chrome is sending request for:', params.request.url)
})
Page.loadEventFired(() => {
loaded = true
})
if (config.logging) {
Cdp.Version((err, info) => {
console.log('CDP version info', err, info)
})
}
return new Promise((resolve) => CDP({
port,
host: 'localhost',
target: (targets) => targets.findIndex((t) => t.type === 'page')
}, resolve))
}
public async open(): Promise {
try {
this.logger.debug(CRIOutputMessages.ATTEMPT_TO_CONNECT);
const { host, port } = this.options;
const chromeRemoteInterface: ChromeRemoteInterface = await connect({
host,
port
});
this.logger.debug(CRIOutputMessages.CONNECTED);
chromeRemoteInterface.once('disconnect', () =>
this.logger.debug(CRIOutputMessages.DISCONNECTED)
);
this.chromeRemoteInterface = chromeRemoteInterface;
} catch (e) {
this.logger.debug(
`${CRIOutputMessages.FAILED_ATTEMPT_TO_CONNECT}: ${e.message}`
);