-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtwitterDL.user.js
458 lines (370 loc) · 16.5 KB
/
twitterDL.user.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// ==UserScript==
// @name Twitter DL - Click "Always Allow"!
// @version 1.1.2
// @description Download twitter videos directly from your browser! (CLICK "ALWAYS ALLOW" IF PROMPTED!)
// @author realcoloride
// @license MIT
// @namespace https://twitter.com/*
// @namespace https://x.com/*
// @match https://twitter.com/*
// @match https://x.com/*
// @match https://pro.twitter.com/*
// @match https://pro.x.com/*
// @connect twitter-video-download.com
// @connect twimg.com
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @grant GM.xmlHttpRequest
// ==/UserScript==
(function() {
let injectedTweets = [];
const checkFrequency = 150; // in milliseconds
const apiEndpoint = "https://twitter-video-download.com/en/tweet/";
const downloadText = "Download"
const style =
`.dl-video {
padding: 6px;
padding-left: 5px;
padding-right: 5px;
margin-left: 5px;
margin-bottom: 2px;
border-color: black;
border-style: none;
border-radius: 10px;
color: white;
background-color: rgba(39, 39, 39, 0.46);
font-family: Arial, Helvetica, sans-serif;
font-size: xx-small;
cursor: pointer;
}
.dl-hq {
background-color: rgba(28, 199, 241, 0.46);
}
.dl-lq {
background-color: rgba(185, 228, 138, 0.46);
}
.dl-gif {
background-color: rgba(219, 117, 22, 0.46);
}
`;
// Styles
function injectStyles() {
const styleElement = document.createElement("style");
styleElement.textContent = style;
document.head.appendChild(styleElement);
}
injectStyles();
// Snippet extraction
function getRetweetFrame(tweetElement) {
let retweetFrame = null;
const candidates = tweetElement.querySelectorAll(`[id^="id__"]`);
candidates.forEach((candidate) => {
const candidateFrame = candidate.querySelector('div[tabindex="0"][role="link"]');
if (candidateFrame)
retweetFrame = candidateFrame;
});
return retweetFrame;
}
function getTopBar(tweetElement, isRetweet) {
// I know its kind of bad but it works
let element = tweetElement;
if (isRetweet) {
const retweetFrame = getRetweetFrame(tweetElement);
const videoPlayer = tweetElement.querySelector('[data-testid="videoPlayer"]');
const videoPlayerOnRetweet = retweetFrame.querySelector('[data-testid="videoPlayer"]')
const isVideoOnRetweet = (videoPlayer == videoPlayerOnRetweet);
if (videoPlayerOnRetweet && isVideoOnRetweet) element = retweetFrame;
else if (videoPlayerOnRetweet == null) element = tweetElement;
}
const userName = element.querySelector('[data-testid="User-Name"]');
if (isRetweet && element != tweetElement) return userName.parentNode.parentNode;
return userName.parentNode.parentNode.parentNode;
}
// Fetching
async function getMediasFromTweetId(tweetInformation) {
const id = tweetInformation.id;
const payload = {
"url": `${apiEndpoint}${id}`,
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "fr-FR,fr;q=0.9,en-US;q=0.8,en;q=0.7",
"cache-control": "max-age=0",
"sec-ch-ua": "\"Not/A)Brand\";v=\"99\", \"Google Chrome\";v=\"115\", \"Chromium\";v=\"115\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
"referrer": "https://twitter-video-download.com/en",
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET",
"mode": "cors",
"credentials": "omit"
};
const request = await GM.xmlHttpRequest(payload);
let lq = null;
let hq = null;
try {
const regex = /https:\/\/[a-zA-Z0-9_-]+\.twimg\.com\/[a-zA-Z0-9_\-./]+\.mp4/g;
const text = request.responseText;
const links = text.match(regex);
// Calculate the size of a video based on resolution
function calculateSize(resolution) {
const parts = resolution.split("x");
const width = parseInt(parts[0]);
const height = parseInt(parts[1]);
return width * height;
}
if (!links) return null;
// Map links to objects with resolution and size
const linkObjects = links.map(link => {
const resolutionMatch = link.match(/\/(\d+x\d+)\//);
const resolution = resolutionMatch ? resolutionMatch[1] : "";
const size = calculateSize(resolution);
return { link, resolution, size };
});
// Sort linkObjects based on size in descending order
linkObjects.sort((a, b) => a.size - b.size);
// Create a Set to track seen links and store unique links
const uniqueLinks = new Set();
const deduplicatedLinks = [];
for (const obj of linkObjects) {
if (!uniqueLinks.has(obj.link)) {
uniqueLinks.add(obj.link);
deduplicatedLinks.push(obj.link);
}
}
if (tweetInformation.isGif && tweetInformation.tabIndex == "-1" ||
links[0].startsWith('https://video.twimg.com/tweet_video/')
) {
lq = links[0];
} else {
lq = deduplicatedLinks[0];
if (deduplicatedLinks.length > 1) hq = deduplicatedLinks[deduplicatedLinks.length-1];
// first quality is VERY bad so if can swap to second (medium) then its better
if (deduplicatedLinks.length > 2) lq = deduplicatedLinks[1];
}
} catch (error) {
console.error(error);
return null;
}
return {lq, hq};
}
// Downloading
async function downloadFile(button, url, mode, filename) {
const baseText = `${downloadText} (${mode.toUpperCase()})`;
button.disabled = true;
button.innerText = "Downloading...";
console.log(`[TwitterDL] Downloading Tweet URL (${mode.toUpperCase()}): ${url}`);
function finish() {
if (button.innerText == baseText) return;
button.disabled = false;
button.innerText = baseText;
}
GM.xmlHttpRequest({
method: 'GET',
url: url,
responseType: 'blob',
onload: function(response) {
const blob = response.response;
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.setAttribute('download', filename);
link.click();
URL.revokeObjectURL(link.href);
button.innerText = 'Downloaded!';
button.disabled = false;
setTimeout(finish, 1000);
},
onerror: function(error) {
console.error('[TwitterDL] Download Error:', error);
button.innerText = 'Download Failed';
setTimeout(finish, 1000);
},
onprogress: function(progressEvent) {
if (progressEvent.lengthComputable) {
const percentComplete = Math.round((progressEvent.loaded / progressEvent.total) * 100);
button.innerText = `Downloading: ${percentComplete}%`;
} else
button.innerText = 'Downloading...';
}
});
}
function createDownloadButton(tweetInformation, url, tag) {
const button = document.createElement("button");
button.hidden = true;
const username = tweetInformation.username;
const filename = `TwitterDL_${username}_${tweetInformation.id}`;
button.classList.add("dl-video", `dl-${tag}`);
button.innerText = `${downloadText} (${tag.toUpperCase()})`;
button.setAttribute("href", url);
button.setAttribute("download", "");
button.addEventListener('click', async() => {
await downloadFile(button, url, tag, filename);
});
button.hidden = false;
return button;
}
function createDownloadButtons(tweetElement) {
const tweetInformation = getTweetInformation(tweetElement);
if (!tweetInformation) return;
getMediasFromTweetId(tweetInformation).then((medias) => {
if (!medias) return;
const retweetFrame = getRetweetFrame(tweetElement);
const isRetweet = (retweetFrame != null);
let lowQualityButton;
let highQualityButton;
const lq = medias.lq;
const hq = medias.hq;
if (lq) lowQualityButton = createDownloadButton(tweetInformation, lq, tweetInformation.isGif ? "gif" : "lq");
if (hq && !tweetInformation.isGif) highQualityButton = createDownloadButton(tweetInformation, hq, "hq");
const videoPlayer = isRetweet ? tweetElement.querySelector('[data-testid="videoPlayer"]') : null;
const videoPlayerOnRetweet = isRetweet ? retweetFrame.querySelector('[data-testid="videoPlayer"]') : null;
const topBar = getTopBar(tweetElement, isRetweet);
const threeDotsElement = topBar.lastChild
const isVideoOnRetweet = (videoPlayer == videoPlayerOnRetweet);
if (!lowQualityButton && !highQualityButton) return;
// Order: HQ then LQ
if (videoPlayer != null && isRetweet && isVideoOnRetweet) {
// Add a little side dot
addSideTextToRetweet(tweetElement, " · ", 6, 5);
if (highQualityButton) topBar.appendChild(highQualityButton);
if (lowQualityButton) topBar.appendChild(lowQualityButton);
} else {
if (lowQualityButton) topBar.insertBefore(lowQualityButton, threeDotsElement);
if (highQualityButton) topBar.insertBefore(highQualityButton, lowQualityButton);
}
})
}
function addSideTextToRetweet(tweetElement, text, forcedMargin, forcedWidth) {
const timeElement = tweetElement.querySelector("time");
const computedStyles = window.getComputedStyle(timeElement);
// Make a new text based on the font and color
const textElement = timeElement.cloneNode(true);
textElement.innerText = text;
textElement.setAttribute("datetime", "");
for (const property of computedStyles) {
textElement.style[property] = computedStyles.getPropertyValue(property);
}
textElement.style.overflow = "visible";
textElement.style["padding-left"] = "4px";
textElement.style["margin-left"] = forcedMargin || 0;
const tweetAvatarElement = tweetElement.querySelectorAll('[data-testid="Tweet-User-Avatar"]')[1];
const targetTweetBar = tweetAvatarElement.parentNode;
targetTweetBar.appendChild(textElement);
const contentWidth = textElement.scrollWidth;
textElement.style.width = (forcedWidth || contentWidth) + 'px';
injectedFallbacks.push(tweetElement);
}
// Page information gathering
function getTweetsInPage() {
return document.getElementsByTagName("article");
}
let injectedFallbacks = [];
function getTweetInformation(tweetElement) {
let information = {};
// ID
// Check the tweet timestamp, it has a link with the id at the end
// In case something goes wrong, a fallback text is shown
let id = null;
let username = null;
let tweetUrl = null;
let isGif = false;
let tabIndex = null;
const retweetFrame = getRetweetFrame(tweetElement);
const isRetweet = (retweetFrame != null);
const videoPlayer = isRetweet ? retweetFrame.querySelector('[data-testid="videoPlayer"]') : null;
const isPost = (isStatusUrl(window.location.href));
tabIndex = tweetElement.getAttribute('tabindex');
const regex = /^https:\/\/twitter\.com\/([^\/]+)\/status\/(\d+)/;
function setInfo(url) {
const match = url.match(regex);
id = match[2];
username = match[1];
tweetUrl = url;
}
const url = window.location.href;
try {
setInfo(url);
} catch {}
function fallback() {
if (injectedFallbacks.includes(tweetElement)) return;
console.log("[TwitterDL] Twitter quote retweets from statuses are not supported yet, sorry! Throwing fallback...");
addSideTextToRetweet(tweetElement, " · Open to Download");
}
try {
if (isRetweet) {
if (isPost) {
const hasRetweetVideoPlayer = (videoPlayer != null);
if (hasRetweetVideoPlayer)
fallback();
} else fallback();
} else {
const timeElement = tweetElement.querySelector("time");
const timeHref = timeElement.parentNode;
const tweetUrl = timeHref.href;
if (tweetUrl) setInfo(tweetUrl);
else fallback();
}
} catch (error) {
try {
fallback();
} catch (error) {}
}
// VideoPlayer element
const videoPlayerElement = tweetElement.querySelector('[data-testid="videoPlayer"]');
const spanElement = videoPlayerElement.querySelector('div[dir="ltr"] > span');
if (spanElement)
isGif = spanElement.innerText == "GIF";
if (!id) return;
information.id = id;
information.username = username;
information.url = tweetUrl;
information.videoPlayer = videoPlayerElement;
information.isGif = isGif;
information.tabIndex = tabIndex;
// Play button
return information;
}
// Page injection
async function injectAll() {
const tweets = getTweetsInPage();
for (let i = 0; i < tweets.length; i++) {
const tweet = tweets[i];
const alreadyInjected = injectedTweets.includes(tweet);
if (!alreadyInjected) {
const videoPlayer = tweet.querySelector('[data-testid="videoPlayer"]');
const isVideo = (videoPlayer != null);
if (!isVideo) continue;
createDownloadButtons(tweet);
injectedTweets.push(tweet);
}
}
}
function checkForInjection() {
const tweets = getTweetsInPage();
const shouldInject = (injectedTweets.length != tweets.length);
if (shouldInject) injectAll();
}
function isStatusUrl(url) {
const statusUrlRegex = /^https?:\/\/(pro\.x|x)\.com\/\w+\/status\/\d+$/;
return statusUrlRegex.test(url);
}
function isValidUrl(url) {
const tweetUrlRegex = /^https?:\/\/(pro\.x|x)\.com\/\w+(\/\w+)*$/;
return tweetUrlRegex.test(url) || isStatusUrl(window.location.href);
}
if (isValidUrl(window.location.href)) {
console.log("[TwitterDL] by (real)coloride - 2023 // Loading... ");
setInterval(async() => {
try {
checkForInjection();
} catch (error) {
console.error("[TwitterDL] Fatal error: ", error);
}
}, checkFrequency);
}
})();