-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-script.js
60 lines (56 loc) · 1.94 KB
/
content-script.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
// Function that detects content frames and injects the button
function mainLoop() {
// Get the source attribute of the first child of the 'topid-display' element
var source = null;
try {
source = document
.getElementsByClassName("topic-display")
.item(0)
.firstElementChild.getAttribute("src");
} finally {
console.log("Source: " + source)
}
var newTabButtonList = document.getElementsByClassName("open-in-new-tab-btn");
console.log("Button list: " + newTabButtonList.length);
// No source, but button does not exist: do nothing
if (source == null && newTabButtonList.length == 0) {
return;
}
// No source, but button exists: hide button
if (source == null && newTabButtonList.length > 0) {
newTabButtonList.item(0).style.visibility = "hidden";
return;
}
// Source exists and button exists: show button
if (newTabButtonList.length > 0) {
newTabButtonList.item(0).style.visibility = "visible";
return;
}
// Source exists and button does not exist: insert button
var buttonTray = document
.getElementsByClassName("header-button-tray")
.item(0);
if (buttonTray == null) return;
insertButton(buttonTray, source);
}
function insertButton(buttonTray, source) {
buttonTray.insertAdjacentHTML(
"beforeend",
`
<button class="open-in-new-tab-btn" onclick=
"window.open('${source}')">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path d="M19 19H5V5h7V3H5c-1.11 0-2 .9-2 2v14c0 1.1.89 2 2 2h14c1.1 0 2-.9 2-2v-7h-2v7zM14 3v2h3.59l-9.83 9.83 1.41 1.41L19 6.41V10h2V3h-7z"/></svg>
</button>
`
);
}
// Initial run
setTimeout(function () {
mainLoop();
}, 1000);
// Continuous checking is required as content frames are not present when a BrightSpace 'unit' is selected
document.addEventListener("click", function () {
setTimeout(function () {
mainLoop();
}, 1000);
});