-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuser-data.js
More file actions
41 lines (36 loc) · 1.24 KB
/
user-data.js
File metadata and controls
41 lines (36 loc) · 1.24 KB
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
/* Encapsulate user-data helpers in a single namespace to avoid polluting
* the global scope. Other scripts can call DeepLUser.getApiSubscription().
*/
window.DeepLUser = (() => {
const API_BASE_URL = 'https://w.deepl.com/web';
const API_PARAMS = 'request_type=jsonrpc&il=en&method=getClientState';
const apiUrl = `${API_BASE_URL}?${API_PARAMS}`;
/* Following the standards of deepl.com, we place the request parameters in both the URL and the body,
* although this may not be necessary.
* TODO: Complete error handling, for cases when the user is not logged in, there is no user, etc.
*/
async function getApiSubscription() {
try {
const response = await fetch(
apiUrl,
{
method: "POST",
headers: {
"Accept": "application/json",
"Content-Type": "application/json"
},
body: JSON.stringify({
jsonrpc: "2.0",
method: "getClientState"
}),
credentials: 'include'
}
);
const json = await response.json();
return json?.result?.featureSet?.subscription?.api;
} catch (error) {
console.error('Error fetching user data:', error);
}
}
return { getApiSubscription };
})();