-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
232 lines (214 loc) · 6.42 KB
/
content.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
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 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": () => {
function maxSizeAsset(url) {
return url.split("?")[0] + "?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
getElementByClassNameStart(
"stickerIcon"
).parentNode.parentNode.parentNode.click();
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 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 otherSizesButton = document.getElementsByClassName(
"MMViewerButtons-ImageSizes"
)[0];
if (otherSizesButton) {
otherSizesButton.click(); // open list
const sizeSelector = document.getElementsByClassName(
"MMViewerButtons-ImageSizesPopup"
)[0];
const resolutions = sizeSelector.getElementsByClassName(
"MMViewerButtons-ImageSizesListItem"
);
otherSizesButton.click(); // close list
return resolutions[0].href;
} else {
const downloadButton = document.getElementsByClassName("MMViewerButtons-OpenImage")[0];
return downloadButton.href
}
};
function getDownloadUrl(hostname) {
if (getters.hasOwnProperty(hostname)) {
return getters[hostname]();
} 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);
if (
downloadUrl === undefined &&
videoDownloadBlacklist.indexOf(window.location.hostname) === -1
) {
downloadUrl = getCurrentlyPlayingVideo();
}
if (downloadUrl !== undefined) {
e.preventDefault();
if (downloadUrl !== "") {
chrome.extension.sendMessage({ url: downloadUrl }, function () {});
}
}
}
},
false
);