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
cb9eb363
Commit
cb9eb363
authored
Jan 22, 2021
by
rockyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修改脚本
parent
5b16ae67
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
9 deletions
+14
-9
package.json
package.json
+1
-1
dom-parser.ts
src/dom-parser.ts
+13
-8
No files found.
package.json
View file @
cb9eb363
{
{
"name"
:
"html-shot"
,
"name"
:
"html-shot"
,
"version"
:
"1.0.
6
"
,
"version"
:
"1.0.
10
"
,
"main"
:
"dist/index.js"
,
"main"
:
"dist/index.js"
,
"types"
:
"types/index.d.ts"
,
"types"
:
"types/index.d.ts"
,
"license"
:
"
MIT
"
,
"license"
:
"
MIT
"
,
...
...
src/dom-parser.ts
View file @
cb9eb363
...
@@ -19,6 +19,10 @@ const styleKeys = [
...
@@ -19,6 +19,10 @@ const styleKeys = [
'fontStyle'
,
'fontStyle'
,
]
]
const
includeStyleKeys
=
[
'fontSize'
,
]
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
();
...
@@ -28,16 +32,18 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -28,16 +32,18 @@ export function parseDom(el: HTMLElement = document.body) {
switch
(
childNode
.
nodeName
)
{
switch
(
childNode
.
nodeName
)
{
case
'IMG'
:
case
'IMG'
:
node
=
childNode
;
node
=
childNode
;
if
(
node
.
src
)
{
//过滤src为空的图片
vNode
=
{
vNode
=
{
type
:
2
,
type
:
2
,
src
:
childNode
.
src
,
src
:
node
.
src
,
}
}
}
break
;
break
;
case
'CANVAS'
:
case
'CANVAS'
:
node
=
childNode
;
node
=
childNode
;
vNode
=
{
vNode
=
{
type
:
3
,
type
:
3
,
img
:
childN
ode
,
img
:
n
ode
,
}
}
break
;
break
;
case
'#text'
:
case
'#text'
:
...
@@ -64,7 +70,7 @@ export function parseDom(el: HTMLElement = document.body) {
...
@@ -64,7 +70,7 @@ export function parseDom(el: HTMLElement = document.body) {
bound
=
childNode
.
getBoundingClientRect
();
bound
=
childNode
.
getBoundingClientRect
();
}
}
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
);
let
styles
=
getStylesWithoutDefaults
(
node
||
childNode
,
includeStyleKeys
);
if
(
!
isText
)
{
if
(
!
isText
)
{
for
(
let
skey
of
commonStyleKeys
)
{
for
(
let
skey
of
commonStyleKeys
)
{
...
@@ -115,7 +121,7 @@ function walkNode(root, callback) {
...
@@ -115,7 +121,7 @@ function walkNode(root, callback) {
}
}
}
}
function
getStylesWithoutDefaults
(
element
):
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
);
...
@@ -124,8 +130,7 @@ function getStylesWithoutDefaults(element): any {
...
@@ -124,8 +130,7 @@ function getStylesWithoutDefaults(element): any {
let
diff
=
{};
let
diff
=
{};
for
(
let
key
in
elementStyles
)
{
for
(
let
key
in
elementStyles
)
{
if
(
elementStyles
.
hasOwnProperty
(
key
)
if
(
includes
.
indexOf
(
key
)
>=
0
||
(
elementStyles
.
hasOwnProperty
(
key
)
&&
defaultStyles
[
key
]
!==
elementStyles
[
key
]))
{
&&
defaultStyles
[
key
]
!==
elementStyles
[
key
])
{
diff
[
key
]
=
elementStyles
[
key
];
diff
[
key
]
=
elementStyles
[
key
];
}
}
}
}
...
...
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