Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
ScreenCat
Overview
Overview
Details
Activity
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
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Iteravse
ScreenCat
Commits
6302f22c
Commit
6302f22c
authored
Dec 11, 2023
by
Iteravse
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial commit
parents
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
+103
-0
ScreenCat.js
+99
-0
readme.md
+4
-0
No files found.
ScreenCat.js
0 → 100644
View file @
6302f22c
async
function
cutImg
(
name
,
DomStr
)
{
let
targetDom
=
document
.
querySelector
(
DomStr
)
targetDom
.
scrollTop
=
0
;
let
Max
=
Math
.
ceil
(
targetDom
.
scrollHeight
/
targetDom
.
offsetHeight
),
Canvases
=
[],
LastHeight
=
targetDom
.
scrollHeight
-
(
Max
-
1
)
*
targetDom
.
offsetHeight
,
ICs
=
[];
for
(
let
i
=
0
;
i
<
Max
;
i
++
)
{
if
(
i
==
(
Max
-
1
)
&&
(
LastHeight
>
0
)){
ICs
=
await
html2canvas
(
document
.
querySelector
(
"body"
),
{
useCORS
:
true
,
x
:
targetDom
.
offsetLeft
,
y
:
targetDom
.
offsetTop
-
LastHeight
+
targetDom
.
offsetHeight
,
height
:
LastHeight
,
width
:
targetDom
.
scrollWidth
})
targetDom
.
scrollTop
+=
LastHeight
}
else
{
ICs
=
await
html2canvas
(
document
.
querySelector
(
"body"
),
{
useCORS
:
true
,
x
:
targetDom
.
offsetLeft
,
y
:
targetDom
.
offsetTop
,
height
:
targetDom
.
offsetHeight
,
width
:
targetDom
.
scrollWidth
})
targetDom
.
scrollTop
+=
targetDom
.
offsetHeight
}
Canvases
.
push
(
ICs
)
// .then(function (canvas) {
// var imgData = canvas.toDataURL();
// // document.body.children[0].remove()
// //document.body.appendChild(canvas); 把截的图显示在网页上
// })
}
let
MergeC
=
MergeCanvases
(
Canvases
);
var
imgData
=
MergeC
.
toDataURL
();
//获取年月日作为文件名
var
timers
=
new
Date
();
var
fullYear
=
timers
.
getFullYear
();
var
month
=
timers
.
getMonth
()
+
1
;
var
date
=
timers
.
getDate
();
var
randoms
=
Math
.
random
()
+
''
;
//年月日加上随机数
var
numberFileName
=
fullYear
+
''
+
month
+
date
+
randoms
.
slice
(
3
,
10
);
//保存图片
var
saveFile
=
function
(
data
,
filename
)
{
var
save_link
=
document
.
createElementNS
(
'http://www.w3.org/1999/xhtml'
,
'a'
);
save_link
.
href
=
data
;
save_link
.
download
=
filename
;
var
event
=
document
.
createEvent
(
'MouseEvents'
);
event
.
initMouseEvent
(
'click'
,
true
,
false
,
window
,
0
,
0
,
0
,
0
,
0
,
false
,
false
,
false
,
false
,
0
,
null
);
save_link
.
dispatchEvent
(
event
);
};
//最终文件名+文件格式
var
filename
=
getNowDate
()
+
(
name
||
currentWord
)
+
'.png'
;
saveFile
(
imgData
,
filename
);
}
const
getNowDate
=
()
=>
{
var
date
=
new
Date
();
var
sign2
=
":"
;
var
year
=
date
.
getFullYear
()
// 年
var
month
=
date
.
getMonth
()
+
1
;
// 月
var
day
=
date
.
getDate
();
// 日
var
hour
=
date
.
getHours
();
// 时
var
minutes
=
date
.
getMinutes
();
// 分
var
seconds
=
date
.
getSeconds
()
//秒
var
weekArr
=
[
'星期一'
,
'星期二'
,
'星期三'
,
'星期四'
,
'星期五'
,
'星期六'
,
'星期天'
];
var
week
=
weekArr
[
date
.
getDay
()];
// 给一位数的数据前面加 “0”
if
(
month
>=
1
&&
month
<=
9
)
{
month
=
"0"
+
month
;
}
if
(
day
>=
0
&&
day
<=
9
)
{
day
=
"0"
+
day
;
}
if
(
hour
>=
0
&&
hour
<=
9
)
{
hour
=
"0"
+
hour
;
}
if
(
minutes
>=
0
&&
minutes
<=
9
)
{
minutes
=
"0"
+
minutes
;
}
if
(
seconds
>=
0
&&
seconds
<=
9
)
{
seconds
=
"0"
+
seconds
;
}
return
year
+
"-"
+
month
+
"-"
+
day
+
" "
+
hour
+
sign2
+
minutes
+
sign2
+
seconds
;
}
// 合并画布
function
MergeCanvases
(
canvases
)
{
// 创建一个新的画布
let
totalHeight
=
canvases
.
reduce
((
sum
,
canvas
)
=>
sum
+
canvas
.
height
,
0
);
let
maxWidth
=
Math
.
max
(...
canvases
.
map
(
canvas
=>
canvas
.
width
));
let
mergedCanvas
=
document
.
createElement
(
'canvas'
);
mergedCanvas
.
width
=
maxWidth
;
mergedCanvas
.
height
=
totalHeight
;
// 获取新画布的上下文
let
ctx
=
mergedCanvas
.
getContext
(
'2d'
);
// 依次绘制每个画布
let
currentY
=
0
;
canvases
.
forEach
(
canvas
=>
{
ctx
.
drawImage
(
canvas
,
0
,
currentY
);
currentY
+=
canvas
.
height
;
});
return
mergedCanvas
;
}
readme.md
0 → 100644
View file @
6302f22c
# 调用示例
cutImg("推演截图",".chat-thread")
\ No newline at end of file
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