Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
html2pdf
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Lukas Niegsch
html2pdf
Commits
30768ecb
Commit
30768ecb
authored
Jun 11, 2022
by
lukas.niegsch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implement some chromium commands
parent
839d73b0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
12 deletions
+59
-12
chromium.js
chromium.js
+35
-1
index.html
index.html
+24
-11
No files found.
chromium.js
View file @
30768ecb
...
...
@@ -17,8 +17,42 @@ class HeadlessChromium
var
command
=
{
id
:
0
,
method
:
method
,
params
:
params
}
this
.
websocket
.
send
(
JSON
.
stringify
(
command
))
this
.
websocket
.
onmessage
=
(
response
)
=>
{
callback
(
response
.
data
)
callback
(
JSON
.
parse
(
response
.
data
)
)
}
}
openNewPage
(
url
,
callback
)
{
this
.
runCommand
(
"
Target.createTarget
"
,
{
url
:
url
},
(
response
)
=>
{
callback
(
response
.
result
.
targetId
)
})
}
closePage
(
page
,
callback
)
{
this
.
runCommand
(
"
Target.closeTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
callback
()
})
}
focusPage
(
page
,
callback
)
{
this
.
runCommand
(
"
Target.activateTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
this
.
runCommand
(
"
Target.attachToTarget
"
,
{
targetId
:
page
},
(
response
)
=>
{
callback
()
})
})
}
// 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
))
})
})
}
}
index.html
View file @
30768ecb
...
...
@@ -11,20 +11,33 @@
var
websocketUrl
=
searchParams
.
get
(
"
websocket
"
)
var
browser
=
new
HeadlessChromium
(
websocketUrl
)
function
mainloop
(
)
function
html2pdf
(
url
,
callback
)
{
if
(
browser
.
isConnected
())
{
browser
.
runCommand
(
"
Browser.getVersion
"
,
{},
(
version
)
=>
{
console
.
log
(
version
)
})
}
else
if
(
!
browser
.
isConnected
())
{
console
.
log
(
"
Not connected!
"
)
console
.
log
(
"
Browser is not connected!
"
)
callback
(
null
)
return
}
setTimeout
(
mainloop
,
2
/* seconds */
*
1000
)
browser
.
openNewPage
(
url
,
(
page
)
=>
{
browser
.
focusPage
(
page
,
()
=>
{
browser
.
printToPdf
(()
=>
{
browser
.
closePage
(
page
,
()
=>
{
console
.
log
(
"
close
"
)
})
})
})
})
}
function
mainloop
()
{
html2pdf
(
"
http://www.example.com
"
,
(
pdf
)
=>
{
console
.
log
(
pdf
)
setTimeout
(
mainloop
,
5
/* seconds */
*
1000
)
})
}
mainloop
()
</script>
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment