Skip to content

Commit 901e63c

Browse files
committed
fix: injector polling
1 parent ea403d2 commit 901e63c

1 file changed

Lines changed: 27 additions & 10 deletions

File tree

packages/snippets/src/inject.tsx

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,41 @@ export const inject = () => {
1313
<App />
1414
</StrictMode>
1515
);
16+
console.log("snippets injected");
1617

1718
injectButtons();
1819
};
1920

2021
export const injectButtons = () => {
2122
try {
22-
const buttons = document.querySelectorAll(".api-main-buttom.w-button");
23-
buttons.forEach((b) => {
24-
const url = new URL(b.getAttribute("href")!);
25-
const model = window.location.pathname.match(/\/models\/(.+?)\/?/)?.[1];
26-
if (model) {
27-
url.searchParams.set("model", model);
23+
let attempts = 0;
24+
const intervalId = setInterval(() => {
25+
const buttons = document.querySelectorAll(".api-main-buttom.w-button");
26+
buttons.forEach((b) => {
27+
const url = new URL(b.getAttribute("href")!);
28+
const model = window.location.pathname.match(/\/models\/(.+?)\/?/)?.[1];
29+
if (model) {
30+
url.searchParams.set("model", model);
31+
}
32+
33+
url.hostname = window.location.hostname;
34+
35+
b.setAttribute("href", url.toString());
36+
});
37+
38+
if (attempts > 20) {
39+
console.error("failed to inject hostnames: timeout exceeded");
40+
clearInterval(intervalId);
2841
}
2942

30-
url.hostname = window.location.hostname;
31-
32-
b.setAttribute("href", url.toString());
33-
});
43+
if (buttons.length > 0) {
44+
console.error("hostnames injected");
45+
clearInterval(intervalId);
46+
} else {
47+
console.warn("failed to inject hostnames: no buttons detected");
48+
attempts += 1;
49+
}
50+
}, 1000);
3451
} catch (error) {
3552
console.error("failed to update hostnames", error);
3653
}

0 commit comments

Comments
 (0)