-
Notifications
You must be signed in to change notification settings - Fork 7
/
written-interview-in-new-tab.user.js
55 lines (50 loc) · 2.24 KB
/
written-interview-in-new-tab.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
// ==UserScript==
// @name Greenhouse written interviews in a new tab
// @namespace https://canonical.com/
// @version 1.0.1
// @author Canonical's workplace engineering team
// @description Open written interviews in a new tab instead of downloading "File1.pdf" files.
// @homepage https://github.com/canonical/greenhouse-browser-scripts
// @homepageURL https://github.com/canonical/greenhouse-browser-scripts
// @icon https://icons.duckduckgo.com/ip3/greenhouse.io.ico
// @updateURL https://raw.githubusercontent.com/canonical/greenhouse-browser-scripts/main/written-interview-in-new-tab.user.js
// @downloadURL https://raw.githubusercontent.com/canonical/greenhouse-browser-scripts/main/written-interview-in-new-tab.user.js
// @supportURL https://github.com/canonical/greenhouse-browser-scripts/issues
// @match https://canonical.greenhouse.io/guides/**/people/**
// @match https://canonical.greenhouse.io/scorecards/**
// ==/UserScript==
const downloadLinks = Array.from(document.querySelectorAll(
"a[data-download-url*='person_attachments/downloads']"
)).filter(el => el.textContent.endsWith(".pdf"));
downloadLinks.forEach((linkEl) => {
// remove existing React listeners
const newLinkEl = linkEl.cloneNode(true);
linkEl.replaceWith(newLinkEl);
newLinkEl.href = newLinkEl.getAttribute("data-download-url");
// download and open the PDF in a new tab
newLinkEl.onclick = onClick;
});
function openPdf(pdf) {
const blobStore = new Blob([pdf], { type: "application/pdf" });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(blobStore);
return;
}
const data = window.URL.createObjectURL(blobStore);
const link = document.createElement("a");
link.download = "Written interview.pdf";
document.body.appendChild(link);
window.open(data, "_blank");
window.URL.revokeObjectURL(data);
link.remove();
}
async function onClick(evt) {
evt.preventDefault();
const url = evt.target.href;
const redirectionResponse = await fetch(url);
const pdfUrl = redirectionResponse.url;
const response = await fetch(pdfUrl);
const pdfBlob = await response.blob();
openPdf(pdfBlob);
return false;
}