Commit ea89ea09 authored by lukas.niegsch's avatar lukas.niegsch

wip: fix misusage of cdp api

parent 4a7c3d19
......@@ -5,6 +5,10 @@ class HeadlessChromium
this.websocket = websocket
}
// session websocket != page websocket
// page websocket can be found at localhost:<port>/json/version
// at webSocketDebuggerUrl where targetId = id
// question? how to make localhost GET request to get this resource ...
async runCommand(command, params = {})
{
return new Promise((resolve, reject) => {
......@@ -19,17 +23,17 @@ class HeadlessChromium
server.close()
var data = JSON.parse(response.data)
if ('result' in data)
{
resolve(data.result)
}
if ('error' in data)
{
reject(data.error)
}
reject(data)
if ('result' in data)
{
resolve(data.result)
}
resolve(data)
}
server.onerror = (error) => {
......@@ -59,21 +63,23 @@ class HeadlessChromium
return await this.runCommand("Page.enable")
}
async openNewPage(url)
async openNewPage(url, width = 1920, height = 1080)
{
var response = await this.runCommand("Target.createTarget", {url: url})
var response = await this.runCommand("Target.createTarget", {url: url, width: width, height: height})
return response.targetId
}
async closePage(page)
{
var response = await this.runCommand("Target.closeTarget", {targetId: page})
return response.success
return await this.runCommand("Target.closeTarget", {targetId: page})
}
async focusPage(page)
{
await this.runCommand("Target.activateTarget", {targetId: page})
var response = await this.runCommand("Target.attachToTarget", {targetId: page})
await this.runCommand(response.method, response.params)
console.log(response)
}
async printToPdf()
......@@ -81,4 +87,14 @@ class HeadlessChromium
var response = await this.runCommand("Page.printToPDF")
return response
}
async canEmulate()
{
return await this.runCommand("Emulation.canEmulate")
}
async getPages()
{
return await this.runCommand("Target.getTargets")
}
}
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", "http://localhost:9222/json/version", false);
xmlHttp.send(null);
console.log(xmlHttp.responseText);
\ No newline at end of file
......@@ -14,8 +14,13 @@
async function html2pdf(url)
{
var pdf = await browser.getPages()
console.log(pdf)
return
var page = await browser.openNewPage(url)
await browser.focusPage(page)
await browser.enablePage()
var pdf = browser.printToPdf()
await browser.closePage(page)
return pdf
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment