Skip to content

Commit 463737d

Browse files
committed
fix download tiktok video
1 parent e7b49d5 commit 463737d

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

scripts/content-scripts/ufs_global.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -764,14 +764,23 @@ async function chooseFolderToDownload(subDirName = "") {
764764
});
765765
return subDir;
766766
}
767-
async function downloadToFolder(url, fileName, dirHandler, subFolderName = "") {
767+
async function downloadToFolder({
768+
url,
769+
fileName,
770+
dirHandler,
771+
expectBlobType,
772+
subFolderName = "",
773+
}) {
768774
if (!url) return false;
769775
try {
770776
// const f = getOriginalWindowFunction("fetch");
771-
// try download directly, using fetch blob
772-
console.log(url);
773777
const res = await fetch(url);
774778
const blob = await res.blob();
779+
if (expectBlobType && blob.type !== expectBlobType) {
780+
throw new Error(
781+
`Blob type ${blob.type} doesn't match expected type ${expectBlobType}`
782+
);
783+
}
775784
const fileHandler = await dirHandler.getFileHandle(fileName, {
776785
create: true,
777786
});

scripts/tiktok_batchDownload.js

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ export default {
4747
if (event.data?.type === commId) {
4848
const dir = await UfsGlobal.Utils.chooseFolderToDownload("tiktok");
4949
for (const { url, name } of event.data.data) {
50-
await UfsGlobal.Utils.downloadToFolder(url, name, dir);
50+
await UfsGlobal.Utils.downloadToFolder({
51+
url,
52+
fileName: name,
53+
dirHandler: dir,
54+
});
5155
}
5256
}
5357
});
@@ -218,10 +222,12 @@ export default {
218222
const downVideoBtn = container.querySelector("button#video");
219223
downVideoBtn.addEventListener("click", () => {
220224
download(
225+
"video/mp4",
221226
data.map((_, i) => {
222227
const urlList =
223-
_.video.bitrateInfo.find((b) => b.Bitrate === _.video.bitrate)
224-
?.PlayAddr?.UrlList || [];
228+
_.video?.bitrateInfo?.find?.(
229+
(b) => b.Bitrate === _.video.bitrate
230+
)?.PlayAddr?.UrlList || [];
225231

226232
const bestUrl = urlList[urlList.length - 1];
227233

@@ -378,14 +384,19 @@ export default {
378384
.join("");
379385
}
380386

381-
async function download(data, onProgress) {
387+
async function download(expectBlobType, data, onProgress) {
382388
const dir = await UfsGlobal.Utils.chooseFolderToDownload("tiktok");
383389

384390
for (let i = 0; i < data.length; ++i) {
385391
try {
386392
const { url, filename } = data[i];
387393
const realUrl = await UfsGlobal.Utils.getRedirectedUrl(url);
388-
await UfsGlobal.Utils.downloadToFolder(realUrl, filename, dir);
394+
await UfsGlobal.Utils.downloadToFolder({
395+
url: realUrl,
396+
fileName: filename,
397+
dirHandler: dir,
398+
expectBlobType,
399+
});
389400
onProgress?.(i + 1, data.length);
390401
} catch (e) {
391402
console.error(e);
@@ -396,19 +407,33 @@ export default {
396407
hookFetch({
397408
onAfter: async (url, options, response) => {
398409
if (url.includes("item_list/")) {
399-
// clone to new response
400410
const res = response.clone();
401411
const json = await res.json();
402412
console.log(json);
403413

404414
if (json?.itemList) {
405-
CACHED.list.push(...json.itemList);
406415
json.itemList.forEach((_) => {
407416
if (_.video.playAddr) CACHED.videoById.set(_.video.id, _);
408417
});
409-
floatingBtn.innerHTML = `📥 (${CACHED.videoById.size})`;
410418
}
411419
}
420+
421+
if (url.includes("api/search")) {
422+
const res = response.clone();
423+
const json = await res.json();
424+
console.log(json);
425+
426+
if (json.data?.length) {
427+
json.data.forEach((_) => {
428+
if (_.type === 1) {
429+
CACHED.videoById.set(_.item.video.id, _.item);
430+
}
431+
});
432+
}
433+
}
434+
435+
if (CACHED.videoById.size)
436+
floatingBtn.innerHTML = `📥 (${CACHED.videoById.size})`;
412437
},
413438
});
414439
},

0 commit comments

Comments
 (0)