-
Notifications
You must be signed in to change notification settings - Fork 0
/
CtrlSave.js
274 lines (250 loc) · 7.71 KB
/
CtrlSave.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
// ==UserScript==
// @name CtrlSave
// @namespace Violentmonkey Scripts
// @match *://*/*
// @grant GM_download
// @version 1.0
// @author Layerex
// @description Save images from various sites by pressing Ctrl+S.
// ==/UserScript==
function getElementByClassNameStart(classNameStart, node = document) {
return node.querySelector(
`[class^="${classNameStart}"],[class*=" ${classNameStart}"]`
);
}
function getElementsByClassNameStart(classNameStart, node = document) {
return node.querySelectorAll(
`[class^="${classNameStart}"],[class*=" ${classNameStart}"]`
);
}
function getElementByXpath(path) {
return document.evaluate(
path,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
}
function getBackgroundImage(node) {
const value = node.style.backgroundImage;
const urlRegex = /url\("(.*)"\)/;
return value.match(urlRegex)[1];
}
// Get first image in node
function getImage(node) {
return node.getElementsByTagName("img")[0];
}
// get first video in node
function getVideo(node) {
return node.getElementsByTagName("video")[0];
}
// Return values:
// - undefined - nothing to download
// - "" - no manual downloading needed
const getters = {
"vk.com": () => {
const storyVideo = document.getElementsByClassName("stories_video")[0];
if (storyVideo) {
return storyVideo.src;
}
const storyPhoto = document.getElementsByClassName("stories_photo")[0];
if (storyPhoto) {
return getBackgroundImage(storyPhoto);
}
const gifVideo = document.getElementsByClassName("pages_gif_img")[0];
if (gifVideo) {
return gifVideo.src;
}
const imageDiv = document.getElementById("pv_photo");
if (imageDiv) {
return getImage(imageDiv).src;
}
},
"discord.com": (alt) => {
function maxSizeAsset(url) {
const sizeRegex = new RegExp("[?&]size=[0-9]+");
return url.replace(sizeRegex, "?size=4096")
}
function maxSizeImage(url) {
const widthRegex = new RegExp("[?&]width=[0-9]+");
const heightRegex = new RegExp("[?&]height=[0-9]+");
return url.replace(widthRegex, "").replace(heightRegex, "");
}
// Close sticker dialog bound to Ctrl+S immediately
// When in channels user can't send messages to it doesn't open
try {
getElementByClassNameStart(
"stickerIcon"
).parentNode.parentNode.parentNode.click();
} catch {}
if (alt) {
const bannerDiv = getElementByClassNameStart("bannerPremium");
if (bannerDiv) {
const bannerUrl = getBackgroundImage(bannerDiv);
return maxSizeAsset(bannerUrl);
}
}
const backdrop = getElementByClassNameStart("backdrop");
if (backdrop) {
const currentContainer = backdrop.parentNode;
const video = getVideo(currentContainer);
if (video) {
return video.src;
}
const image = getImage(currentContainer);
if (image) {
return maxSizeAsset(maxSizeImage(image.src));
}
return undefined;
}
const avatarContainer =
getElementByClassNameStart("userPopout") ||
getElementByClassNameStart("topSection");
if (avatarContainer) {
const avatarDiv = getElementByClassNameStart(
"avatarStack",
avatarContainer
);
if (avatarDiv) {
const avatarUrl = getImage(avatarDiv).src;
return maxSizeAsset(avatarUrl);
}
}
const emojiContainer =
getElementByClassNameStart("emojiSection") ||
getElementByClassNameStart("reactionTooltipInner");
if (emojiContainer) {
const emojiUrl = getImage(emojiContainer).src;
return maxSizeAsset(emojiUrl);
}
},
"app.element.io": () => {
document.getElementsByClassName("mx_ImageView_button_download")[0].click();
return "";
},
"2ch.hk": () => {
const videoDiv = document.getElementById("js-mv-player");
if (videoDiv) {
return videoDiv.getElementsByTagName("source")[0].src;
}
const imageDiv = document.getElementById("js-mv-main");
if (imageDiv) {
return getImage(imageDiv).src;
}
},
"teddit.net": () => {
const post = document.getElementById("post");
const imageDiv = post.getElementsByClassName("image")[0];
if (imageDiv) {
const image = getImage(imageDiv);
if (image) {
return image.src;
}
}
},
"web.skype.com": () => {
// const imageDiv = getElementByXpath(
// "/html/body/div[1]/div/div/div[2]/div/div/div[1]/div[2]/div[1]/div/div/div/div/div/div/div/div/div[2]/div/div"
// );
const imageDiv = document.querySelector(
`[style^='position: absolute; overflow: visible; background-color: transparent; flex-grow: 0; flex-shrink: 0; inset: 0px; align-items: stretch; background-position: center center; background-repeat: no-repeat; background-image: url("https://api.asm.skype.com/v1/objects/'][style$='/views/imgpsh_mobile_save_anim"); background-size: contain; border-style: none; display: flex;']`
);
return getBackgroundImage(imageDiv).replace("mobile_save", "fullsize");
},
};
const yandex = () => {
const sizeWidth = (button) => {
return +button.textContent.split("×")[0];
};
const currentSizeButton = document.getElementsByClassName(
"MMViewerButtons-OpenImage"
)[0];
const otherSizesButton = document.getElementsByClassName(
"MMViewerButtons-ImageSizes"
)[0];
if (otherSizesButton) {
otherSizesButton.click(); // open list
const sizeSelector = document.getElementsByClassName(
"OpenImageButton-Popup"
)[0];
const otherSizeButtons = sizeSelector.getElementsByClassName(
"OpenImageButton-ListItem"
);
otherSizesButton.click(); // close list
const maxOtherSizeButton = otherSizeButtons[0];
const currentSize = sizeWidth(otherSizesButton);
const maxOtherSize = sizeWidth(maxOtherSizeButton);
if (currentSize > maxOtherSize) {
return currentSizeButton.href;
} else {
return maxOtherSizeButton.href;
}
} else {
return currentSizeButton.href;
}
};
function getDownloadUrl(hostname, alt) {
if (getters.hasOwnProperty(hostname)) {
return getters[hostname](alt);
} else {
if (hostname.split(".", 1)[0] === "yandex") {
return yandex();
}
}
}
// Sites, where videos are undownloadable or not downloaded by regular means
const videoDownloadBlacklist = ["vk.com", "2ch.hk"];
function isVideoPlaying(video) {
return (
video.currentTime > 0 &&
!video.paused &&
!video.ended &&
video.readyState > 2
);
}
function getCurrentlyPlayingVideo() {
const videos = document.getElementsByTagName("video");
for (let i = 0; i < videos.length; ++i) {
const video = videos.item(i);
if (isVideoPlaying(video)) {
if (video.src) {
return video.src;
} else {
const sources = video.getElementsByTagName("source");
for (source of sources) {
if (source.src) {
return source.src;
}
}
}
}
}
}
document.addEventListener(
"keydown",
function (e) {
if (
(window.navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey) &&
e.keyCode === 83
) {
let downloadUrl = getDownloadUrl(window.location.hostname, e.shiftKey);
if (
downloadUrl === undefined &&
videoDownloadBlacklist.indexOf(window.location.hostname) === -1
) {
downloadUrl = getCurrentlyPlayingVideo();
}
if (downloadUrl !== undefined) {
e.preventDefault();
if (downloadUrl !== "") {
const downloadUrlParts = downloadUrl.split("/")
const name = downloadUrlParts[downloadUrlParts.length - 1].split("?")[0]
console.log("CtrlSave: Downloading", downloadUrl)
GM_download(downloadUrl, name);
}
}
}
},
false
);