8
8
// - Simplification of certain functions like updateVars
9
9
// - Fixes/adaptations for use in Atlas documentation
10
10
11
- const msApiUrl = "https://www.microsoft.com/software-download-connector/api/"
12
- const parms = "?profile=606624d44113&Locale=en-US&sessionID="
13
- const sessionUrl = "https://vlscppe.microsoft.com/fp/tags?org_id=y6jn8c31&session_id="
14
- const apiUrl = "https://api.gravesoft.dev/msdl/"
15
-
16
- const sharedSessionGUID = "47cbc254-4a79-4be6-9866-9c625eb20911" ;
17
- const langAttempt = 3 ;
11
+ const apiUrl = "https://api.gravesoft.dev/msdl/" ;
18
12
19
13
let sessionId ;
20
14
let msContent ;
21
15
let pleaseWait ;
22
16
let processingError ;
23
17
let download ;
24
18
let downloadLink ;
25
- let getLangFailCount ;
26
- let shouldUseSharedSession = true ;
27
19
let winProductID ;
28
20
let skuId ;
29
21
30
22
function uuidv4 ( ) {
31
- return ( [ 1e7 ] + - 1e3 + - 4e3 + - 8e3 + - 1e11 ) . replace ( / [ 0 1 8 ] / g, ( c ) => ( c ^ ( crypto . getRandomValues ( new Uint8Array ( 1 ) ) [ 0 ] & ( 15 >> ( c / 4 ) ) ) ) . toString ( 16 ) ) ;
23
+ return ( [ 1e7 ] + - 1e3 + - 4e3 + - 8e3 + - 1e11 ) . replace ( / [ 0 1 8 ] / g, ( c ) =>
24
+ ( c ^ crypto . getRandomValues ( new Uint8Array ( 1 ) ) [ 0 ] & ( 15 >> ( c / 4 ) ) ) . toString ( 16 )
25
+ ) ;
32
26
}
33
27
34
28
function updateVars ( ) {
29
+ // With auto-selection, an option is always selected.
35
30
let id = document . getElementById ( 'product-languages' ) . value ;
36
- if ( id == "" ) {
37
- document . getElementById ( 'submit-sku' ) . disabled = 1 ;
38
- return ;
39
- }
40
-
41
- document . getElementById ( 'submit-sku' ) . disabled = 0 ;
31
+ document . getElementById ( 'submit-sku' ) . disabled = false ;
42
32
skuId = JSON . parse ( id ) [ 'id' ] ;
43
33
console . log ( `Updated skuId: ${ skuId } ` ) ;
44
34
return skuId ;
@@ -58,17 +48,12 @@ function getFileNameFromLink(link) {
58
48
}
59
49
60
50
function getLanguages ( productId ) {
61
- let url = `${ msApiUrl } getskuinformationbyproductedition ${ parms } ${ sessionId . value } &ProductEditionId =${ productId } ` ;
51
+ let url = `${ apiUrl } skuinfo?product_id =${ productId } ` ;
62
52
let xhr = new XMLHttpRequest ( ) ;
63
53
xhr . onload = onLanguageXhrChange ;
64
54
xhr . onerror = function ( ) {
65
- getLangFailCount ++ ;
66
- if ( getLangFailCount > langAttempt ) {
67
- showError ( "Failed to retrieve languages." ) ;
68
- } else {
69
- getLanguages ( productId ) ;
70
- }
71
- } ;
55
+ showError ( "Failed to retrieve languages." ) ;
56
+ } ;
72
57
xhr . open ( "GET" , url , true ) ;
73
58
xhr . send ( ) ;
74
59
}
@@ -84,11 +69,8 @@ function onLanguageXhrChange() {
84
69
let langHtml = langJsonStrToHTML ( this . responseText ) ;
85
70
msContent . innerHTML = langHtml ;
86
71
87
- let submitSku = document . getElementById ( 'submit-sku' ) ;
88
- submitSku . setAttribute ( "onClick" , "getDownload();" ) ;
89
-
90
72
let prodLang = document . getElementById ( 'product-languages' ) ;
91
- prodLang . setAttribute ( "onChange ", " updateVars();" ) ;
73
+ prodLang . addEventListener ( "change ", updateVars ) ;
92
74
93
75
updateVars ( ) ;
94
76
}
@@ -110,7 +92,7 @@ function langJsonStrToHTML(jsonStr) {
110
92
111
93
let defaultOption = document . createElement ( 'option' ) ;
112
94
defaultOption . value = "" ;
113
- defaultOption . selected = "selected" ;
95
+ defaultOption . selected = true ;
114
96
defaultOption . textContent = "Choose one" ;
115
97
select . appendChild ( defaultOption ) ;
116
98
@@ -125,130 +107,63 @@ function langJsonStrToHTML(jsonStr) {
125
107
126
108
let button = document . createElement ( 'button' ) ;
127
109
button . id = "submit-sku" ;
128
- button . textContent = "Submit " ;
110
+ button . textContent = "Download " ;
129
111
button . disabled = true ;
130
112
button . setAttribute ( "onClick" , "getDownload();" ) ;
131
-
132
113
container . appendChild ( button ) ;
133
114
134
115
return container . innerHTML ;
135
116
}
136
117
137
- function getDownload ( sharedSession = false ) {
118
+ function getDownload ( ) {
138
119
msContent . style . display = "none" ;
139
120
pleaseWait . style . display = "block" ;
140
121
skuId = skuId ? skuId : updateVars ( ) ;
141
- let sessionGUID = sharedSession ? sharedSessionGUID : sessionId . value ;
142
- let url = `${ msApiUrl } GetProductDownloadLinksBySku${ parms } ${ sessionGUID } &SKU=${ skuId } ` ;
122
+ let url = `${ apiUrl } proxy?product_id=${ winProductID } &sku_id=${ skuId } ` ;
143
123
console . log ( `Requesting download links from URL: ${ url } ` ) ;
144
124
let xhr = new XMLHttpRequest ( ) ;
145
- xhr . onload = function ( ) {
146
- onDownloadsXhrChange ( sharedSession ) ;
147
- } ;
125
+ xhr . onload = onDownloadsXhrChange ;
148
126
xhr . open ( "GET" , url , true ) ;
149
127
xhr . send ( ) ;
150
128
}
151
129
152
- function onDownloadsXhrChange ( sharedSession ) {
130
+ function onDownloadsXhrChange ( ) {
153
131
if ( this . status != 200 ) {
154
- if ( ! sharedSession ) {
155
- console . log ( "Request failed, retrying with shared session..." ) ;
156
- getDownload ( true ) ;
157
- } else {
158
- console . log ( "Request failed, using server proxy..." ) ;
159
- getFromServer ( ) ;
160
- }
132
+ showError ( "Failed to retrieve download links." ) ;
161
133
return ;
162
134
}
163
-
164
135
let response = JSON . parse ( this . responseText ) ;
165
136
console . log ( 'Download response:' , response ) ;
166
137
167
- if ( response . Errors || response . ValidationContainer . Errors ) {
168
- if ( ! sharedSession ) {
169
- console . log ( "Request failed, retrying with shared session..." ) ;
170
- getDownload ( true ) ;
171
- } else {
172
- console . log ( "Request failed, using server proxy..." ) ;
173
- getFromServer ( ) ;
174
- }
175
- return ;
176
- }
177
-
178
138
pleaseWait . style . display = "none" ;
179
139
msContent . innerHTML = "" ;
180
140
msContent . style . display = "block" ;
181
141
182
- response . ProductDownloadOptions . forEach ( option => {
183
- let optionContainer = document . createElement ( 'div' ) ;
184
- let header = document . createElement ( 'h1' ) ;
185
- header . textContent = `Windows 11 ${ option . LocalizedLanguage } ` ;
186
- let downloadButton = document . createElement ( 'a' ) ;
187
- downloadButton . href = option . Uri ;
188
- downloadButton . textContent = getFileNameFromLink ( option . Uri ) ;
189
- downloadButton . target = "_blank" ;
190
- optionContainer . appendChild ( downloadButton ) ;
191
- msContent . appendChild ( optionContainer ) ;
192
-
193
- // Trigger automatic download
194
- const link = document . createElement ( 'a' ) ;
195
- link . href = option . Uri ;
196
- link . download = getFileNameFromLink ( option . Uri ) ;
197
- document . body . appendChild ( link ) ;
198
- link . click ( ) ;
199
- document . body . removeChild ( link ) ;
200
- } ) ;
201
- }
202
-
203
- function getFromServer ( ) {
204
- console . log ( "Using server proxy to get the download link." ) ;
205
- let url = `${ apiUrl } proxy?product_id=${ winProductID } &sku_id=${ skuId } ` ;
206
- let xhr = new XMLHttpRequest ( ) ;
207
- xhr . onload = displayResponseFromServer ;
208
- xhr . open ( "GET" , url , true ) ;
209
- xhr . send ( ) ;
210
- }
211
-
212
- function displayResponseFromServer ( ) {
213
- if ( this . status != 200 ) {
214
- showError ( "Failed to retrieve download links from server." ) ;
215
- return ;
216
- }
217
-
218
- const response = JSON . parse ( this . responseText ) ;
219
- console . log ( 'Server proxy response:' , response ) ;
220
-
221
- // Check if ProductDownloadOptions array is present and has elements
222
- if ( ! response . ProductDownloadOptions || response . ProductDownloadOptions . length === 0 ) {
223
- showError ( "No download options available from server." ) ;
224
- return ;
225
- }
226
-
227
- pleaseWait . style . display = "none" ;
228
- msContent . innerHTML = "" ;
229
- msContent . style . display = "block" ;
230
-
231
- response . ProductDownloadOptions . forEach ( option => {
232
- if ( option . Uri && option . Uri . includes ( '_x64' ) ) {
233
- let optionContainer = document . createElement ( 'div' ) ;
234
- let header = document . createElement ( 'h1' ) ;
235
- header . textContent = `Windows 11 ${ option . LocalizedLanguage } ` ;
236
- let downloadButton = document . createElement ( 'a' ) ;
237
- downloadButton . href = option . Uri ;
238
- downloadButton . textContent = getFileNameFromLink ( option . Uri ) ;
239
- downloadButton . target = "_blank" ;
240
- optionContainer . appendChild ( downloadButton ) ;
241
- msContent . appendChild ( optionContainer ) ;
242
-
243
- // Trigger automatic download
244
- const link = document . createElement ( 'a' ) ;
245
- link . href = option . Uri ;
246
- link . download = getFileNameFromLink ( option . Uri ) ;
247
- document . body . appendChild ( link ) ;
248
- link . click ( ) ;
249
- document . body . removeChild ( link ) ;
250
- }
251
- } ) ;
142
+ if ( response . ProductDownloadOptions && response . ProductDownloadOptions . length > 0 ) {
143
+ let header = document . createElement ( 'h2' ) ;
144
+ header . textContent = `${ response . ProductDownloadOptions [ 0 ] . ProductDisplayName } ${ response . ProductDownloadOptions [ 0 ] . LocalizedLanguage } ` ;
145
+ msContent . appendChild ( header ) ;
146
+
147
+ response . ProductDownloadOptions . forEach ( option => {
148
+ let optionContainer = document . createElement ( 'div' ) ;
149
+ let downloadButton = document . createElement ( 'a' ) ;
150
+ downloadButton . href = option . Uri ;
151
+ downloadButton . textContent = getFileNameFromLink ( option . Uri ) ;
152
+ downloadButton . target = "_blank" ;
153
+ optionContainer . appendChild ( downloadButton ) ;
154
+ msContent . appendChild ( optionContainer ) ;
155
+
156
+ // Trigger automatic download
157
+ const link = document . createElement ( 'a' ) ;
158
+ link . href = option . Uri ;
159
+ link . download = getFileNameFromLink ( option . Uri ) ;
160
+ document . body . appendChild ( link ) ;
161
+ link . click ( ) ;
162
+ document . body . removeChild ( link ) ;
163
+ } ) ;
164
+ } else {
165
+ msContent . innerHTML = "<p>No download options available.</p>" ;
166
+ }
252
167
}
253
168
254
169
function getWindows ( id ) {
@@ -260,23 +175,8 @@ function getWindows(id) {
260
175
download = document . getElementById ( "msdl-download" ) ;
261
176
downloadLink = document . getElementById ( "msdl-download-link" ) ;
262
177
263
- // Misc variables
264
- getLangFailCount = 0 ;
265
-
266
- // Start new session
178
+ skuId = null ;
267
179
sessionId . value = uuidv4 ( ) ;
268
- const xhr = new XMLHttpRequest ( ) ;
269
- xhr . open ( "GET" , sessionUrl + sessionId . value , true ) ;
270
- xhr . send ( ) ;
271
-
272
- let mxhr = new XMLHttpRequest ( ) ;
273
- mxhr . onload = function ( ) {
274
- if ( this . status != 200 ) shouldUseSharedSession = false ;
275
- } ;
276
- mxhr . open ( "GET" , apiUrl + "use_shared_session" , true ) ;
277
- mxhr . send ( ) ;
278
-
279
- // Display the 'Please wait' text
280
180
msContent . style . display = "none" ;
281
181
processingError . style . display = "none" ;
282
182
download . style . display = "none" ;
0 commit comments