summaryrefslogtreecommitdiff
path: root/_site/msdl/js/msdl.js
blob: 12015f3cba2ae2478e4d19b88afa02b3240a37be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
const langsUrl = "https://www.microsoft.com/en-us/api/controls/contentinclude/html?pageId=cd06bda8-ff9c-4a6e-912a-b92a21f42526&host=www.microsoft.com&segments=software-download%2cwindows11&query=&action=getskuinformationbyproductedition&sdVersion=2";
const downUrl = "https://www.microsoft.com/en-us/api/controls/contentinclude/html?pageId=cfa9e580-a81e-4a4b-a846-7b21bf4e2e5b&host=www.microsoft.com&segments=software-download%2Cwindows11&query=&action=GetProductDownloadLinksBySku&sdVersion=2";

const sessionId = document.getElementById('msdl-session-id');
const msContent = document.getElementById('msdl-ms-content');
const pleaseWait = document.getElementById('msdl-please-wait');
const processingError = document.getElementById('msdl-processing-error');

const productsList = document.getElementById('products-list');
const backToProductsDiv = document.getElementById('back-to-products');

var msdlXhr = new XMLHttpRequest();

function uuidv4() {
    return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
        (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
    );
}

function updateVars() {
    var id = document.getElementById('product-languages').value;
    if(id == "") {
        document.getElementById('submit-sku').disabled = 1;
        return;
    }

    id = JSON.parse(id);
    document.getElementById('submit-sku').disabled = 0;

    return id;
}

function checkForError(content) {
    var errorMessage = document.getElementById('errorModalMessage');

    if(errorMessage) {
        processingError.style.display = "block";
        content.style.display = "none";
        return true;
    }

    return false;
}

function updateContent(content, response) {
    content.innerHTML = response;
    return !checkForError(content);
}

function abortAndHide() {
    msdlXhr.abort();

    msContent.style.display = 'none';
    pleaseWait.style.display = 'none';
    processingError.style.display = 'none';

    window.location.hash = "";
}

function fixSubmitSku() {
    var submitSku = document.getElementById('submit-sku');
    submitSku.setAttribute("onClick", "getDownload();");
}

function fixProdLang() {
    var prodLang = document.getElementById('product-languages');
    prodLang.setAttribute("onChange", "updateVars();");
}

function fixLanguageList() {
    fixSubmitSku();
    fixProdLang();
}

function onLanguageXhrChange() {
    if(!(this.readyState == 4 && this.status == 200))
        return;
    
    if(pleaseWait.style.display != "block")
        return;

    pleaseWait.style.display = "none";
    msContent.style.display = "block";

    if(!updateContent(msContent, this.responseText))
        return;

    fixLanguageList();
    updateVars();
}

function onDownloadsXhrChange() {
    if(!(this.readyState == 4 && this.status == 200))
        return;
    
    if(pleaseWait.style.display != "block")
        return;

    pleaseWait.style.display = "none";
    msContent.style.display = "block";

    if(!updateContent(msContent, this.responseText))
        return;
}

function getLanguages(productId) {
    sessionId.value = uuidv4();

    msContent.style.display = "none";
    pleaseWait.style.display = "block";

    var url = langsUrl + "&productEditionId=" + encodeURIComponent(productId) + 
        "&sessionId=" + encodeURIComponent(sessionId.value);

    msdlXhr.abort();
    msdlXhr.onreadystatechange = onLanguageXhrChange;
    msdlXhr.open("GET", url, true);
    msdlXhr.send();
}

function getDownload() {
    msContent.style.display = "none";
    pleaseWait.style.display = "block";

    var id = updateVars();

    var url = downUrl + "&skuId=" + encodeURIComponent(id['id']) +
        "&language=" + encodeURIComponent(id['language'])+ 
        "&sessionId=" + encodeURIComponent(sessionId.value);

    msdlXhr.abort();
    msdlXhr.onreadystatechange = onDownloadsXhrChange;
    msdlXhr.open("GET", url, true);
    msdlXhr.send();
}

function backToProducts() {
    abortAndHide();

    backToProductsDiv.style.display = 'none';
    productsList.style.display = 'block';
}

function prepareDownload(id) {
    productsList.style.display = 'none';
    backToProductsDiv.style.display = 'block';

    return getLanguages(id);
}

function createTable(data) {
    var table = document.getElementById('products-table-body');
    for(value in data) {
        var a = document.createElement('a')
        a.href = "#" + value;
        a.setAttribute("onClick", "prepareDownload(" + value + ");");
        a.appendChild(document.createTextNode(data[value]))

        var tr = table.insertRow();

        var td = tr.insertCell();
        td.appendChild(a);

        var td2 = tr.insertCell();
        td2.appendChild(document.createTextNode(value))
    }
}

function checkHash() {
    var hash = window.location.hash;
    if(hash.length == 0)
        return

    prepareDownload(hash.substring(1))
}

function preparePage(resp) {
    var data = JSON.parse(resp);

    createTable(data['products']);

    pleaseWait.style.display = 'none';
    productsList.style.display = 'block';

    checkHash();
}

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    if(this.readyState != 4)
        return;

    if(this.status != 200) {
        pleaseWait.style.display = 'none';
        processingError.style.display = 'block';
        return;
    }

    preparePage(this.responseText);
};

xhr.open("GET", 'data/products.json', true);
xhr.send();

pleaseWait.style.display = 'block';