|
| 1 | +// AGPL-3.0-only https://spdx.org/licenses/AGPL-3.0-or-later.html |
| 2 | +// Modified from https://github.com/gravesoft/msdl for use in AtlasOS documentation |
| 3 | +// |
| 4 | +// Changes: |
| 5 | +// - Auto-selecting language, and removing 'Choose once' |
| 6 | +// - Removing table to download other versions |
| 7 | +// - Restructuring of script |
| 8 | +// - Simplification of certain functions like updateVars |
| 9 | +// - Fixes/adaptations for use in Atlas documentation |
| 10 | + |
| 11 | +const apiUrl = "https://api.gravesoft.dev/msdl/"; |
| 12 | + |
| 13 | +let sessionId; |
| 14 | +let msContent; |
| 15 | +let pleaseWait; |
| 16 | +let processingError; |
| 17 | +let download; |
| 18 | +let downloadLink; |
| 19 | +let winProductID; |
| 20 | +let skuId; |
| 21 | + |
| 22 | +function uuidv4() { |
| 23 | + return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c) => |
| 24 | + (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4))).toString(16) |
| 25 | + ); |
| 26 | +} |
| 27 | + |
| 28 | +function updateVars() { |
| 29 | + // With auto-selection, an option is always selected. |
| 30 | + let id = document.getElementById('product-languages').value; |
| 31 | + document.getElementById('submit-sku').disabled = false; |
| 32 | + skuId = JSON.parse(id)['id']; |
| 33 | + console.log(`Updated skuId: ${skuId}`); |
| 34 | + return skuId; |
| 35 | +} |
| 36 | + |
| 37 | +function showError(error) { |
| 38 | + processingError.style.display = "block"; |
| 39 | + pleaseWait.style.display = "none"; |
| 40 | + msContent.style.display = "none"; |
| 41 | + processingError.innerHTML = 'Error: ' + error; |
| 42 | + console.error('Error: ' + error); |
| 43 | +} |
| 44 | + |
| 45 | +function getFileNameFromLink(link) { |
| 46 | + let raw_link = link.split('?')[0]; |
| 47 | + return raw_link.split('/').pop(); |
| 48 | +} |
| 49 | + |
| 50 | +function getLanguages(productId) { |
| 51 | + let url = `${apiUrl}skuinfo?product_id=${productId}`; |
| 52 | + let xhr = new XMLHttpRequest(); |
| 53 | + xhr.onload = onLanguageXhrChange; |
| 54 | + xhr.onerror = function() { |
| 55 | + showError("Failed to retrieve languages."); |
| 56 | + }; |
| 57 | + xhr.open("GET", url, true); |
| 58 | + xhr.send(); |
| 59 | +} |
| 60 | + |
| 61 | +function onLanguageXhrChange() { |
| 62 | + if (this.status != 200) { |
| 63 | + showError("Failed to retrieve languages."); |
| 64 | + return; |
| 65 | + } |
| 66 | + pleaseWait.style.display = "none"; |
| 67 | + msContent.style.display = "block"; |
| 68 | + |
| 69 | + let langHtml = langJsonStrToHTML(this.responseText); |
| 70 | + msContent.innerHTML = langHtml; |
| 71 | + |
| 72 | + let prodLang = document.getElementById('product-languages'); |
| 73 | + prodLang.addEventListener("change", updateVars); |
| 74 | + |
| 75 | + updateVars(); |
| 76 | +} |
| 77 | + |
| 78 | +function langJsonStrToHTML(jsonStr) { |
| 79 | + let json = JSON.parse(jsonStr); |
| 80 | + let container = document.createElement('div'); |
| 81 | + |
| 82 | + let header = document.createElement('h2'); |
| 83 | + header.textContent = "Select the product language"; |
| 84 | + container.appendChild(header); |
| 85 | + |
| 86 | + let info = document.createElement('p'); |
| 87 | + info.innerHTML = "You'll need to choose the same language when you install Windows. To see what language you're currently using, go to <strong>Time and language</strong> in PC settings or <strong>Region</strong> in Control Panel."; |
| 88 | + container.appendChild(info); |
| 89 | + |
| 90 | + let select = document.createElement('select'); |
| 91 | + select.id = "product-languages"; |
| 92 | + |
| 93 | + let defaultOption = document.createElement('option'); |
| 94 | + defaultOption.value = ""; |
| 95 | + defaultOption.selected = true; |
| 96 | + defaultOption.textContent = "Choose one"; |
| 97 | + select.appendChild(defaultOption); |
| 98 | + |
| 99 | + json.Skus.forEach(sku => { |
| 100 | + let option = document.createElement('option'); |
| 101 | + option.value = JSON.stringify({ id: sku.Id }); |
| 102 | + option.textContent = sku.LocalizedLanguage; |
| 103 | + select.appendChild(option); |
| 104 | + }); |
| 105 | + |
| 106 | + container.appendChild(select); |
| 107 | + |
| 108 | + let button = document.createElement('button'); |
| 109 | + button.id = "submit-sku"; |
| 110 | + button.textContent = "Download"; |
| 111 | + button.disabled = true; |
| 112 | + button.setAttribute("onClick", "getDownload();"); |
| 113 | + button.classList = "msdl-button"; |
| 114 | + container.appendChild(button); |
| 115 | + |
| 116 | + return container.innerHTML; |
| 117 | +} |
| 118 | + |
| 119 | +function getDownload() { |
| 120 | + msContent.style.display = "none"; |
| 121 | + pleaseWait.style.display = "block"; |
| 122 | + skuId = skuId ? skuId : updateVars(); |
| 123 | + let url = `${apiUrl}proxy?product_id=${winProductID}&sku_id=${skuId}`; |
| 124 | + console.log(`Requesting download links from URL: ${url}`); |
| 125 | + let xhr = new XMLHttpRequest(); |
| 126 | + xhr.onload = onDownloadsXhrChange; |
| 127 | + xhr.open("GET", url, true); |
| 128 | + xhr.send(); |
| 129 | +} |
| 130 | + |
| 131 | +function onDownloadsXhrChange() { |
| 132 | + if (this.status != 200) { |
| 133 | + showError("Failed to retrieve download links."); |
| 134 | + return; |
| 135 | + } |
| 136 | + let response = JSON.parse(this.responseText); |
| 137 | + console.log('Download response:', response); |
| 138 | + |
| 139 | + pleaseWait.style.display = "none"; |
| 140 | + msContent.innerHTML = ""; |
| 141 | + msContent.style.display = "block"; |
| 142 | + |
| 143 | + if (response.ProductDownloadOptions && response.ProductDownloadOptions.length > 0) { |
| 144 | + let header = document.createElement('h2'); |
| 145 | + header.textContent = `${response.ProductDownloadOptions[0].ProductDisplayName} ${response.ProductDownloadOptions[0].LocalizedLanguage}`; |
| 146 | + msContent.appendChild(header); |
| 147 | + |
| 148 | + response.ProductDownloadOptions.forEach(option => { |
| 149 | + let optionContainer = document.createElement('div'); |
| 150 | + let downloadButton = document.createElement('a'); |
| 151 | + downloadButton.href = option.Uri; |
| 152 | + downloadButton.textContent = getFileNameFromLink(option.Uri); |
| 153 | + downloadButton.target = "_blank"; |
| 154 | + optionContainer.appendChild(downloadButton); |
| 155 | + msContent.appendChild(optionContainer); |
| 156 | + |
| 157 | + // Trigger automatic download |
| 158 | + const link = document.createElement('a'); |
| 159 | + link.href = option.Uri; |
| 160 | + link.download = getFileNameFromLink(option.Uri); |
| 161 | + document.body.appendChild(link); |
| 162 | + link.click(); |
| 163 | + document.body.removeChild(link); |
| 164 | + }); |
| 165 | + } else { |
| 166 | + msContent.innerHTML = "<p>No download options available.</p>"; |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +function getWindows(id) { |
| 171 | + // Define variables for contents of page |
| 172 | + sessionId = document.getElementById("msdl-session-id"); |
| 173 | + msContent = document.getElementById("msdl-ms-content"); |
| 174 | + pleaseWait = document.getElementById("msdl-please-wait"); |
| 175 | + processingError = document.getElementById("msdl-processing-error"); |
| 176 | + download = document.getElementById("msdl-download"); |
| 177 | + downloadLink = document.getElementById("msdl-download-link"); |
| 178 | + |
| 179 | + skuId = null; |
| 180 | + sessionId.value = uuidv4(); |
| 181 | + msContent.style.display = "none"; |
| 182 | + processingError.style.display = "none"; |
| 183 | + download.style.display = "none"; |
| 184 | + pleaseWait.style.display = "block"; |
| 185 | + |
| 186 | + // Retrieve languages |
| 187 | + winProductID = id; |
| 188 | + getLanguages(winProductID); |
| 189 | +} |
0 commit comments