-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuser-data.js
More file actions
127 lines (105 loc) · 3.47 KB
/
user-data.js
File metadata and controls
127 lines (105 loc) · 3.47 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
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
/* Expose DeepL client state so docs scripts can personalize account links. */
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}`;
const CTA_SELECTOR = '#topbar-cta-button a';
const CTA_LABEL_SELECTOR = 'span.z-10';
let clientStatePromise;
function getClientState() {
if (!clientStatePromise) {
clientStatePromise = (async () => {
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;
} catch (error) {
console.error('Error fetching user data:', error);
clientStatePromise = null;
}
})();
}
return clientStatePromise;
}
function getApiSubscription(clientState) {
return clientState?.featureSet?.subscription?.api;
}
async function getAccountId() {
const clientState = await getClientState();
return clientState?.loginState?.accountId;
}
function findCtaButton() {
const link = document.querySelector(CTA_SELECTOR);
if (!link) return null;
return {
link,
label: link.querySelector(CTA_LABEL_SELECTOR),
};
}
function setCtaButton(label, href) {
const cta = findCtaButton();
if (!cta) return false;
if (cta.label && cta.label.textContent !== label) {
cta.label.textContent = label;
}
if (cta.link.href !== href) {
cta.link.href = href;
}
return true;
}
function resolveCtaState(clientState) {
if (!clientState?.loginState) return null;
if (getApiSubscription(clientState)) {
return { label: 'Dashboard', href: 'https://www.deepl.com/en/your-account/keys' };
}
// TODO: Update link once self-serve multisub lets users add an API
// subscription to their existing translate account.
// Then we can display separate links for anon vs non-API users.
return { label: 'Free API Key', href: 'https://www.deepl.com/en/checkout?is_api=true' };
}
async function initNavbarCta() {
const clientState = await getClientState();
const cta = resolveCtaState(clientState);
if (!cta) return;
// Mintlify is an SPA: the nav may not be rendered yet.
let attempts = 0;
const poll = setInterval(() => {
if (setCtaButton(cta.label, cta.href) || ++attempts >= 20) {
clearInterval(poll);
}
}, 500);
// Re-apply when Mintlify re-renders the nav during SPA navigation.
let rafPending = false;
new MutationObserver(() => {
if (rafPending) return;
rafPending = true;
requestAnimationFrame(() => {
setCtaButton(cta.label, cta.href);
rafPending = false;
});
}).observe(document.body, { childList: true, subtree: true });
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initNavbarCta);
} else {
initNavbarCta();
}
return { getClientState, getAccountId };
})();
window.getDeepLClientStateNow = function getDeepLClientStateNow() {
return window.DeepLUser?.getClientState?.();
};