-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
ggdrive_downloadVideo.js
159 lines (140 loc) · 4.35 KB
/
ggdrive_downloadVideo.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
import { BADGES } from "./helpers/badge.js";
export default {
icon: "https://drive-thirdparty.googleusercontent.com/32/type/video/mp4",
name: {
en: "GG Drive - Download video",
vi: "GG Drive - Tải video",
},
description: {
en: "Download google drive video that dont have download button",
vi: "Tải video không có nút download trên google drive",
},
badges: [BADGES.hot],
infoLink:
"https://www.facebook.com/groups/j2team.community/posts/974953859503401/",
changeLogs: {
"2024-07-25": "add backup plan",
},
popupScript: {
onClick: async function () {
const { getCurrentTab, openPopupWithHtml, showLoading } = await import(
"./helpers/utils.js"
);
let { closeLoading } = showLoading("Đang tìm link video...");
try {
let docid = await shared.getDocIdFromWebsite();
if (!docid) {
let tab = await getCurrentTab();
let url = prompt("Nhập link google drive video: ", tab.url);
if (url == null) return;
docid = shared.getDocIdFromUrl(url);
if (!docid)
throw Error("Link không hợp lệ. Không tìm thấy id trong link.");
}
let res = await shared.getLinkVideoGDriveFromDocId(docid);
if (!res?.length) throw Error("Không tìm được link video.");
console.log(res);
openPopupWithHtml(
`<h1>${res[0].name}</h1>
${res
.map((_) => {
let name = _.name.replace(/ /g, "_");
return /*html*/ `<div>
<h1><a href="${_.url}" target="_blank">${_.quality}</a></h1>
<video src="${_.url}" controls style="max-width:95%" />
</div>`;
})
.join("<br/>")}`,
700,
700
);
} catch (e) {
alert("ERROR: " + e);
} finally {
closeLoading();
}
},
},
contentScript: {
onClick_: async () => {
let url = new URL(location.href);
const player_response = url.searchParams.get("player_response");
if (player_response) {
const json = JSON.parse(player_response);
console.log(document.title, json);
const { openPopupWithHtml } = await import("./helpers/utils.js");
openPopupWithHtml(
`<h1>${document.title}</h1>
${json.streamingData.formats
.map((_) => {
return /*html*/ `<div>
<h1><a href="${_.url}" target="_blank">${_.quality}</a></h1>
<video src="${_.url}" controls style="max-width:95%" />
</div>`;
})
.join("<br/>")}`,
700,
700
);
}
},
},
};
export const shared = {
// https://stackoverflow.com/a/16840612
getDocIdFromUrl: function (url) {
return url.match(/[-\w]{25,}(?!.*[-\w]{25,})/);
},
getDocIdFromWebsite: async function () {
const { runScriptInCurrentTab } = await import("./helpers/utils.js");
return await runScriptInCurrentTab(() => {
return window?.viewerData?.config?.id;
});
},
// Post: https://www.facebook.com/groups/j2team.community/posts/974953859503401/
getLinkVideoGDriveFromDocId: async function (docid) {
function parse(e) {
let result = {};
let a = new URLSearchParams(e);
a.entries().forEach((e) => {
result[e[0]] = e[1];
});
return result;
// Array.from(new URLSearchParams("").entries())
// .reduce((total, cur) => ({ ...total, [cur[0]]: cur[1] }), {});
}
function parseStream(e) {
var d = [];
return (
e.split(",").forEach(function (e) {
d.push(parse(e));
}),
d
);
}
async function getLink(docid) {
let res = await fetch(
"https://drive.google.com/get_video_info?docid=" + docid
);
let text = await res.text();
let json = parse(text);
if (json?.status === "fail") {
throw Error("FAILED: " + json.reason);
}
json.url_encoded_fmt_stream_map = parseStream(
json.url_encoded_fmt_stream_map
);
let result = json.url_encoded_fmt_stream_map.map(function (stream) {
let name = json.title.replace(/\+/g, " ");
return {
idfile: docid,
name: name,
quality: stream.quality,
url: stream.url,
};
});
return result;
}
return await getLink(docid);
},
};