Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await this.lockFile();
const launcher: BrowserInfo = await this._launcher.launch('about:blank');
let client;
this._pid = launcher.pid;
/*
* We want a new tab for this session. If it is a new browser, a new tab
* will be created automatically. If it was already there, then we need
* to create it ourselves.
*/
if (launcher.isNew) {
// Chrome Launcher could return extensions tabs if installed them but we don't need them.
const tabs = filter(await cdp.List({ port: launcher.port }), (tab: any) => { // eslint-disable-line new-cap
return !tab.url.startsWith('chrome-extension');
});
// Can assume not-null as `this._launcher.launch` always will return a port.
client = await this.getClient(launcher.port!, tabs[0]);
this._tabs = tabs;
} else {
const tab = await cdp.New({ port: launcher.port, url: null }); // eslint-disable-line new-cap
if (!tab) {
throw new Error('Error trying to open a new tab');
}
this._tabs.push(tab);
client = await cdp({
static async new(maxTab = Infinity) {
const port = await sysFreePort();
const chromeTabsPoll = new ChromeTabsPoll();
chromeTabsPoll.port = port;
chromeTabsPoll.chromeLauncher = await launchChrome(port);
chromeTabsPoll.tabs = {};
chromeTabsPoll.maxTab = maxTab;
chromeTabsPoll.requireResolveTasks = [];
// Request the list of the available open targets/tabs of the remote instance.
// @see https://github.com/cyrus-and/chrome-remote-interface/#cdplistoptions-callback
const tabs = await chrome.List({ port });
for (let i = 0; i < tabs.length; i++) {
const tab = tabs[i];
const { id, type } = tab;
// ignore background_page
if (type === 'page') {
chromeTabsPoll.tabs[id] = {
free: true,
client: await chromeTabsPoll.connectTab(id),
};
}
}
return chromeTabsPoll;
}
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)
})
export default async function captureScreenshotOfUrl (url, mobile = false) {
const LOAD_TIMEOUT = process.env.PAGE_LOAD_TIMEOUT || 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, Runtime, Emulation,
} = client
Network.requestWillBeSent((params) => {
log('Chrome is sending request for:', params.request.url)
})
Page.loadEventFired(() => {
loaded = true
})
try {
await Promise.all([Network.enable(), Page.enable()])
function connectClient() {
if (!getValue("chrome.debug")) {
return Promise.resolve(createTabs([]));
}
return CDP.List({
port: getValue("chrome.port"),
host: getValue("chrome.host")
})
.then(tabs => createTabs(tabs, {
clientType: "chrome", type: "page"
}));
}
async function findPageToConnectTo (options) {
const targets = await CRI.List(options)
let backup
let i = 0
for (; i < targets.length; i++) {
const target = targets[i]
if (target.webSocketDebuggerUrl) {
if (target.type === 'page') {
return target.webSocketDebuggerUrl
} else {
backup = target
}
}
}
if (backup) return backup.webSocketDebuggerUrl
throw new Error('Squidwarc could not find a browser page to connect to')
}
async function connectClient() {
if (!getValue("chrome.debug")) {
return createTabs([]);
}
try {
const tabs = await CDP.List({
port: getValue("chrome.port"),
host: getValue("chrome.host")
});
return createTabs(tabs, {
clientType: "chrome",
type: "page"
});
} catch (e) {
return [];
}
}
function connectNodeClient() {
if (!getValue("node.debug")) {
return Promise.resolve(createTabs([]));
}
return CDP.List({
port: getValue("node.port"),
host: getValue("node.host")
})
.then(tabs => createTabs(tabs, {
clientType: "node", type: "node"
}));
}
static List (...args) {
return CDP.List(...args)
}
list = async () => {
let targets;
try {
targets = await CDP.List(await this._getDefaultOptions());
} catch (e) {
echoerr(this._nvim, e.message);
}
if (!targets) {
return;
}
const labels = targets.map(
({ id, title, url }) => `${id}: ${title} - ${url}`,
);
if (labels.length == 0) {
echomsg(this._nvim, 'No targets available.');
} else {
await this._nvim.call('fzf#run', {