-
Notifications
You must be signed in to change notification settings - Fork 0
/
madr.js
64 lines (52 loc) · 1.67 KB
/
madr.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
async function openAsBlob(response) {
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
window.open(url);
window.URL.revokeObjectURL(url);
}
function load(a, link) {
document.body.style.cursor = 'wait';
a.style.cursor = 'wait';
fetch(link).then(openAsBlob).then(() => {
document.body.style.cursor = 'default'
a.style.cursor = 'pointer';
});
return false;
}
function loadAnnotator(a, link) {
document.body.style.cursor = 'wait';
a.style.cursor = 'wait';
fetch(link)
.then((res) => { return res.text(); })
.then((resText) => {
const dom = Document.parseHTMLUnsafe(resText);
const link = dom.getElementById("myprinturl").innerHTML;
return fetch(link);
}).then(openAsBlob).then(() => {
document.body.style.cursor = 'default'
a.style.cursor = 'pointer';
});
return false;
}
const script = document.createElement("script");
// Inject functions into page DOM
script.innerHTML = `
${openAsBlob.toString()}
${load.toString()}
${loadAnnotator.toString()}
`;
document.head.appendChild(script);
const match = /(?:\/pluginfile.php\/[0-9]+\/mod_(?:folder\/content)|(?:assign\/introattachment)|(?:forum\/attachment)|(?:assignsubmission_file\/submission_files))|(?:\/mod\/resource\/view.php)/;
const annotatorMatch = /(?:\/mod\/pdfannotator\/view.php)/;
const main = document.querySelectorAll('[role="main"]')[0];
const anchors = main?.getElementsByTagName('a') ?? [];
for (const elem of anchors) {
const link = elem.href;
if (match.test(link)) {
elem.setAttribute('onclick', `return load(this, '${link}');`);
continue;
};
if (annotatorMatch.test(link)) {
elem.setAttribute('onclick', `return loadAnnotator(this, '${link}');`)
}
};