| 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 | |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 38 | let dtpsHeadLoaded = false; |
| 39 | function injectDTPSLoaderHead(node) { |
| 40 | if (dtpsHeadLoaded) return; |
| 41 | dtpsHeadLoaded = true; |
| 42 | node.insertAdjacentHTML("afterbegin", /*html*/` |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 43 | <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 | `); |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 50 | } |
| 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;"> |
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 58 | <img dtps="true" style="height: 90px; margin-top: 132px;" src="${light ? "https://i.imgur.com/bZHcHoE.png" : "https://i.imgur.com/NrjqsVI.png"}" /> |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 59 | <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); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 74 | } 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] | 75 | guuid = node.textContent.match(/"current_user_global_id":"(.*?)"/)[1]; |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 76 | node.remove(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 77 | } else if (node.nodeType === 1 && node.getAttribute("dtps") != "true") { |
| 78 | //Node is not added by dtps loader |
| 79 | node.remove(); |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 80 | } |
| 81 | }) |
| 82 | }) |
| 83 | }); |
| 84 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 85 | //Start MutationObserver |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 86 | observer.observe(document.documentElement, { |
| 87 | childList: true, |
| 88 | subtree: true |
| 89 | }); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 90 | |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 91 | //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 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 95 | //Get Power+ base URL |
| 96 | var baseURL = "https://powerplus.app"; |
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 97 | if (window.localStorage.dtpsLoaderPref == "local") { |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 98 | 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"); |
| j | 6ca9c7e | 2026-10-13 20:39:55 +0000 | [diff] [blame^] | 110 | s.src = "https://cdn.jottocraft.com/3rdparty/scripts/jquery.min.js"; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 111 | s.async = false; |
| 112 | s.setAttribute("dtps", "true"); |
| 113 | document.documentElement.appendChild(s); |
| 114 | |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 115 | //Wait for page to load |
| 116 | window.onload = function () { |
| 117 | //Stop observer |
| 118 | observer.disconnect(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 119 | |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 120 | //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(''); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 124 | |
| 125 | //Determine LMS script to load |
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 126 | var lmsScript = "canvas"; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 127 | |
| 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"); |
| j | 0f3d7db | 2026-10-13 04:56:55 +0000 | [diff] [blame] | 133 | s.src = baseURL + "/scripts/lms/" + lmsScript + ".js?uh=" + uh; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 134 | 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")) { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 140 | console.log("[DTPS Chrome]", "Failed to load development script... Falling back to production..."); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 141 | 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 | }; |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 148 | }); |
| 149 | } |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 150 | } else if (window.location.search.includes("dtpsLogin=true") && knownCanvasInstance) { |
| 151 | //redirect to Power+ after login |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 152 | console.log("[DTPS Chrome]", "Redirecting after login..."); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 153 | window.location.href = "/power+/"; |
| 154 | } else { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 155 | let dtpsWasInjected = false; |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 156 | function injectDTPSButton(node) { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 157 | if (dtpsWasInjected) return; |
| 158 | dtpsWasInjected = true; |
| j | 81c9b69 | 2023-09-12 21:24:38 -0700 | [diff] [blame] | 159 | |
| 160 | console.log("[DTPS Chrome]", "Initializing the DTPS button..."); |
| 161 | |
| 162 | let releaseType = null; |
| 163 | const loaderPref = window.localStorage.getItem("dtpsLoaderPref"); |
| j | 81c9b69 | 2023-09-12 21:24:38 -0700 | [diff] [blame] | 164 | if (loaderPref === "local") releaseType = "Power+ (local)"; |
| 165 | |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 166 | node.insertAdjacentHTML("beforeend", /*html*/` |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 167 | <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"> |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 169 | <div class="menu-item-icon-container" aria-hidden="true"> |
| j | 469ee10 | 2023-02-27 17:15:52 -0800 | [diff] [blame] | 170 | <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> |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 173 | </div> |
| 174 | <div class="menu-item__text">${releaseType || "Power+"}</div> |
| 175 | </a> |
| 176 | </li> |
| 177 | `); |
| 178 | } |
| 179 | |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 180 | function startDTPSButtonInjector() { |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 181 | //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 | }); |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 198 | |
| 199 | // Or it may have already been loaded |
| 200 | if (document.getElementById("menu")) injectDTPSButton(document.getElementById("menu")); |
| 201 | } |
| 202 | |
| 203 | if (knownCanvasInstance) { |
| 204 | startDTPSButtonInjector(); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 205 | } else { |
| j | 427abfa | 2023-08-22 13:17:31 -0700 | [diff] [blame] | 206 | verifyCanvasInstance(startDTPSButtonInjector); |
| j | 6df3263 | 2022-11-10 08:59:16 -0800 | [diff] [blame] | 207 | } |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 208 | } |
| j | 941bd30 | 2022-11-11 09:13:50 -0800 | [diff] [blame] | 209 | } catch (e) { } |
| j | 5e67d8e | 2022-11-08 22:55:04 -0800 | [diff] [blame] | 210 | })(); |