-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
fb_downloadWatchingVideo.js
103 lines (94 loc) · 3.21 KB
/
fb_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
import { BADGES } from "./helpers/badge.js";
import { UfsGlobal } from "./content-scripts/ufs_global.js";
import { shared as fb_videoDownloader } from "./fb_videoDownloader.js";
import { showLoading, runScriptInCurrentTab } from "./helpers/utils.js";
export default {
icon: "https://www.facebook.com/favicon.ico",
name: {
en: "Download watching fb video",
vi: "Tải video fb đang xem",
},
description: {
en: "Download any facebook video that you are watching (watch / story / comment / reel / chat)",
vi: "Tải bất kỳ video facebook nào mà bạn đang xem (watch / story / comment / reel / chat / bình luận / tin nhắn)",
},
badges: [BADGES.hot],
changeLogs: {
"2024-06-26": "fix logic",
},
whiteList: ["https://*.facebook.com/*"],
infoLink:
"https://greasyfork.org/en/scripts/477748-facebook-video-downloader",
popupScript: {
onClick: async function () {
let { closeLoading, setLoadingText } = showLoading(
"Đang lấy videoId từ trang web..."
);
try {
let listVideoId = await shared.getListVideoIdInWebsite();
if (!listVideoId?.length > 0) throw Error("Không tìm thấy video nào");
setLoadingText("Đang lấy token dtsg...");
let dtsg = await fb_videoDownloader.getDtsg();
let downloaded = 0;
for (let videoId of listVideoId) {
setLoadingText("Đang tìm video url...");
let videoUrl = await fb_videoDownloader.getLinkFbVideo(videoId, dtsg);
if (videoUrl) {
downloaded++;
UfsGlobal.Extension.download({
url: videoUrl,
filename: "fb_video.mp4",
});
}
}
if (downloaded === 0) {
alert("Không tìm thấy link video");
}
} catch (e) {
alert("ERROR: " + e);
} finally {
closeLoading();
}
},
},
};
export const shared = {
getListVideoIdInWebsite: async function () {
return await runScriptInCurrentTab(() => {
let allVideos = Array.from(document.querySelectorAll("video"));
let result = [];
for (let video of allVideos) {
try {
let key = "";
for (let k in video.parentElement) {
if (k.startsWith("__reactProps")) {
key = k;
break;
}
}
result.push({
overlapScore: UfsGlobal.DOM.getOverlapScore(video),
videoId: video.parentElement[key].children.props.videoFBID,
// https://stackoverflow.com/a/31196707/23648002
playing: !!(
video.currentTime > 0 &&
!video.paused &&
!video.ended &&
video.readyState > 2
),
});
} catch (e) {
console.log("ERROR on get videoFBID: ", e);
}
}
// if there is playing video => return that
let playingVideo = result.find((_) => _.playing);
if (playingVideo) return [playingVideo.videoId];
// else return all videos in-viewport
return result
.filter((_) => _.videoId && (_.overlapScore > 0 || _.playing))
.sort((a, b) => b.overlapScore - a.overlapScore)
.map((_) => _.videoId);
});
},
};