Skip to content

Commit e0f2eb5

Browse files
author
hoang.tran12
committed
fix tiktok + download blob
1 parent e5dcead commit e0f2eb5

File tree

2 files changed

+66
-50
lines changed

2 files changed

+66
-50
lines changed

scripts/content-scripts/scripts/ufs_global_webpage_context.js

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -289,32 +289,37 @@ const UsefulScriptGlobalPageContext = {
289289
alert("Error: " + error);
290290
}
291291
},
292-
async downloadBlobUrl(url, fileName, progressCallback) {
293-
try {
294-
const response = await fetch(url);
295-
if (!response.ok) {
296-
throw new Error(`Error: ${response.status} - ${response.statusText}`);
297-
}
298-
const contentLength = response.headers.get("content-length");
299-
const total = parseInt(contentLength, 10);
300-
let loaded = 0;
301-
const reader = response.body.getReader();
302-
const chunks = [];
292+
async downloadBlobUrlWithProgress(url, fileName, progressCallback) {
293+
const response = await fetch(url);
294+
if (!response.ok) {
295+
throw new Error(`Error: ${response.status} - ${response.statusText}`);
296+
}
297+
const contentLength = response.headers.get("content-length");
298+
const total = parseInt(contentLength, 10);
299+
let loaded = 0;
300+
const reader = response.body.getReader();
301+
const chunks = [];
303302

304-
while (true) {
305-
const { done, value } = await reader.read();
306-
if (done) break;
307-
loaded += value.byteLength;
308-
progressCallback?.(loaded, total);
309-
chunks.push(value);
310-
}
303+
while (true) {
304+
const { done, value } = await reader.read();
305+
if (done) break;
306+
loaded += value.byteLength;
307+
progressCallback?.(loaded, total);
308+
chunks.push(value);
309+
}
311310

312-
const blob = new Blob(chunks, {
313-
type: response.headers.get("content-type"),
314-
});
315-
UsefulScriptGlobalPageContext.Utils.downloadBlob(blob, fileName);
316-
} catch (error) {
317-
alert("Error: " + error);
311+
const blob = new Blob(chunks, {
312+
type: response.headers.get("content-type"),
313+
});
314+
UsefulScriptGlobalPageContext.Utils.downloadBlob(blob, fileName);
315+
},
316+
async downloadBlobUrl(url, title) {
317+
try {
318+
let res = await fetch(url);
319+
let blob = await res.blob();
320+
UsefulScriptGlobalPageContext.Utils.downloadBlob(blob, title);
321+
} catch (e) {
322+
alert("Error: " + e);
318323
}
319324
},
320325
downloadBlob(blob, filename) {

scripts/tiktok_downloadWatchingVideo.js

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,47 @@ export default {
1313

1414
onClickExtension: async function () {
1515
const { closeLoading, setLoadingText } = showLoading("Đang lấy video id..");
16-
try {
17-
const videoIds = await shared.getListVideoIdInWebsite();
18-
if (!videoIds.length) throw Error("Không tìm thấy video nào");
19-
else {
20-
setLoadingText(`Đang tìm link tải video ${videoIds[0]}...`);
21-
const link =
22-
await UsefulScriptGlobalPageContext.Tiktok.downloadTiktokVideoFromId(
16+
17+
const getLinkFuncs = [
18+
async () => {
19+
setLoadingText("Đang tìm videoid...");
20+
const videoIds = await shared.getListVideoIdInWebsite();
21+
if (videoIds.length) {
22+
setLoadingText(`Đang tìm link tải video ${videoIds[0]}...`);
23+
return await UsefulScriptGlobalPageContext.Tiktok.downloadTiktokVideoFromId(
2324
videoIds[0]
2425
);
25-
if (link) {
26-
// await UsefulScriptGlobalPageContext.Utils.downloadBlobUrl(
27-
// link,
28-
// "video.mp4",
29-
// (loaded, total) => {
30-
// let loadedMB = ~~(loaded / 1024 / 1024);
31-
// let totalMB = ~~(total / 1024 / 1024);
32-
// let percent = ((loaded / total) * 100) | 0;
33-
// setLoadingText(
34-
// `Đang tải video... (${loadedMB}/${totalMB}MB - ${percent}%)`
35-
// );
36-
// }
37-
// );
38-
window.open(link);
39-
} else alert("Không tìm được video đang xem");
26+
}
27+
},
28+
29+
async () => {
30+
setLoadingText("Đang tìm video url từ DOM...");
31+
return await runScriptInCurrentTab(
32+
async () =>
33+
await UsefulScriptGlobalPageContext.DOM.getWatchingVideoSrc()
34+
);
35+
},
36+
];
37+
38+
let link;
39+
for (let func of getLinkFuncs) {
40+
try {
41+
link = await func();
42+
if (link) break;
43+
} catch (e) {
44+
alert("lol");
4045
}
41-
} catch (e) {
42-
alert("ERROR: " + e);
43-
} finally {
44-
closeLoading();
4546
}
47+
48+
if (!link) alert("Không tìm được link video");
49+
else {
50+
UsefulScriptGlobalPageContext.Utils.downloadBlobUrl(
51+
link,
52+
"tiktok_video.mp4"
53+
);
54+
}
55+
56+
closeLoading();
4657
},
4758
};
4859

0 commit comments

Comments
 (0)