blob: e317d2ba6d0ed33f63dcafb19cc32880e7311fef [file] [log] [blame]
j5e67d8e2022-11-08 22:55:04 -08001(function () {
j6df32632022-11-10 08:59:16 -08002 try {
3 function verifyCanvasInstance(cb) {
j941bd302022-11-11 09:13:50 -08004 window.addEventListener("load", function () {
5 //What are the odds that another non-Canvas page uses this?
6 //Hopefully slim-to-none because I'm using this as an indicator of a Canvas instance...
j6df32632022-11-10 08:59:16 -08007 if (window.ENV?.current_user_global_id) {
j941bd302022-11-11 09:13:50 -08008 //Remember that this is Canvas to speed up future page loads
j6df32632022-11-10 08:59:16 -08009 window.localStorage.setItem("dtpsThinksThisIsCanvas", "true");
10 cb();
j5e67d8e2022-11-08 22:55:04 -080011 }
j941bd302022-11-11 09:13:50 -080012 });
j5e67d8e2022-11-08 22:55:04 -080013 }
14
j6df32632022-11-10 08:59:16 -080015 //Is this a Canvas instance? (always YES for instructure.com subdomains)
16 const knownCanvasInstance = (
j427abfa2023-08-22 13:17:31 -070017 window.location.hostname.endsWith("instructure.com")
18 || window.localStorage.getItem("dtpsThinksThisIsCanvas") === "true"
j6df32632022-11-10 08:59:16 -080019 );
20
21 if ((window.location.pathname == "/power+") || (window.location.pathname == "/power+/")) {
22 if (!knownCanvasInstance) {
23 verifyCanvasInstance(() => window.location.reload());
24 return;
25 }
26
27 //Check for light or dark theme
28 var light = false;
29 if (window.localStorage.fluidTheme == "light") {
30 light = true;
31 } else if (((window.localStorage.fluidTheme == "system") || (window.localStorage.fluidTheme == "auto")) && !window.matchMedia("(prefers-color-scheme: dark)").matches) {
32 light = true;
33 }
34
35 //Globally-Unique User ID
j427abfa2023-08-22 13:17:31 -070036 let guuid = window.ENV && window.ENV.current_user_global_id;
j6df32632022-11-10 08:59:16 -080037
38 const useClassicDTPS = window.location.search && window.location.search.includes("classicEdition=true");
j469ee102023-02-27 17:15:52 -080039 if (useClassicDTPS) light = false;
j427abfa2023-08-22 13:17:31 -070040
41 let dtpsHeadLoaded = false;
42 function injectDTPSLoaderHead(node) {
43 if (dtpsHeadLoaded) return;
44 dtpsHeadLoaded = true;
45 node.insertAdjacentHTML("afterbegin", /*html*/`
j6df32632022-11-10 08:59:16 -080046 <link dtps="true" rel="shortcut icon" href="https://powerplus.app/favicon.png" type="image/png">
47 <meta dtps="true" name="viewport" content="width=device-width, initial-scale=1">
48 <meta dtps="true" charset="utf-8">
49 <title dtps="true">Power+</title>
50 <meta dtps="true" name="description" content="A better UI for Canvas LMS">
51 <meta dtps="true" name="author" content="jottocraft">
52 `);
j427abfa2023-08-22 13:17:31 -070053 }
54
55 let dtpsBodyLoaded = false;
56 function injectDTPSLoaderBody(node) {
57 if (dtpsBodyLoaded) return;
58 dtpsBodyLoaded = true;
59 node.insertAdjacentHTML("afterbegin", /*html*/`
60 <div dtps="true" id="dtpsNativeOverlay" style="background-color: inherit; position: fixed; top: 0px; left: 0px; width: 100%; height: 100vh; z-index: 99;text-align: center;z-index: 999;transition: opacity 0.2s;">
61 <img dtps="true" style="height: 90px; margin-top: 132px;" src="${useClassicDTPS ? "https://i.imgur.com/fqqPF9i.png" : (light ? "https://i.imgur.com/bZHcHoE.png" : "https://i.imgur.com/NrjqsVI.png")}" />
62 <br dtps="true" />
63 <div dtps="true" class="progress"><div id="dtpsLoadingScreenBar" dtps="true" class="indeterminate"></div></div>
64 <style dtps="true">body {background-color: ${light ? "white" : "#151515"}; --crxElements: ${light ? "#ececec" : "#2b2b2b"}; --crxText: ${light ? "#333" : "#efefef"}; overflow: hidden;}*,:after,:before{box-sizing:border-box}.progress{background:var(--crxElements);position:relative;width:600px;height:5px;overflow:hidden;border-radius:12px;backdrop-filter:opacity(.4);display:inline-block;margin-top:75px}.progress .indeterminate{position:absolute;background:#e3ba4b;height:5px;animation:indeterminate 1.4s infinite;animation-timing-function:linear}@keyframes indeterminate{0%{width:5%;left:-15%}to{width:100%;left:110%}}p{font-family:BlinkMacSystemFont,-apple-system,"Segoe UI",Roboto,Oxygen,Ubuntu,Cantarell,"Fira Sans","Droid Sans","Helvetica Neue",Helvetica,Arial,sans-serif;color: var(--crxText);margin-top: 24px;}</style>
65 </div>
66 `);
67 }
68
69 const observer = new MutationObserver(mutations => {
70 mutations.forEach(({ addedNodes }) => {
71 addedNodes.forEach(node => {
72 //Power+ preloader
73 if (node.nodeType === 1 && node.tagName === 'BODY') {
74 injectDTPSLoaderBody(node);
75 } else if (node.nodeType === 1 && node.tagName === 'HEAD') {
76 injectDTPSLoaderHead(node);
j6df32632022-11-10 08:59:16 -080077 } else if (node.nodeType === 1 && node.tagName === 'SCRIPT' && (node.textContent && node.textContent.includes('"current_user"'))) {
j6df32632022-11-10 08:59:16 -080078 guuid = node.textContent.match(/"current_user_global_id":"(.*?)"/)[1];
j427abfa2023-08-22 13:17:31 -070079 node.remove();
j6df32632022-11-10 08:59:16 -080080 } else if (node.nodeType === 1 && node.getAttribute("dtps") != "true") {
81 //Node is not added by dtps loader
82 node.remove();
j5e67d8e2022-11-08 22:55:04 -080083 }
84 })
85 })
86 });
87
j6df32632022-11-10 08:59:16 -080088 //Start MutationObserver
j5e67d8e2022-11-08 22:55:04 -080089 observer.observe(document.documentElement, {
90 childList: true,
91 subtree: true
92 });
j6df32632022-11-10 08:59:16 -080093
j427abfa2023-08-22 13:17:31 -070094 //Try loading now if we can (if we're late and the document already loaded)
95 if (document.head) injectDTPSLoaderHead(document.head);
96 if (document.body) injectDTPSLoaderBody(document.body);
97
j6df32632022-11-10 08:59:16 -080098 //Get Power+ base URL
99 var baseURL = "https://powerplus.app";
100 if (useClassicDTPS) {
101 baseURL = "https://classic.powerplus.app";
102 } else if (window.localStorage.externalReleaseURL && (window.localStorage.dtpsLoaderPref == "external")) {
103 baseURL = window.localStorage.externalReleaseURL;
104 } else if (window.localStorage.dtpsLoaderPref == "local") {
105 baseURL = "http://localhost:2750";
106 }
107
108 //Set DTPS loader parameters
109 var s = document.createElement("script");
110 s.textContent = "window.dtpsPreLoader = true;window.dtpsBaseURL = '" + baseURL + "'";
111 s.async = false;
112 s.setAttribute("dtps", "true");
113 document.documentElement.appendChild(s);
114
115 //Load jQuery
116 var s = document.createElement("script");
117 s.src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js";
118 s.async = false;
119 s.setAttribute("dtps", "true");
120 document.documentElement.appendChild(s);
121
j941bd302022-11-11 09:13:50 -0800122 //Wait for page to load
123 window.onload = function () {
124 //Stop observer
125 observer.disconnect();
j6df32632022-11-10 08:59:16 -0800126
j941bd302022-11-11 09:13:50 -0800127 //Hash global user ID for extra privacy
128 window.crypto.subtle.digest("SHA-256", new TextEncoder().encode(guuid)).then(data => {
129 //unique hash
130 let uh = Array.from(new Uint8Array(data)).map((b) => b.toString(16).padStart(2, '0')).join('');
j6df32632022-11-10 08:59:16 -0800131
132 //Determine LMS script to load
133 var lmsScript = null;
134 if (window.location.hostname === "dtechhs.instructure.com") {
135 lmsScript = "dtech";
136 } else {
137 lmsScript = "canvas";
138 }
139
140 //Check for debugging LMS overrides
141 if (window.localStorage.dtpsLMSOverride) lmsScript = window.localStorage.dtpsLMSOverride;
142
143 //Add script to DOM
144 var s = document.createElement("script");
145 s.src = useClassicDTPS ? "https://classic.powerplus.app/init.js" : baseURL + "/scripts/lms/" + lmsScript + ".js?uh=" + uh;
146 s.async = false;
147 s.setAttribute("dtps", "true");
148 document.documentElement.appendChild(s);
149 s.onerror = function () {
150 //Couldn't load debugging script, fallback to production
151 if (window.localStorage.dtpsLoaderPref && (window.localStorage.dtpsLoaderPref !== "prod")) {
j427abfa2023-08-22 13:17:31 -0700152 console.log("[DTPS Chrome]", "Failed to load development script... Falling back to production...");
j6df32632022-11-10 08:59:16 -0800153 window.localStorage.dtpsLoaderPref = "prod";
154 window.location.reload();
155 } else {
156 document.getElementById("dtpsLoadingScreenBar").style.animationPlayState = "paused";
157 document.getElementById("dtpsLoadingScreenStatus").innerText = "Could not load Power+. Please try again later.";
158 }
159 };
j941bd302022-11-11 09:13:50 -0800160 });
161 }
j6df32632022-11-10 08:59:16 -0800162 } else if (window.location.search.includes("dtpsLogin=true") && knownCanvasInstance) {
163 //redirect to Power+ after login
j427abfa2023-08-22 13:17:31 -0700164 console.log("[DTPS Chrome]", "Redirecting after login...");
j6df32632022-11-10 08:59:16 -0800165 window.location.href = "/power+/";
166 } else {
j427abfa2023-08-22 13:17:31 -0700167 let dtpsWasInjected = false;
j6df32632022-11-10 08:59:16 -0800168 function injectDTPSButton(node) {
j427abfa2023-08-22 13:17:31 -0700169 if (dtpsWasInjected) return;
170 dtpsWasInjected = true;
j81c9b692023-09-12 21:24:38 -0700171
172 console.log("[DTPS Chrome]", "Initializing the DTPS button...");
173
174 let releaseType = null;
175 const loaderPref = window.localStorage.getItem("dtpsLoaderPref");
176 if (loaderPref === "external") releaseType = "Power+ (external)";
177 if (loaderPref === "local") releaseType = "Power+ (local)";
178
j6df32632022-11-10 08:59:16 -0800179 node.insertAdjacentHTML("beforeend", /*html*/`
j427abfa2023-08-22 13:17:31 -0700180 <li class="menu-item ic-app-header__menu-list-item">
181 <a onclick="" id="global_nav_dtps_link" role="button" href="/power+/" class="ic-app-header__menu-list-link" referrerpolicy="no-referrer">
j6df32632022-11-10 08:59:16 -0800182 <div class="menu-item-icon-container" aria-hidden="true">
j469ee102023-02-27 17:15:52 -0800183 <svg class="ic-icon-svg ic-icon-svg--dtps" version="1.1" viewBox="0 0 23.122 29" xmlns="http://www.w3.org/2000/svg">
184 <path d="m0 2c0-1.1046 0.89543-2 2-2h18.5c1.3807 0 2.5 1.1193 2.5 2.5v23.5h-20.5c-0.27614 0-0.5 0.2239-0.5 0.5s0.22386 0.5 0.5 0.5h20.622l-0.2356 0.6666c-0.2744 0.7761-1.0145 1.3334-1.8861 1.3334h-19c-1.1046 0-2-0.8954-2-2zm20.645 22 1.3553-1.3553v-3.5858l-4.9411 4.9411zm-16.645-21.941v3.5858l4.6447-4.6447h-3.5858zm6.0589-1.0589-6.0589 6.0589v3.5858l9.6447-9.6447zm5 0-11.059 11.059v3.5858l14.645-14.645zm5 0-16.059 16.059v3.5858l18-18v-0.1447c0-0.82843-0.6716-1.5-1.5-1.5zm1.9411 3.0589-18 18v1.9411h1.6447l16.355-16.355zm0 5-14.941 14.941h3.5858l11.355-11.355zm0 5-9.9411 9.9411h3.5858l6.3553-6.3553z" />
185 </svg>
j6df32632022-11-10 08:59:16 -0800186 </div>
187 <div class="menu-item__text">${releaseType || "Power+"}</div>
188 </a>
189 </li>
190 `);
191 }
192
j427abfa2023-08-22 13:17:31 -0700193 function startDTPSButtonInjector() {
j6df32632022-11-10 08:59:16 -0800194 //Inject button as the page loads in
195 const observer = new MutationObserver(mutations => {
196 mutations.forEach(({ addedNodes }) => {
197 addedNodes.forEach(node => {
198 //Power+ button
199 if (node.nodeType === 1 && node.id === "menu") {
200 injectDTPSButton(node);
201 }
202 })
203 })
204 });
205
206 // Starts the monitoring
207 observer.observe(document.documentElement, {
208 childList: true,
209 subtree: true
210 });
j427abfa2023-08-22 13:17:31 -0700211
212 // Or it may have already been loaded
213 if (document.getElementById("menu")) injectDTPSButton(document.getElementById("menu"));
214 }
215
216 if (knownCanvasInstance) {
217 startDTPSButtonInjector();
j6df32632022-11-10 08:59:16 -0800218 } else {
j427abfa2023-08-22 13:17:31 -0700219 verifyCanvasInstance(startDTPSButtonInjector);
j6df32632022-11-10 08:59:16 -0800220 }
j5e67d8e2022-11-08 22:55:04 -0800221 }
j941bd302022-11-11 09:13:50 -0800222 } catch (e) { }
j5e67d8e2022-11-08 22:55:04 -0800223})();