| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 1 | (function () { |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 2 | try { |
| 3 | function verifyCanvasInstance(cb) { |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 4 | 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... |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 7 | if (window.ENV?.current_user_global_id) { |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 8 | //Remember that this is Canvas to speed up future page loads |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 9 | window.localStorage.setItem("dtpsThinksThisIsCanvas", "true"); |
| 10 | cb(); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 11 | } |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 12 | }); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 13 | } |
| 14 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 15 | //Is this a Canvas instance? (always YES for instructure.com subdomains) |
| 16 | const knownCanvasInstance = ( |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 17 | window.location.hostname.endsWith("instructure.com") |
| 18 | || window.localStorage.getItem("dtpsThinksThisIsCanvas") === "true" |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 19 | ); |
| 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 |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 36 | let guuid = window.ENV && window.ENV.current_user_global_id; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 37 | |
| 38 | const useClassicDTPS = window.location.search && window.location.search.includes("classicEdition=true"); |
| j | 469ee10 | 2023-02-27 17:15:52 -0800 | [diff] [blame] | 39 | if (useClassicDTPS) light = false; |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 40 | |
| 41 | let dtpsHeadLoaded = false; |
| 42 | function injectDTPSLoaderHead(node) { |
| 43 | if (dtpsHeadLoaded) return; |
| 44 | dtpsHeadLoaded = true; |
| 45 | node.insertAdjacentHTML("afterbegin", /*html*/` |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 46 | <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 | `); |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 53 | } |
| 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); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 77 | } else if (node.nodeType === 1 && node.tagName === 'SCRIPT' && (node.textContent && node.textContent.includes('"current_user"'))) { |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 78 | guuid = node.textContent.match(/"current_user_global_id":"(.*?)"/)[1]; |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 79 | node.remove(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 80 | } else if (node.nodeType === 1 && node.getAttribute("dtps") != "true") { |
| 81 | //Node is not added by dtps loader |
| 82 | node.remove(); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 83 | } |
| 84 | }) |
| 85 | }) |
| 86 | }); |
| 87 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 88 | //Start MutationObserver |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 89 | observer.observe(document.documentElement, { |
| 90 | childList: true, |
| 91 | subtree: true |
| 92 | }); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 93 | |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 94 | //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 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 98 | //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 | |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 122 | //Wait for page to load |
| 123 | window.onload = function () { |
| 124 | //Stop observer |
| 125 | observer.disconnect(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 126 | |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 127 | //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(''); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 131 | |
| 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")) { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 152 | console.log("[DTPS Chrome]", "Failed to load development script... Falling back to production..."); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 153 | 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 | }; |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 160 | }); |
| 161 | } |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 162 | } else if (window.location.search.includes("dtpsLogin=true") && knownCanvasInstance) { |
| 163 | //redirect to Power+ after login |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 164 | console.log("[DTPS Chrome]", "Redirecting after login..."); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 165 | window.location.href = "/power+/"; |
| 166 | } else { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 167 | let dtpsWasInjected = false; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 168 | function injectDTPSButton(node) { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 169 | if (dtpsWasInjected) return; |
| 170 | dtpsWasInjected = true; |
| j | 81c9b69 | 2023-09-12 21:24:38 -0700 | [diff] [blame] | 171 | |
| 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 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 179 | node.insertAdjacentHTML("beforeend", /*html*/` |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 180 | <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"> |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 182 | <div class="menu-item-icon-container" aria-hidden="true"> |
| j | 469ee10 | 2023-02-27 17:15:52 -0800 | [diff] [blame] | 183 | <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> |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 186 | </div> |
| 187 | <div class="menu-item__text">${releaseType || "Power+"}</div> |
| 188 | </a> |
| 189 | </li> |
| 190 | `); |
| 191 | } |
| 192 | |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 193 | function startDTPSButtonInjector() { |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 194 | //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 | }); |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 211 | |
| 212 | // Or it may have already been loaded |
| 213 | if (document.getElementById("menu")) injectDTPSButton(document.getElementById("menu")); |
| 214 | } |
| 215 | |
| 216 | if (knownCanvasInstance) { |
| 217 | startDTPSButtonInjector(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 218 | } else { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 219 | verifyCanvasInstance(startDTPSButtonInjector); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 220 | } |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 221 | } |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 222 | } catch (e) { } |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 223 | })(); |