Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
H
html-shot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
劳工
html-shot
Commits
2a777ad9
Commit
2a777ad9
authored
Jul 05, 2021
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
init
parent
7a7dcc5b
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
157 additions
and
157 deletions
+157
-157
config.ts
src/config.ts
+1
-1
dom-parser.ts
src/dom-parser.ts
+45
-45
index.ts
src/index.ts
+2
-2
toCanvas.ts
src/toCanvas.ts
+109
-109
No files found.
src/config.ts
View file @
2a777ad9
...
@@ -2,4 +2,4 @@
...
@@ -2,4 +2,4 @@
* Created by rockyl on 2021/1/26.
* Created by rockyl on 2021/1/26.
*/
*/
export
const
debugMode
=
window
[
'html-shot/debug-mode'
]
||
false
;
export
const
debugMode
=
window
[
'html-shot/debug-mode'
]
||
false
src/dom-parser.ts
View file @
2a777ad9
/**
/**
* Created by rockyl on 2021/1/11.
* Created by rockyl on 2021/1/11.
*/
*/
import
{
debugMode
}
from
"./config.js"
;
import
{
debugMode
}
from
"./config.js"
const
commonStyleKeys
=
[
const
commonStyleKeys
=
[
'backgroundColor'
,
'backgroundColor'
,
...
@@ -26,15 +26,15 @@ const includeStyleKeys = [
...
@@ -26,15 +26,15 @@ const includeStyleKeys = [
]
]
export
function
parseDom
(
el
:
HTMLElement
=
document
.
body
)
{
export
function
parseDom
(
el
:
HTMLElement
=
document
.
body
)
{
const
{
left
:
pX
,
top
:
pY
,
width
,
height
}
=
el
.
getBoundingClientRect
()
;
const
{
left
:
pX
,
top
:
pY
,
width
,
height
}
=
el
.
getBoundingClientRect
()
let
nodes
=
[]
;
let
nodes
=
[]
walkNode
(
el
,
function
(
childNode
)
{
walkNode
(
el
,
function
(
childNode
)
{
let
vNode
,
bound
,
node
,
isText
;
let
vNode
,
bound
,
node
,
isText
let
skip
=
false
;
let
skip
=
false
switch
(
childNode
.
nodeName
)
{
switch
(
childNode
.
nodeName
)
{
case
'IMG'
:
case
'IMG'
:
node
=
childNode
;
node
=
childNode
const
src
=
node
.
getAttribute
(
'src'
)
//必须使用此方法,使用node.src且为空时会返回当前页面链接
const
src
=
node
.
getAttribute
(
'src'
)
//必须使用此方法,使用node.src且为空时会返回当前页面链接
if
(
src
)
{
//过滤src为空的图片
if
(
src
)
{
//过滤src为空的图片
vNode
=
{
vNode
=
{
...
@@ -42,46 +42,46 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -42,46 +42,46 @@ export function parseDom(el: HTMLElement = document.body) {
src
,
src
,
}
}
}
}
break
;
break
case
'CANVAS'
:
case
'CANVAS'
:
node
=
childNode
;
node
=
childNode
vNode
=
{
vNode
=
{
type
:
3
,
type
:
3
,
img
:
node
,
img
:
node
,
}
}
break
;
break
case
'#text'
:
case
'#text'
:
isText
=
true
;
isText
=
true
node
=
childNode
.
parentElement
;
node
=
childNode
.
parentElement
let
text
=
childNode
.
data
.
trim
()
;
let
text
=
childNode
.
data
.
trim
()
if
(
text
)
{
//过滤空文本
if
(
text
)
{
//过滤空文本
let
range
=
document
.
createRange
()
;
let
range
=
document
.
createRange
()
range
.
selectNode
(
childNode
)
;
range
.
selectNode
(
childNode
)
bound
=
range
.
getBoundingClientRect
()
;
bound
=
range
.
getBoundingClientRect
()
range
.
detach
()
;
range
.
detach
()
vNode
=
{
vNode
=
{
type
:
1
,
type
:
1
,
text
,
text
,
}
}
}
}
break
;
break
case
'#comment'
:
case
'#comment'
:
skip
=
true
;
skip
=
true
break
;
break
default
:
default
:
break
;
break
}
}
if
(
skip
)
{
if
(
skip
)
{
return
;
return
}
}
if
(
!
bound
&&
childNode
.
getBoundingClientRect
)
{
if
(
!
bound
&&
childNode
.
getBoundingClientRect
)
{
bound
=
childNode
.
getBoundingClientRect
()
;
bound
=
childNode
.
getBoundingClientRect
()
}
}
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
,
includeStyleKeys
)
;
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
,
includeStyleKeys
)
if
(
!
isText
)
{
if
(
!
isText
)
{
for
(
let
skey
of
commonStyleKeys
)
{
for
(
let
skey
of
commonStyleKeys
)
{
...
@@ -95,64 +95,64 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -95,64 +95,64 @@ export function parseDom(el: HTMLElement = document.body) {
}
}
}
}
if
(
styles
[
skey
]
!==
undefined
)
{
if
(
styles
[
skey
]
!==
undefined
)
{
vNode
[
skey
]
=
styles
[
skey
]
;
vNode
[
skey
]
=
styles
[
skey
]
}
}
}
}
}
}
if
(
vNode
&&
bound
)
{
if
(
vNode
&&
bound
)
{
const
{
left
,
top
,
width
,
height
}
=
bound
;
const
{
left
,
top
,
width
,
height
}
=
bound
vNode
.
x
=
left
-
pX
;
vNode
.
x
=
left
-
pX
vNode
.
y
=
top
-
pY
;
vNode
.
y
=
top
-
pY
vNode
.
width
=
width
;
vNode
.
width
=
width
vNode
.
height
=
height
;
vNode
.
height
=
height
for
(
let
skey
in
styles
)
{
for
(
let
skey
in
styles
)
{
if
(
styleKeys
.
indexOf
(
skey
)
<
0
)
{
if
(
styleKeys
.
indexOf
(
skey
)
<
0
)
{
continue
continue
}
}
vNode
[
skey
]
=
styles
[
skey
]
;
vNode
[
skey
]
=
styles
[
skey
]
}
}
nodes
.
push
(
vNode
)
;
nodes
.
push
(
vNode
)
}
}
})
;
})
if
(
debugMode
)
{
if
(
debugMode
)
{
console
.
info
(
nodes
)
;
console
.
info
(
nodes
)
}
}
return
{
return
{
width
,
width
,
height
,
height
,
nodes
,
nodes
,
}
;
}
}
}
function
walkNode
(
root
,
callback
)
{
function
walkNode
(
root
,
callback
)
{
callback
(
root
)
;
callback
(
root
)
for
(
let
i
=
0
,
li
=
root
.
childNodes
.
length
;
i
<
li
;
i
++
)
{
for
(
let
i
=
0
,
li
=
root
.
childNodes
.
length
;
i
<
li
;
i
++
)
{
const
childNode
=
root
.
childNodes
[
i
]
;
const
childNode
=
root
.
childNodes
[
i
]
walkNode
(
childNode
,
callback
)
;
walkNode
(
childNode
,
callback
)
}
}
}
}
function
getStylesWithoutDefaults
(
element
,
includes
:
string
[]
=
[]):
any
{
function
getStylesWithoutDefaults
(
element
,
includes
:
string
[]
=
[]):
any
{
let
dummy
=
document
.
createElement
(
'element-'
+
(
new
Date
().
getTime
()))
;
let
dummy
=
document
.
createElement
(
'element-'
+
(
new
Date
().
getTime
()))
document
.
body
.
appendChild
(
dummy
)
;
document
.
body
.
appendChild
(
dummy
)
let
defaultStyles
=
getComputedStyle
(
dummy
)
;
let
defaultStyles
=
getComputedStyle
(
dummy
)
let
elementStyles
=
getComputedStyle
(
element
)
;
let
elementStyles
=
getComputedStyle
(
element
)
let
diff
=
{}
;
let
diff
=
{}
for
(
let
key
in
elementStyles
)
{
for
(
let
key
in
elementStyles
)
{
if
(
includes
.
indexOf
(
key
)
>=
0
||
(
elementStyles
.
hasOwnProperty
(
key
)
&&
defaultStyles
[
key
]
!==
elementStyles
[
key
]))
{
if
(
includes
.
indexOf
(
key
)
>=
0
||
(
elementStyles
.
hasOwnProperty
(
key
)
&&
defaultStyles
[
key
]
!==
elementStyles
[
key
]))
{
diff
[
key
]
=
elementStyles
[
key
]
;
diff
[
key
]
=
elementStyles
[
key
]
}
}
}
}
dummy
.
remove
()
;
dummy
.
remove
()
return
diff
;
return
diff
}
}
src/index.ts
View file @
2a777ad9
...
@@ -2,8 +2,8 @@
...
@@ -2,8 +2,8 @@
* Created by rockyl on 2021/1/11.
* Created by rockyl on 2021/1/11.
*/
*/
import
{
parseDom
}
from
"./dom-parser.js"
;
import
{
parseDom
}
from
"./dom-parser.js"
import
{
RenderOptions
,
toCanvas
}
from
"./toCanvas.js"
;
import
{
RenderOptions
,
toCanvas
}
from
"./toCanvas.js"
/**
/**
* HTML截图
* HTML截图
...
...
src/toCanvas.ts
View file @
2a777ad9
This diff is collapsed.
Click to expand it.
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