-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
tiktok_batchDownload.js
253 lines (223 loc) · 7.94 KB
/
tiktok_batchDownload.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
export default {
icon: "https://www.tiktok.com/favicon.ico",
name: {
en: "Tiktok - Batch download",
vi: "Tiktok - Tải hàng loạt",
},
description: {
en: "Select and download all tiktok video (user profile, tiktok explore).",
vi: "Tải hàng loạt video tiktok (trang người dùng, trang tìm kiếm), có giao diện chọn video muốn tải.",
img: "/scripts/tiktok_batchDownload.jpg",
},
changeLogs: {
1.66: {
"2024-04-27": "fix bug - use snaptik",
},
},
whiteList: ["https://www.tiktok.com/*"],
onDocumentIdle: () => {
let checkboxes = [];
// Setup DOM
let container = document.createElement("div");
container.style = [
"position: fixed",
"bottom: 20px",
"left: 50%",
"transform: translateX(-50%)",
"background: #333e",
"min-height: 50px",
"padding: 15px",
"z-index: 6",
"border-radius: 5px",
"border: 1px solid #eee",
"color: white",
].join(";");
(document.body || document.documentElement).appendChild(container);
let progressDiv = document.createElement("p");
progressDiv.innerText = "Useful script: Tiktok tải hàng loạt";
progressDiv.style = "margin-bottom: 5px; font-family: monospace;";
container.appendChild(progressDiv);
// scroll button
let scrolling = false;
const scrollBtn = document.createElement("button");
scrollBtn.innerText = "Scroll xuống ⏬";
scrollBtn.onclick = async () => {
scrolling = !scrolling;
scrollBtn.innerText = scrolling ? "Đang scroll... ⏳" : "Scroll xuống ⏬";
let doubleCheck = 0;
while (scrolling) {
let previousHeight = document.body.scrollHeight;
window.scrollTo(0, document.body.scrollHeight);
await new Promise((resolve) => setTimeout(resolve, 1000));
if (document.body.scrollHeight <= previousHeight) {
doubleCheck++;
console.log(doubleCheck);
if (doubleCheck > 5) {
scrolling = false;
scrollBtn.innerText = "Scroll xuống ⏬";
}
}
}
};
container.appendChild(scrollBtn);
// Select all button
const selectAllBtn = document.createElement("button");
selectAllBtn.innerText = "Chọn/Huỷ chọn ✅";
selectAllBtn.onclick = function () {
let value = checkboxes[0].checked;
for (let checkbox of checkboxes) {
checkbox.checked = !value;
}
};
container.appendChild(selectAllBtn);
// Revert all Button
const revertAllBtn = document.createElement("button");
revertAllBtn.innerText = "Đảo ngược 🔁";
revertAllBtn.onclick = function () {
for (let checkbox of checkboxes) {
checkbox.checked = !checkbox.checked;
}
};
container.appendChild(revertAllBtn);
// Download button
const downloadBtn = document.createElement("button");
downloadBtn.innerText = "GET LINK 🔗";
downloadBtn.onclick = function () {
let videoUrls = [];
for (let checkbox of checkboxes) {
if (checkbox.checked) {
videoUrls.push(checkbox["data-url"]);
}
}
console.log(videoUrls);
getLinkVideos(videoUrls);
};
container.appendChild(downloadBtn);
// result div
let resultDiv = document.createElement("div");
resultDiv.style = "margin-top: 10px";
container.appendChild(resultDiv);
let resultLabel = document.createElement("label");
resultDiv.appendChild(resultLabel);
let resultTxt = document.createElement("textarea");
resultTxt.style = "width: 100%; height: 50px";
resultTxt.hidden = true;
resultDiv.appendChild(resultTxt);
// click listener
window.onclick = function (e) {
if (
e.target.type === "checkbox" ||
e.target == selectAllBtn ||
e.target == revertAllBtn
) {
let selected = checkboxes.filter((checkbox) => checkbox.checked).length;
progressDiv.innerText = `Đã chọn ${selected}/${checkboxes.length} video. Bấm nút Get link khi chọn xong nhé.`;
}
};
function createCheckBox(url) {
let checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.name = "video";
checkbox.checked = false;
checkbox["data-url"] = url;
checkbox.style =
"z-index: 0; position: absolute; top: 0; right: 0; width: 60px; height: 60px;";
return checkbox;
}
async function sleep(time) {
await new Promise((resolve) => setTimeout(resolve, time));
}
async function getLinkVideos(videoUrls) {
if (!videoUrls.length) return;
const getId = (url) => url.split("/").at(-1);
const queue = [...videoUrls];
const links = [];
downloadBtn.disabled = true;
while (queue.length) {
let progress = `[${videoUrls.length - queue.length + 1}/${
videoUrls.length
}]`;
try {
console.log(`${progress} Đang tìm link cho video ${queue[0]}`);
progressDiv.innerText = `${progress} Đang tìm link video ${queue[0]}...`;
downloadBtn.innerText = `Đang get link ${progress}...`;
let link = await UfsGlobal.Tiktok.downloadTiktokVideoFromUrl(
queue[0],
true
);
if (!link) {
link = await UfsGlobal.Tiktok.downloadTiktokVideoFromId(
getId(queue[0])
);
}
if (link) {
resultTxt.hidden = false;
resultTxt.value += link + "\n";
let count = resultTxt.value.split("\n").filter((i) => i).length;
resultLabel.innerText = `Link tại đây, ${count} video, copy bỏ vào IDM tải hàng loạt nhé:`;
links.push(link);
} else {
progressDiv.innerText = `[LỖI] Không thể tải video ${queue[0]}.`;
await sleep(1000);
}
queue.shift();
} catch (e) {
console.log(`${progress} Lỗi tải, thử lại sau 1s...`);
let failId = queue.shift();
queue.push(failId);
await sleep(1000);
}
}
progressDiv.innerText = "Bạn vẫn có thể chọn thêm video để get link.";
downloadBtn.disabled = false;
downloadBtn.innerText = "GET LINK 🔗";
if (links?.length) UfsGlobal.Utils.copyToClipboard(links.join("\n"));
console.log(links);
}
// Listen for videos
UfsGlobal.DOM.onElementsVisible(
'a[href*="/video/"]',
(nodes) => {
// remove if not in DOM
for (let i = checkboxes.length - 1; i >= 0; i--) {
let checkbox = checkboxes[i];
if (!document.contains(checkbox)) {
checkboxes.splice(i, 1);
}
}
for (let node of nodes) {
if (!node.querySelector("canvas")) continue;
let isPrivate = node.querySelector("svg.private") !== null;
if (isPrivate) {
let p = document.createElement("p");
p.innerText = "Riêng tư";
p.style = [
"position: absolute;",
"top: 0;",
"right: 0;",
"color: red",
"background: black",
"padding: 8px",
"font-weight: bold",
].join(";");
node.parentElement.appendChild(p);
} else {
let url = node.getAttribute("href");
let checkbox = createCheckBox(url);
node.parentElement.appendChild(checkbox);
checkboxes.push(checkbox);
}
}
let selected = checkboxes.filter((checkbox) => checkbox.checked).length;
progressDiv.innerText = `Đã chọn ${selected}/${checkboxes.length} video. Bấm nút Get link khi chọn xong nhé.`;
},
true
);
},
onClickExtension: () => {
alert(`Làm các bước sau:
1: Tích chọn nút bên trái để mở chức năng.
2: Tải lại trang web tiktok.
3: Sẽ hiện giao diện giúp tải hàng loạt ngay trong trang web.`);
},
};