Commit 4a7c3d19 authored by lukas.niegsch's avatar lukas.niegsch

refactored syntax

parent 30768ecb
class HeadlessChromium class HeadlessChromium
{ {
constructor(websocketUrl) constructor(websocket)
{ {
this.websocketUrl = websocketUrl this.websocket = websocket
this.websocket = new WebSocket(websocketUrl)
} }
async runCommand(command, params = {})
{
return new Promise((resolve, reject) => {
var server = new WebSocket(this.websocket)
server.onopen = () => {
var message = {id: 0, method: command, params: params}
server.send(JSON.stringify(message))
}
server.onmessage = (response) => {
server.close()
var data = JSON.parse(response.data)
if ('result' in data)
{
resolve(data.result)
}
isConnected() if ('error' in data)
{
reject(data.error)
}
reject(data)
}
server.onerror = (error) => {
server.close()
reject(error)
}
})
}
async getVersion()
{ {
return this.websocket.readyState == WebSocket.OPEN return await this.runCommand("Browser.getVersion")
} }
runCommand(method, params = {}, callback) async enableDOM()
{ {
var command = {id: 0, method: method, params: params} return await this.runCommand("DOM.enable")
this.websocket.send(JSON.stringify(command))
this.websocket.onmessage = (response) => {
callback(JSON.parse(response.data))
}
} }
openNewPage(url, callback) async enableNetwork()
{ {
this.runCommand("Target.createTarget", {url: url}, (response) => { return await this.runCommand("Network.enable")
callback(response.result.targetId)
})
} }
closePage(page, callback) async enablePage()
{ {
this.runCommand("Target.closeTarget", {targetId: page}, (response) => { return await this.runCommand("Page.enable")
callback()
})
} }
focusPage(page, callback) async openNewPage(url)
{ {
this.runCommand("Target.activateTarget", {targetId: page}, (response) => { var response = await this.runCommand("Target.createTarget", {url: url})
this.runCommand("Target.attachToTarget", {targetId:page}, (response) => { return response.targetId
callback()
})
})
} }
// todo: does not work yet async closePage(page)
printToPdf(callback) {
{ var response = await this.runCommand("Target.closeTarget", {targetId: page})
this.runCommand("Page.enable", {}, (ignored) => { return response.success
console.log("Page.enable: " + JSON.stringify(ignored)) }
this.runCommand("Page.printToPDF", {}, (response) => {
console.log("Page.printToPDF:" + JSON.stringify(response)) async focusPage(page)
}) {
}) await this.runCommand("Target.activateTarget", {targetId: page})
} }
}
async printToPdf()
{
var response = await this.runCommand("Page.printToPDF")
return response
}
}
function test()
{
return "test"
}
...@@ -10,34 +10,28 @@ ...@@ -10,34 +10,28 @@
var searchParams = (new URL(window.location.href)).searchParams var searchParams = (new URL(window.location.href)).searchParams
var websocketUrl = searchParams.get("websocket") var websocketUrl = searchParams.get("websocket")
var browser = new HeadlessChromium(websocketUrl) var browser = new HeadlessChromium(websocketUrl)
var urls = ["https://www.example.com"]
function html2pdf(url, callback)
async function html2pdf(url)
{ {
if (!browser.isConnected()) var page = await browser.openNewPage(url)
{ await browser.focusPage(page)
console.log("Browser is not connected!") var pdf = browser.printToPdf()
callback(null) await browser.closePage(page)
return return pdf
}
browser.openNewPage(url, (page) => {
browser.focusPage(page, () => {
browser.printToPdf(() => {
browser.closePage(page, () => {
console.log("close")
})
})
})
})
} }
function mainloop() async function mainloop()
{ {
html2pdf("http://www.example.com", (pdf) => { if (urls.length == 0)
console.log(pdf) {
setTimeout(mainloop, 30 /* seconds */ * 1000)
setTimeout(mainloop, 5 /* seconds */ * 1000) return
}) }
var pdf = await html2pdf(urls.pop())
console.log(pdf)
setTimeout(mainloop, 3 /* seconds */ * 1000)
} }
mainloop() mainloop()
</script> </script>
......
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