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
d9f6a733
Commit
d9f6a733
authored
Jul 25, 2022
by
lukas.niegsch
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
handles event messages via devtools
parent
bb01303d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
58 additions
and
59 deletions
+58
-59
gadget_converter.js
gadget_converter.js
+1
-29
gadget_devtools.js
gadget_devtools.js
+57
-29
index.js
index.js
+0
-1
No files found.
gadget_converter.js
View file @
d9f6a733
...
...
@@ -36,34 +36,9 @@
var
sessionId
;
var
base64data
;
/**
* Steps:
*
* // new page setup
* Target.createTarget();
* Target.attachToTarget();
* Target.activateTarget();
*
* // enable events
* Network.enable();
* Page.enable();
*
* // change content
* Page.frameAttached();
* Page.setDocumentContent();
* Page.loadEventFired(); // Is this event fired once loading starts or stops?
*
* // convert html to pdf
* Page.printToPDF();
*
* // cleanup
* Target.detachFromTarget();
* Target.closeTarget();
*
* todo: detaching should reset session properly
* todo: implement receiving events to avoid waiting
* todo: close target stops receiving messages ...
*/
return
gadget
.
getDeclaredGadget
(
"
devtools
"
)
.
push
(
function
(
subgadget
)
{
return
subgadget
.
getDevtools
();
...
...
@@ -90,16 +65,13 @@
return
devtools
.
Page
.
getFrameTree
();
})
.
push
(
function
(
result
)
{
console
.
log
(
result
);
var
frameId
=
result
.
frameTree
.
frame
.
id
;
return
devtools
.
Page
.
setDocumentContent
({
frameId
:
frameId
,
html
:
html
});
})
.
push
(
function
()
{
console
.
log
(
"
begin delay
"
);
return
RSVP
.
delay
(
3
/* seconds*/
*
1000
);
return
devtools
.
Page
.
loadEventFired
();
})
.
push
(
function
()
{
console
.
log
(
"
end delay
"
);
return
devtools
.
Page
.
printToPDF
(
optionsForPrintToPdf
);
})
.
push
(
function
(
result
)
{
...
...
gadget_devtools.js
View file @
d9f6a733
...
...
@@ -70,10 +70,9 @@ var BROWSER_PASSWORD = "ignored";
params
:
params
,
}
var
message
=
JSON
.
stringify
(
command
);
console
.
log
(
message
);
gadget
.
state
.
websocket
.
send
(
message
);
var
handler
=
{
sessionId
,
id
:
gadget
.
state
.
nextIndex
,
result
:
resolve
,
error
:
reject
...
...
@@ -96,10 +95,14 @@ var BROWSER_PASSWORD = "ignored";
function
addEventAPI
(
object
,
domain
,
method
)
{
var
result
=
(
gadget
,
params
)
=>
{
var
callback
=
(
resolve
,
reject
)
=>
{
// todo: implement this method
resolve
(
null
);
var
sessionId
=
gadget
.
state
.
sessionId
;
var
handler
=
{
sessionId
,
method
:
`
${
domain
.
domain
}
.
${
method
.
name
}
`
,
result
:
resolve
,
error
:
reject
}
gadget
.
state
.
handlers
.
add
(
handler
);
}
return
RSVP
.
Queue
().
push
(
function
()
{
return
RSVP
.
Promise
(
callback
);
...
...
@@ -167,7 +170,6 @@ var BROWSER_PASSWORD = "ignored";
gadget
.
changeState
({
nextIndex
:
gadget
.
state
.
nextIndex
+
1
});
}
websocket
.
onerror
=
(
error
)
=>
{
console
.
log
(
error
);
websocket
.
close
();
updateWebsocket
(
gadget
,
websocketUrl
);
}
...
...
@@ -177,6 +179,45 @@ var BROWSER_PASSWORD = "ignored";
return
RSVP
.
Promise
(
callback
);
});
}
/**
* Find the corresponding handler for the message and resolves it.
*
* @param {object} message The message that contains the result data.
* @param {object} gadget The gadget that contains all the handlers.
*/
function
handleMethodMessage
(
gadget
,
message
)
{
for
(
let
it
=
gadget
.
state
.
handlers
.
values
(),
handler
=
null
;
handler
=
it
.
next
().
value
;)
{
if
(
handler
.
id
!=
message
.
id
)
{
continue
;
}
gadget
.
state
.
handlers
.
delete
(
handler
);
if
(
'
error
'
in
message
)
{
handler
.
error
(
message
.
error
);
}
else
{
handler
.
result
(
message
.
result
)
}
}
}
/**
* Find the corresponding handler for the event and resolves it.
*
* @param {object} message The message that contains the event data.
* @param {object} gadget The gadget that contains all the handlers.
*/
function
handleEventMessage
(
gadget
,
message
)
{
for
(
let
it
=
gadget
.
state
.
handlers
.
values
(),
handler
=
null
;
handler
=
it
.
next
().
value
;)
{
if
(
handler
.
method
!=
message
.
method
)
{
continue
;
}
gadget
.
state
.
handlers
.
delete
(
handler
);
handler
.
result
(
message
.
params
);
}
}
rJS
(
window
)
.
setState
({
...
...
@@ -220,31 +261,18 @@ var BROWSER_PASSWORD = "ignored";
return
gadget
.
state
.
devtools
;
})
})
.
onStateChange
(
function
(
modifications
)
{
.
onStateChange
(
function
()
{
var
gadget
=
this
;
/*
* This is O(n * m) with n = messages.size and m = handlers.size.
*
* I think it is fine since new messages should complete all handlers,
* which means m stays very small. We also have messages from events,
* but there are only finite amount, so n should stay small aswell.
*
* This can be implemented much faster if performance becomes an issue.
*/
for
(
let
i
=
gadget
.
state
.
handlers
.
values
(),
handler
=
null
;
handler
=
i
.
next
().
value
;)
{
for
(
let
j
=
gadget
.
state
.
messages
.
values
(),
message
=
null
;
message
=
j
.
next
().
value
;)
{
if
(
handler
.
id
!=
message
.
id
)
{
continue
;
}
gadget
.
state
.
handlers
.
delete
(
handler
);
gadget
.
state
.
messages
.
delete
(
message
);
if
(
'
error
'
in
message
)
{
handler
.
error
(
message
.
error
);
}
else
{
handler
.
result
(
message
.
result
)
}
for
(
let
it
=
gadget
.
state
.
messages
.
values
(),
message
=
null
;
message
=
it
.
next
().
value
;)
{
console
.
log
(
message
);
if
(
message
.
hasOwnProperty
(
"
id
"
))
{
handleMethodMessage
(
gadget
,
message
);
}
if
(
message
.
hasOwnProperty
(
"
method
"
))
{
handleEventMessage
(
gadget
,
message
);
}
gadget
.
state
.
messages
.
delete
(
message
);
}
})
}(
window
,
rJS
,
RSVP
));
\ No newline at end of file
index.js
View file @
d9f6a733
...
...
@@ -43,7 +43,6 @@
return
converter
.
convert
(
html
);
})
.
push
(
function
(
base64data
)
{
console
.
log
(
base64data
);
viewer
.
src
=
"
data:application/pdf;base64,
"
+
base64data
;
})
.
push
(
function
()
{
...
...
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