-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscript.js
More file actions
226 lines (198 loc) · 5.99 KB
/
script.js
File metadata and controls
226 lines (198 loc) · 5.99 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// ---- GLOBAL STATE -------------------------
let pageObserver = null;
const addLearnMore = (
targetId,
href,
linkText,
learnMoreId,
direction = "after",
) => {
const bodyTarget = document.getElementById(targetId);
const learnMoreTarget = document.getElementById(learnMoreId);
if (
!learnMoreTarget &&
bodyTarget &&
bodyTarget.nextSibling &&
bodyTarget.nextSibling.children &&
bodyTarget.nextSibling.children[0]
) {
const descriptionEl = bodyTarget.nextSibling.children[0];
const a = document.createElement("a");
if (direction === "before") {
descriptionEl.before(a);
} else {
descriptionEl.after(a);
}
a.id = learnMoreId;
a.href = href;
a.target = "_blank";
a.textContent = linkText;
a.classList.add("prose-sm");
}
};
function waitForElementId(elementId, text, callback) {
const check = () => {
const element = document.querySelector(elementId);
return element && text
? element && element.textContent.includes(text)
: element;
};
// If it's already there, run immediately
if (check()) {
callback(check());
return;
}
// Otherwise observe DOM mutations
const observer = new MutationObserver(() => {
const el = check();
if (el) {
observer.disconnect();
callback(el);
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
});
}
// Safely reruns logic on DOM change
function startPageObserver(onDomChange) {
// Kill old observer (if any)
if (pageObserver) {
pageObserver.disconnect();
}
pageObserver = new MutationObserver(() => {
onDomChange();
});
pageObserver.observe(document.body, {
subtree: true,
childList: true,
});
// Run once immediately
onDomChange();
}
// ---- PAGE LOGIC ---------------------------
function enhanceGetQuotePage() {
waitForElementId("#body-trade-type", undefined, () => {
addLearnMore(
"body-trade-type",
"/references/api/api_core_concepts/trade-types",
"Learn more about trade types",
"learn-more-trade-type",
);
addLearnMore(
"body-app-fees",
"/features/app-fees",
"Learn more about app fees",
"learn-more-app-fees",
);
addLearnMore(
"body-topup-gas",
"/features/gas-top-up",
"Learn more about topping up gas",
"learn-more-topup-gas",
);
addLearnMore(
"response-fees",
"/references/api/api_core_concepts/fees",
"Learn more about fees",
"learn-more-fees",
"before",
);
});
}
function enhanceGetChainsPage() {
waitForElementId("#response-chains-token-support", undefined, () => {
addLearnMore(
"response-chains-token-support",
"/references/api/api_resources/supported-routes#step-1:-check-token-support-level",
"Learn more about token support",
"learn-more-token-support",
"before",
);
});
}
function enhanceFeeSponsorshipPage() {
waitForElementId("#page-title", "Fee Sponsorship", () => {
if (!document.getElementById("enterprise-link")) {
const enterpriseLink = document.createElement("a");
enterpriseLink.textContent = "Enterprise";
enterpriseLink.href = "/resources/enterprise";
enterpriseLink.id = "enterprise-link";
const pageTitle = document.getElementById("page-title");
pageTitle.appendChild(enterpriseLink);
pageTitle.classList.add("flex", "items-center");
}
});
}
function enhanceSponsoredExecutionPage() {
waitForElementId("#page-title", "Sponsored Execution", () => {
if (!document.getElementById("enterprise-link")) {
const enterpriseLink = document.createElement("a");
enterpriseLink.textContent = "Enterprise";
enterpriseLink.href = "/resources/enterprise";
enterpriseLink.id = "enterprise-link";
const pageTitle = document.getElementById("page-title");
pageTitle.appendChild(enterpriseLink);
pageTitle.classList.add("flex", "items-center");
}
});
}
function enhanceFastFillPage() {
waitForElementId("#page-title", "Fast Fill", () => {
if (!document.getElementById("enterprise-link")) {
const enterpriseLink = document.createElement("a");
enterpriseLink.textContent = "Enterprise";
enterpriseLink.href = "/resources/enterprise";
enterpriseLink.id = "enterprise-link";
const pageTitle = document.getElementById("page-title");
pageTitle.appendChild(enterpriseLink);
pageTitle.classList.add("flex", "items-center");
}
});
}
// ---- MAIN NAVIGATION HANDLER --------------
function onPageChange() {
const path = window.location.pathname;
// Stop any existing watchers
if (pageObserver) {
pageObserver.disconnect();
pageObserver = null;
}
if (path.includes("/references/api/get-quote-v2")) {
startPageObserver(enhanceGetQuotePage);
} else if (path.includes("/references/api/get-chains")) {
startPageObserver(enhanceGetChainsPage);
} else if (path.includes("/features/fee-sponsorship")) {
startPageObserver(enhanceFeeSponsorshipPage);
} else if (path.includes("/features/sponsored-execution")) {
startPageObserver(enhanceSponsoredExecutionPage);
} else if (path.includes("/features/fast-fill")) {
startPageObserver(enhanceFastFillPage);
}
}
// Run on first page load
onPageChange();
// ---- NAVIGATION PATCHING --------------
(function () {
// Patch pushState
const pushState = history.pushState;
history.pushState = function () {
const ret = pushState.apply(this, arguments);
window.dispatchEvent(new Event("mintlify:navigation"));
return ret;
};
// Patch replaceState
const replaceState = history.replaceState;
history.replaceState = function () {
const ret = replaceState.apply(this, arguments);
window.dispatchEvent(new Event("mintlify:navigation"));
return ret;
};
// Back/forward navigation
window.addEventListener("popstate", () => {
window.dispatchEvent(new Event("mintlify:navigation"));
});
})();
// Listen for any navigation event
window.addEventListener("mintlify:navigation", onPageChange);