-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtriggerSetup.js
115 lines (98 loc) · 4.64 KB
/
triggerSetup.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
/*
Settings are established by default to reset YouTube to look how it did just before the circular UI rework,
leave all settings true to return to how it used to be, this changes shapes but also some other things such
as colors and text.
*/
// const PROPER_DATES = true; //Changes main video date info from "<#> years/months/etc. ago" to it's formatted date
//Created pages to inject
let script = document.createElement('script');
let activator = document.createElement('script');
//Element to hold injected items and insert within
let injectedDiv = document.createElement("div");
//SWITCHING FOR CROSS-COMPATABILITY BETWEEN BROWSERS //TODO: Remove duplication
// Select the correct browser object
let determinedBrowserAPI = typeof browser !== 'undefined' ? browser : chrome;
function getProjectConfiguration() {//TODO: Reduce duplicate code
return fetch(determinedBrowserAPI.runtime.getURL('projectConfiguration.json'))
.then(response => response.json())
.then(data => data);
}
getProjectConfiguration().then(projectConfiguration => { localStorage.setItem("ProjectConfiguration", JSON.stringify(projectConfiguration));});
projectConfiguration = JSON.parse(localStorage.getItem("ProjectConfiguration"));
function logWithConfigMsg(...messages){
if(projectConfiguration === null){
getProjectConfiguration().then(gotProjectConfiguration => {
projectConfiguration = JSON.stringify(gotProjectConfiguration);
for(const message of messages){
console.log("["+projectConfiguration.log_header+"]: "+message);
}
});
}else{
for(const message of messages){
console.log("["+projectConfiguration.log_header+"]: "+message);
}
}
}
determinedBrowserAPI.permissions.request({
permissions: ['*://*.youtube.com/*']
}, function(granted) {
if (granted) {
logWithConfigMsg("Permission granted for access without clicking the extension button each time");
} else {
logWithConfigMsg("Permission denied for access without clicking the extension button each time");
}
});
async function createElementLink(sheetName) {
if(sheetName.endsWith(".css")){
logWithConfigMsg("Linking style name ="+sheetName);
const stylesheetLinkElement = document.createElement('link');
stylesheetLinkElement.rel = 'stylesheet';
stylesheetLinkElement.type = 'text/css';
stylesheetLinkElement.href = determinedBrowserAPI.runtime.getURL(sheetName);
return stylesheetLinkElement;
}else if(sheetName.endsWith(".js")){
logWithConfigMsg("Linking script name ="+sheetName);
const jsSheetLinkElement=document.createElement('script')
jsSheetLinkElement.setAttribute("type","text/javascript")
jsSheetLinkElement.setAttribute("src", determinedBrowserAPI.runtime.getURL(sheetName))
return jsSheetLinkElement;
}
// primary stylesheetLinkElement;
}
let helperFunctions = createElementLink("injection_parts/helper_functions.js");
document.head.appendChild(helperFunctions);
/* Video link windows inside the player that show up during playtime */
script.innerHTML = `
function applyGeneratedScripts(){
console.log("[`+projectConfiguration.log_header+`]: Activator call was received");
`;
script.innerHTML+=` };`;
injectedDiv.appendChild(script);
/* Runs the scrips again that were added to the page using this. YouTube switches videos
in a way that makes it difficult to just see if the page url changes. This also means
we do not need to worry about losing the function we created between different pages. */
activator.innerHTML = `
let video = document.getElementsByTagName('video')[0];
let lastSrc = "-1";
video.addEventListener('playing', function() {
if(video.src !== lastSrc){
lastSrc = video.src;
applyGeneratedScripts();
}
});
`;
let injectedInvisibleClickable = document.createElement("button");
injectedInvisibleClickable.id = "returnYoutubeUI_invisibleClickable";
injectedInvisibleClickable.nodeName = "returnYoutubeUI_invisibleClickable";
document.body.appendChild(injectedInvisibleClickable);
activator.innerHTML+= `
document.getElementById("returnYoutubeUI_invisibleClickable").addEventListener("click", function(){
console.log("[`+projectConfiguration.log_header+`]: injectedInvisibleClickable was 'clicked'");
applyGeneratedScripts();
});
`;
document.body.appendChild(injectedDiv);
document.body.appendChild(activator);
document.getElementById("returnYoutubeUI_invisibleClickable").addEventListener("change", function(){
logWithConfigMsg("injectedInvisibleClickableChangeListener was triggered");
});