-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
54 lines (45 loc) · 1.55 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
function splitVideoIntoSentences(videoElement, sentences) {
// 获取视频元素的持续时间
const videoDuration = videoElement.duration;
let currentTime = 0;
let repeatCount = 1;
while (currentTime < videoDuration) {
// 获取每一个句子的持续时间
const sentence = sentences[Math.floor(currentTime / videoDuration * sentences.length)];
// 每一个句子的持续时间加上当前句子的持续时间
currentTime += sentence.duration * repeatCount;
repeatCount = 1;
// 如果当前句子的持续时间大于视频元素的持续时间,则停止视频
if (currentTime > videoDuration) {
break;
}
// 暂停视频元素
videoElement.pause();
// 设置视频元素的当前时间
videoElement.currentTime = currentTime;
// 延迟一秒后播放视频元素
setTimeout(() => {
videoElement.play();
}, 1000);
// 延迟持续指定的时间后重复播放视频元素
setTimeout(() => {
repeatCount++;
}, sentence.duration * 1000);
}
}
// 获取视频元素
const videoElements = document.getElementsByTagName('video');
// 如果视频元素存在,则分割视频
if (videoElements.length > 0) {
splitVideoIntoSentences(videoElements[0], getSentences(videoElements[0].textContent, 3));
}
function getSentence(text, repeat) {
const sentences = text.split('. ');
const result = [];
for (let i = 0; i < repeat; i++) {
for (const sentence of sentences) {
result.push(sentence);
}
}
return result.join('. ');
}