blob: 89d235018209fbe21ab051ed72fccb8cf202f6b5 [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
j427abfa2023-08-22 13:17:31 -070038 let dtpsHeadLoaded = false;
39 function injectDTPSLoaderHead(node) {
40 if (dtpsHeadLoaded) return;
41 dtpsHeadLoaded = true;
42 node.insertAdjacentHTML("afterbegin", /*html*/`
j6df32632022-11-10 08:59:16 -080043 <link dtps="true" rel="shortcut icon" href="https://powerplus.app/favicon.png" type="image/png">
44 <meta dtps="true" name="viewport" content="width=device-width, initial-scale=1">
45 <meta dtps="true" charset="utf-8">
46 <title dtps="true">Power+</title>
47 <meta dtps="true" name="description" content="A better UI for Canvas LMS">
48 <meta dtps="true" name="author" content="jottocraft">
49 `);
j427abfa2023-08-22 13:17:31 -070050 }
51
52 let dtpsBodyLoaded = false;
53 function injectDTPSLoaderBody(node) {
54 if (dtpsBodyLoaded) return;
55 dtpsBodyLoaded = true;
56 node.insertAdjacentHTML("afterbegin", /*html*/`
57 <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;">
j0f3d7db2026-10-13 04:56:55 +000058 <img dtps="true" style="height: 90px; margin-top: 132px;" src="${light ? "https://i.imgur.com/bZHcHoE.png" : "https://i.imgur.com/NrjqsVI.png"}" />
j427abfa2023-08-22 13:17:31 -070059 <br dtps="true" />
60 <div dtps="true" class="progress"><div id="dtpsLoadingScreenBar" dtps="true" class="indeterminate"></div></div>
61 <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>
62 </div>
63 `);
64 }
65
66 const observer = new MutationObserver(mutations => {
67 mutations.forEach(({ addedNodes }) => {
68 addedNodes.forEach(node => {
69 //Power+ preloader
70 if (node.nodeType === 1 && node.tagName === 'BODY') {
71 injectDTPSLoaderBody(node);
72 } else if (node.nodeType === 1 && node.tagName === 'HEAD') {
73 injectDTPSLoaderHead(node);
j6df32632022-11-10 08:59:16 -080074 } else if (node.nodeType === 1 && node.tagName === 'SCRIPT' && (node.textContent && node.textContent.includes('"current_user"'))) {
j6df32632022-11-10 08:59:16 -080075 guuid = node.textContent.match(/"current_user_global_id":"(.*?)"/)[1];
j427abfa2023-08-22 13:17:31 -070076 node.remove();
j6df32632022-11-10 08:59:16 -080077 } else if (node.nodeType === 1 && node.getAttribute("dtps") != "true") {
78 //Node is not added by dtps loader
79 node.remove();
j5e67d8e2022-11-08 22:55:04 -080080 }
81 })
82 })
83 });
84
j6df32632022-11-10 08:59:16 -080085 //Start MutationObserver
j5e67d8e2022-11-08 22:55:04 -080086 observer.observe(document.documentElement, {
87 childList: true,
88 subtree: true
89 });
j6df32632022-11-10 08:59:16 -080090
j427abfa2023-08-22 13:17:31 -070091 //Try loading now if we can (if we're late and the document already loaded)
92 if (document.head) injectDTPSLoaderHead(document.head);
93 if (document.body) injectDTPSLoaderBody(document.body);
94
j6df32632022-11-10 08:59:16 -080095 //Get Power+ base URL
96 var baseURL = "https://powerplus.app";
j0f3d7db2026-10-13 04:56:55 +000097 if (window.localStorage.dtpsLoaderPref == "local") {
j6df32632022-11-10 08:59:16 -080098 baseURL = "http://localhost:2750";
99 }
100
101 //Set DTPS loader parameters
102 var s = document.createElement("script");
103 s.textContent = "window.dtpsPreLoader = true;window.dtpsBaseURL = '" + baseURL + "'";
104 s.async = false;
105 s.setAttribute("dtps", "true");
106 document.documentElement.appendChild(s);
107
108 //Load jQuery
109 var s = document.createElement("script");
j6ca9c7e2026-10-13 20:39:55 +0000110 s.src = "https://cdn.jottocraft.com/3rdparty/scripts/jquery.min.js";
j6df32632022-11-10 08:59:16 -0800111 s.async = false;
112 s.setAttribute("dtps", "true");
113 document.documentElement.appendChild(s);
114
j941bd302022-11-11 09:13:50 -0800115 //Wait for page to load
116 window.onload = function () {
117 //Stop observer
118 observer.disconnect();
j6df32632022-11-10 08:59:16 -0800119
j941bd302022-11-11 09:13:50 -0800120 //Hash global user ID for extra privacy
121 window.crypto.subtle.digest("SHA-256", new TextEncoder().encode(guuid)).then(data => {
122 //unique hash
123 let uh = Array.from(new Uint8Array(data)).map((b) => b.toString(16).padStart(2, '0')).join('');
j6df32632022-11-10 08:59:16 -0800124
125 //Determine LMS script to load
j0f3d7db2026-10-13 04:56:55 +0000126 var lmsScript = "canvas";
j6df32632022-11-10 08:59:16 -0800127
128 //Check for debugging LMS overrides
129 if (window.localStorage.dtpsLMSOverride) lmsScript = window.localStorage.dtpsLMSOverride;
130
131 //Add script to DOM
132 var s = document.createElement("script");
j0f3d7db2026-10-13 04:56:55 +0000133 s.src = baseURL + "/scripts/lms/" + lmsScript + ".js?uh=" + uh;
j6df32632022-11-10 08:59:16 -0800134 s.async = false;
135 s.setAttribute("dtps", "true");
136 document.documentElement.appendChild(s);
137 s.onerror = function () {
138 //Couldn't load debugging script, fallback to production
139 if (window.localStorage.dtpsLoaderPref && (window.localStorage.dtpsLoaderPref !== "prod")) {
j427abfa2023-08-22 13:17:31 -0700140 console.log("[DTPS Chrome]", "Failed to load development script... Falling back to production...");
j6df32632022-11-10 08:59:16 -0800141 window.localStorage.dtpsLoaderPref = "prod";
142 window.location.reload();
143 } else {
144 document.getElementById("dtpsLoadingScreenBar").style.animationPlayState = "paused";
145 document.getElementById("dtpsLoadingScreenStatus").innerText = "Could not load Power+. Please try again later.";
146 }
147 };
j941bd302022-11-11 09:13:50 -0800148 });
149 }
j6df32632022-11-10 08:59:16 -0800150 } else if (window.location.search.includes("dtpsLogin=true") && knownCanvasInstance) {
151 //redirect to Power+ after login
j427abfa2023-08-22 13:17:31 -0700152 console.log("[DTPS Chrome]", "Redirecting after login...");
j6df32632022-11-10 08:59:16 -0800153 window.location.href = "/power+/";
154 } else {
j427abfa2023-08-22 13:17:31 -0700155 let dtpsWasInjected = false;
j6df32632022-11-10 08:59:16 -0800156 function injectDTPSButton(node) {
j427abfa2023-08-22 13:17:31 -0700157 if (dtpsWasInjected) return;
158 dtpsWasInjected = true;
j81c9b692023-09-12 21:24:38 -0700159
160 console.log("[DTPS Chrome]", "Initializing the DTPS button...");
161
162 let releaseType = null;
163 const loaderPref = window.localStorage.getItem("dtpsLoaderPref");
j81c9b692023-09-12 21:24:38 -0700164 if (loaderPref === "local") releaseType = "Power+ (local)";
165
j6df32632022-11-10 08:59:16 -0800166 node.insertAdjacentHTML("beforeend", /*html*/`
j427abfa2023-08-22 13:17:31 -0700167 <li class="menu-item ic-app-header__menu-list-item">
168 <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 -0800169 <div class="menu-item-icon-container" aria-hidden="true">
j469ee102023-02-27 17:15:52 -0800170 <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">
171 <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" />
172 </svg>
j6df32632022-11-10 08:59:16 -0800173 </div>
174 <div class="menu-item__text">${releaseType || "Power+"}</div>
175 </a>
176 </li>
177 `);
178 }
179
j427abfa2023-08-22 13:17:31 -0700180 function startDTPSButtonInjector() {
j6df32632022-11-10 08:59:16 -0800181 //Inject button as the page loads in
182 const observer = new MutationObserver(mutations => {
183 mutations.forEach(({ addedNodes }) => {
184 addedNodes.forEach(node => {
185 //Power+ button
186 if (node.nodeType === 1 && node.id === "menu") {
187 injectDTPSButton(node);
188 }
189 })
190 })
191 });
192
193 // Starts the monitoring
194 observer.observe(document.documentElement, {
195 childList: true,
196 subtree: true
197 });
j427abfa2023-08-22 13:17:31 -0700198
199 // Or it may have already been loaded
200 if (document.getElementById("menu")) injectDTPSButton(document.getElementById("menu"));
201 }
202
203 if (knownCanvasInstance) {
204 startDTPSButtonInjector();
j6df32632022-11-10 08:59:16 -0800205 } else {
j427abfa2023-08-22 13:17:31 -0700206 verifyCanvasInstance(startDTPSButtonInjector);
j6df32632022-11-10 08:59:16 -0800207 }
j5e67d8e2022-11-08 22:55:04 -0800208 }
j941bd302022-11-11 09:13:50 -0800209 } catch (e) { }
j5e67d8e2022-11-08 22:55:04 -0800210})();