-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweetdeck_media_downloader.user.js
144 lines (124 loc) · 4.71 KB
/
tweetdeck_media_downloader.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
// ==UserScript==
// @name Ez Twimg Downloader DECK
// @description TweetDeckの画像を簡単に保存するUserScriptです。
// @author 00A0
// @match https://tweetdeck.twitter.com/*
// @match https://pro.twitter.com/*
// @match https://pbs.twimg.com/media/*
// @version 3.2
// @grant GM_download
// @license The Unlicense
// @updateURL https://github.com/ZEROssk/TweetDeck_image_Downloader/raw/master/tweetdeck_media_downloader.user.js
// ==/UserScript==
// Original Code author
// yanorei32
// Original Code
// https://github.com/Yanorei32/EzTwimgDownloaderGen2
(function () {
'use strict';
// download filename format
// https://twitter.com/{userName}/status/{tweetId}
// https://pbs.twimg.com/media/{randomName}.{extension}
const FILENAME_FORMAT = 'Twitter-{tweetId}-{userName}-{randomName}.{extension}';
const processedLists = new WeakMap();
const addCSS = () => {
const btnCSS =
'<style>' +
'.download:hover>div>div>div{color: rgba(29, 161, 242, 1.0);}' +
'.download:hover>div>div>div>div>div{background-color: rgba(29, 161, 242, 0.1);}' +
'</style>'
;
document.head.insertAdjacentHTML('beforeend', btnCSS);
};
const toArray = (arrayLikeObject) => {
return Array.prototype.slice.call(arrayLikeObject);
};
const getArticleByChildElement = (e) => {
while (e.tagName != 'ARTICLE') {
e = e.parentElement;
}
return e;
};
const parseNewFormatURI = (uri) => {
const splittedURI = uri.split('/')[4].split('?');
return {
'randomName': splittedURI[0],
'extension': splittedURI[1].split('&')[0].split('=')[1],
};
};
const getTweetInfoByImage = (e) => {
while (e.tagName != 'A') {
e = e.parentElement;
};
const splittedHref = e.href.split('/');
return {
'displayName': splittedHref[3],
'tweetId': splittedHref[5],
};
};
const getImagesContainerByImageElement = (e) => {
while (e.tagName != 'A') {
e = e.parentElement;
};
return e.parentElement.parentElement.parentElement.parentElement;
};
const addButtons = (e) => {
const imgs = document.getElementsByTagName('img');
if (imgs == undefined) return;
for (let i = 0; i < imgs.length; i++) {
const img = imgs[i];
if (img.parentElement.getAttribute('data-testid') != "tweetPhoto") continue;
const article = getArticleByChildElement(img);
if (processedLists.has(article)) continue;
processedLists.set(article, 1);
let buttonGr = article.querySelector('div[role="group"]:last-of-type, ul.tweet-actions, ul.tweet-detail-actions');
let buttonPNode = Array.from(buttonGr.querySelectorAll(':scope>div>div, li.tweet-action-item>a, li.tweet-detail-action-item>a')).pop().parentNode;
let button = buttonPNode.cloneNode(true);
button.querySelector('svg').innerHTML =
'<a class="tweet-action svgDownload">' +
'<path d="M3,14 v5 q0,2 2,2 h14 q2,0 2,-2 v-5 M7,10 l4,4 q1,1 2,0 l4,-4 M12,3 v11" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" />' +
'</a>'
;
button.classList.add('download');
buttonGr.insertBefore(button, buttonPNode.nextSibling);
button.addEventListener('click', () => {
const tweetImgs = getImagesContainerByImageElement(img).getElementsByTagName('img');
const tweetInfo = getTweetInfoByImage(img);
for (let j = 0; j < tweetImgs.length; j++) {
const parsedURI = parseNewFormatURI(tweetImgs[j].src);
const maximumQualityURI = `https://pbs.twimg.com/media/${parsedURI.randomName}.${parsedURI.extension}:orig`;
const downloadFileName = FILENAME_FORMAT
.replace(/{userName}/g, tweetInfo.displayName)
.replace(/{tweetId}/g, tweetInfo.tweetId)
.replace(/{randomName}/g, parsedURI.randomName)
.replace(/{extension}/g, parsedURI.extension);
if (!(event || window.event).shiftKey) {
GM_download(maximumQualityURI, downloadFileName);
} else {
window.open(maximumQualityURI);
};
}
});
}
};
//const destroyPromotion = (e) => {
// const spantags = e.getElementsByTagName('span');
// //getArticleByChildElement(spantags[spantags.length-1]).parentElement.parentElement.parentElement.parentElement.textContent = null;
//};
new MutationObserver((records) => {
records.forEach((record) => {
toArray(record.addedNodes).forEach((addedNode) => {
if (addedNode.nodeType != Node.ELEMENT_NODE) return;
addButtons(addedNode);
//destroyPromotion(addedNode);
});
});
}).observe(
document.body,
{
childList: true,
subtree: true,
}
);
addCSS();
})();