Skip to content

Commit 755f5ac

Browse files
committed
replace fetch with old-school XMLHttpRequests
1 parent 010a034 commit 755f5ac

File tree

2 files changed

+40
-20
lines changed

2 files changed

+40
-20
lines changed

client/data.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,13 @@ export class Data {
165165
}
166166

167167
private _refetchDataJson(): void {
168-
fetch(PATH_DATA_JSON)
169-
.then(response => response.json())
170-
.then(data => {
168+
// Using XMLHttpRequest to fetch data.json instead of fetch API
169+
// because while nody-greeter supports fetch, web-greeter does not.
170+
// It would error with "URL scheme 'web-greeter' is not supported"
171+
const req = new XMLHttpRequest();
172+
req.addEventListener('load', () => {
173+
try {
174+
const data: DataJson = JSON.parse(req.responseText);
171175
console.log("Fetched data.json", data);
172176
if ("error" in data) {
173177
console.warn("data.json response contains an error", data);
@@ -176,18 +180,23 @@ export class Data {
176180
}
177181
// Fallback for missing message field in older versions of data.json
178182
if (!("message" in data)) {
179-
data.message = "";
183+
(data as DataJson).message = "";
180184
}
181185
this._dataJson = data;
182186
// Emit data change event to all listeners
183187
for (const listener of this._dataChangeListeners) {
184188
listener(this._dataJson);
185189
}
186-
})
187-
.catch(error => {
188-
if (window.ui) {
189-
window.ui.setDebugInfo(`Error fetching data.json: ${error}`);
190-
}
191-
});
190+
} catch (err) {
191+
window.ui.setDebugInfo(`Failed to parse data.json: ${err}`);
192+
}
193+
});
194+
req.addEventListener('error', (err) => {
195+
if (window.ui) {
196+
window.ui.setDebugInfo(`Error fetching data.json: ${err}`);
197+
}
198+
});
199+
req.open('GET', PATH_DATA_JSON);
200+
req.send();
192201
}
193202
}

client/uis/screens/lockscreen.ts

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,17 +133,28 @@ export class LockScreenUI extends UIScreen {
133133
}
134134

135135
private _getScreenLockedTimestamp(login: string): Promise<Date> {
136+
// Using XMLHttpRequest to fetch data.json instead of fetch API
137+
// because while nody-greeter supports fetch, web-greeter does not.
138+
// It would error with "URL scheme 'web-greeter' is not supported"
136139
return new Promise((resolve, reject) => {
137-
fetch(`${PATH_LOCK_TIMESTAMP_PREFIX}_${login}`)
138-
.then(response => response.text())
139-
.then(text => {
140-
// Get the first word from the text file
141-
const timestamp = text.split(' ')[0];
142-
resolve(new Date(parseInt(timestamp) * 1000));
143-
})
144-
.catch(() => {
145-
reject();
146-
});
140+
const req = new XMLHttpRequest();
141+
req.addEventListener('load', () => {
142+
try {
143+
const timestamp = req.responseText.split(' ')[0];
144+
if (timestamp) {
145+
resolve(new Date(parseInt(timestamp) * 1000));
146+
} else {
147+
reject(new Error("No timestamp found in response"));
148+
}
149+
} catch (err) {
150+
reject(err);
151+
}
152+
});
153+
req.addEventListener('error', (err) => {
154+
reject(err);
155+
});
156+
req.open('GET', `${PATH_LOCK_TIMESTAMP_PREFIX}_${login}`);
157+
req.send();
147158
});
148159
}
149160

0 commit comments

Comments
 (0)