-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjected.js
56 lines (49 loc) · 1.86 KB
/
injected.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
console.log("GMDL injected");
function* execAll(str, regex) {
if (!regex.global) {
console.error('RegExp must have the global flag to retrieve multiple results.');
}
let match;
while (match = regex.exec(str)) {
yield { 'id': match[1], 'caption': match[2] };
}
}
function sleep(time) {
return new Promise((resolve) => setTimeout(resolve, time));
}
const postfix = ["Image", "圖片", "Image Shortcut", "圖片捷徑"];
var matches;
document.addEventListener("pointermove", (_) => {
// append the direct link if it is removed by google drive
sleep(500).then(() => {
let elm_cnt = 0;
matches.forEach(function (element, _) {
// console.log(element);
if (!postfix.some(s => element['caption'].trim().includes(s))) {
return;
}
const elm = document.querySelector('div[data-id="' + element['id'] + '"]');
if (elm === null) {
return;
}
elm_cnt++;
if (elm.nextSibling.className != "injected-gmdl-link") {
elm.insertAdjacentHTML('afterend',
'<div class="injected-gmdl-link" style="z-index: 999; text-align: center;"><a href="https://lh3.googleusercontent.com/d/' + element['id'] + '" target="_blank">direct link</a></div>');
// console.log("https://lh3.googleusercontent.com/d/" + element['id']);
}
});
if (elm_cnt == 0) {
// trigger reload for changing folder
let source = document.getElementsByTagName('html')[0].innerHTML;
const myRegexp = new RegExp("data-id=\"(.*?)\"[\\s\\S]*?aria-label=\"(.*?)\"", "g");
matches = [...execAll(source, myRegexp)];
}
});
});
async function main() {
let source = document.getElementsByTagName('html')[0].innerHTML;
const myRegexp = new RegExp("data-id=\"(.*?)\"[\\s\\S]*?aria-label=\"(.*?)\"", "g");
matches = [...execAll(source, myRegexp)];
}
main().then().catch(err => { console.error(err); })