-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
tiktok_downloadWatchingVideo.js
182 lines (163 loc) · 5.24 KB
/
tiktok_downloadWatchingVideo.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { BADGES } from "./helpers/badge.js";
import { UfsGlobal } from "./content-scripts/ufs_global.js";
import { showLoading, runScriptInCurrentTab } from "./helpers/utils.js";
import { shared as tiktok_downloadVideo } from "./tiktok_downloadVideo.js";
export default {
icon: "https://www.tiktok.com/favicon.ico",
name: {
en: "Tiktok - Download watching video",
vi: "Tiktok - Tải video đang xem",
},
description: {
en: "Download tiktok video you are watching (no watermark)",
vi: "Tải video tiktok bạn đang xem (không watermark)",
},
badges: [BADGES.hot],
changeLogs: {
"2024-04-27": "fix bug - use snaptik",
"2024-05-16": "fix title + show download speed",
},
whiteList: ["https://www.tiktok.com/*"],
popupScript: {
onClick: async function () {
const { t } = await import("../popup/helpers/lang.js");
const { closeLoading, setLoadingText } = showLoading(
t({ vi: "Đang tìm video id..", en: "Finding video id..." })
);
let title = "tiktok_video";
const getLinkFuncs = [
async () => {
setLoadingText(
t({ vi: "Đang lấy video id..", en: "Finding video id..." })
);
const videos = await shared.getListVideoIdInWebsite();
if (videos.length) {
let video = videos[0];
title = video?.title?.split?.("#")?.[0] || video.id || title;
setLoadingText(
t({
vi: `Đang tìm link tải video ${title}...`,
en: `Fetching download link of ${title}...`,
})
);
let res = await tiktok_downloadVideo.getVideoNoWaterMark(
shared.genTiktokUrl(video.author, video.id)
);
return res;
}
},
async () => {
setLoadingText(
t({
vi: "Đang tìm video url từ DOM...",
en: "Finding video url from DOM..",
})
);
return await runScriptInCurrentTab(
async () => await UfsGlobal.DOM.getWatchingVideoSrc()
);
},
];
let link;
for (let func of getLinkFuncs) {
try {
link = await func();
if (link) break;
} catch (e) {
alert("ERROR: " + e);
}
}
if (!link)
alert(
t({ vi: "Không tìm được link video", en: "Cannot find video link" })
);
else {
setLoadingText(
t({ vi: `Đang tải video ${title}...`, en: `Downloading ${title}...` })
);
UfsGlobal.Extension.download({
url: link,
filename: title + ".mp4",
});
// const { formatSize, downloadBlob } = UfsGlobal.Utils;
// const blob = await getBlobFromUrlWithProgress(
// link,
// ({ loaded, total, speed }) => {
// let desc =
// formatSize(loaded, 1) +
// " / " +
// formatSize(total, 1) +
// " (" +
// formatSize(speed, 1) +
// "/s)";
// setLoadingText(
// t({
// vi: `Đang tải ${desc}...<br/>${title}`,
// en: `Downloading ${desc}...<br/>${title}`,
// })
// );
// }
// );
// downloadBlob(blob, title + ".mp4");
}
closeLoading();
},
},
};
export const shared = {
genTiktokUrl(author, videoId) {
return `https://www.tiktok.com/@${author}/video/${videoId}`;
},
getListVideoIdInWebsite: async function () {
return await runScriptInCurrentTab(() => {
const { getOverlapScore, closest } = UfsGlobal.DOM;
let allVideos = Array.from(document.querySelectorAll("video"));
let result = [];
for (let video of allVideos) {
try {
result.push({
overlapScore: getOverlapScore(video),
id: video.parentElement.id.split("-").at(-1),
title:
closest(video, '[data-e2e="browse-video-desc"]')?.textContent ||
closest(video, '[data-e2e="video-desc"]')?.textContent,
author:
closest(video, '[data-e2e="browse-user-avatar"]')?.href ||
closest(video, '[data-e2e="video-author-avatar"]')?.href,
});
} catch (e) {
console.log("ERROR on get: ", e);
}
}
return result.sort((a, b) => b.overlapScore - a.overlapScore);
});
},
};
async function getBlobFromUrlWithProgress(url, progressCallback) {
const response = await fetch(url, {});
if (!response.ok) {
throw new Error(`Error: ${response.status} - ${response.statusText}`);
}
const contentLength = response.headers.get("content-length");
const total = parseInt(contentLength, 10);
let loaded = 0;
const reader = response.body.getReader();
const chunks = [];
const startTime = Date.now();
while (true) {
const { done, value } = await reader.read();
if (done) break;
loaded += value.byteLength;
const ds = (Date.now() - startTime + 1) / 1000;
progressCallback?.({
loaded,
total,
speed: loaded / ds,
});
chunks.push(value);
}
const blob = new Blob(chunks, {
type: response.headers.get("content-type"),
});
return blob;
}