Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
let framesCaptured = 0
// @TODO: write a better queue, which waits a few seconds when reaching 0
// before emitting "empty"
const requestQueue = []
const loading = async (startTime = Date.now()) => {
log('Request queue size:', requestQueue.length, requestQueue)
if ((!loaded || requestQueue.length > 0) && Date.now() - startTime < LOAD_TIMEOUT) {
await sleep(100)
await loading(startTime)
}
}
const tab = await Cdp.New()
const client = await Cdp({ host: '127.0.0.1', target: tab })
const {
Network, Page, Input, DOM,
} = client
Network.requestWillBeSent((data) => {
// only add requestIds which aren't already in the queue
// why? if a request to http gets redirected to https, requestId remains the same
if (!requestQueue.find(item => item === data.requestId)) {
requestQueue.push(data.requestId)
}
log('Chrome is sending request for:', data.requestId, data.request.url)
})
export async function printUrlToPdf (url, printOptions = {}) {
const LOAD_TIMEOUT = (config && config.chrome.pageLoadTimeout) || 1000 * 20
let result
let loaded = false
const requestQueue = [] // @TODO: write a better quite, which waits a few seconds when reaching 0 before emitting "empty"
const loading = async (startTime = Date.now()) => {
log('Request queue size:', requestQueue.length, requestQueue)
if ((!loaded || requestQueue.length > 0) && Date.now() - startTime < LOAD_TIMEOUT) {
await sleep(100)
await loading(startTime)
}
}
const tab = await Cdp.New()
const client = await Cdp({ host: '127.0.0.1', target: tab })
const { Network, Page } = client
Network.requestWillBeSent((data) => {
// only add requestIds which aren't already in the queue
// why? if a request to http gets redirected to https, requestId remains the same
if (!requestQueue.find(item => item === data.requestId)) {
requestQueue.push(data.requestId)
}
log('Chrome is sending request for:', data.requestId, data.request.url)
})
Network.responseReceived(async (data) => {
// @TODO: handle this better. sometimes images, fonts, etc aren't done loading before we think loading is finished
async createTab() {
const tabCount = Object.keys(this.tabs).length;
if (tabCount < this.maxTab) {
// Create a new target/tab in the remote instance.
// @see https://github.com/cyrus-and/chrome-remote-interface/#cdpnewoptions-callback
const tab = await chrome.New({ port: this.port });
const { id } = tab;
this.tabs[id] = {
free: true,
client: await this.connectTab(id),
};
return id;
}
}
return _regenerator2.default.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
_context3.next = 2;
return CRI.New(localConnectionOptions);
case 2:
tab = _context3.sent;
_context3.next = 5;
return CRI(Object.assign({}, localConnectionOptions, { tab }));
case 5:
client = _context3.sent;
Page = client.Page;
_context3.next = 9;
return Page.enable();
case 9:
if (!options.inject) {
_context3.next = 19;
break;
async function createNewDebuggerInstance() {
debug(
`Launching new tab with debugger at port ${dockerHost}:${chromeDockerPort}`
);
const target = await CDP.New({ host: dockerHost, port: chromeDockerPort });
debug(`Launched with target id ${target.id}`);
const client = await CDP({
host: dockerHost,
port: chromeDockerPort,
target,
});
client.close = () => {
debug('New closing tab');
return CDP.Close({
host: dockerHost,
port: chromeDockerPort,
id: target.id,
});
};
async function newPage(chrome, url) {
const target = await CDP.New({port: chrome.port})
const client = await CDP({target})
const {Page} = client
await Page.enable()
await Page.navigate({url, referrer: ''})
await Page.loadEventFired()
await sleep(200)
return {chrome, Page, client}
}
function getClient(options) {
if (!options || !options.port) {
options = Object.assign(options || {}, {port: Tab.settings.port});
}
return CDP.New(options).then((tab) => {
return CDP({target: tab});
});
}
async function createNewDebuggerInstance() {
debug(`Launching new tab with debugger at port ${host}:${port}`);
const target = await CDP.New({ host, port });
debug(`Launched with target id ${target.id}`);
const client = await CDP({ host, port, target });
client.close = () => {
debug('New closing tab');
return CDP.Close({ host, port, id: target.id });
};
return client;
}
private async connectToChrome(): Promise {
const { host, port } = this.options.cdp
const target = await CDP.New({
port,
host,
})
return await CDP({ target, host, port })
}