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

refactored syntax

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