Skip to content

Commit 9bc079d

Browse files
authored
bugfix: made Trieve script safer to load (#245)
1 parent 7375fe1 commit 9bc079d

File tree

1 file changed

+82
-63
lines changed

1 file changed

+82
-63
lines changed

fern/assets/trieve-user-script.js

Lines changed: 82 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -10,82 +10,101 @@
1010
// ==/UserScript==
1111

1212
const removeAllClickListeners = (element) => {
13-
const newElement = element.cloneNode(true);
14-
element.parentNode.replaceChild(newElement, element);
15-
return newElement;
13+
try {
14+
const newElement = element?.cloneNode(true);
15+
element.parentNode.replaceChild(newElement, element);
16+
return newElement;
17+
} catch (e) {
18+
return element;
19+
}
1620
};
1721

1822
const makeDefaultSearchTrieve = async () => {
19-
let defaultSearchBar = null;
20-
let retries = 0;
21-
while (!defaultSearchBar && retries < 10) {
22-
for (const el of document.querySelectorAll("*")) {
23-
if (el.querySelector('[aria-label="Search"]')) {
24-
defaultSearchBar = el.querySelector('[aria-label="Search"]');
25-
break;
23+
try {
24+
let defaultSearchBar = null;
25+
let retries = 0;
26+
while (!defaultSearchBar && retries < 10) {
27+
for (const el of document.querySelectorAll("*")) {
28+
if (el.querySelector('[aria-label="Search"]')) {
29+
defaultSearchBar = el.querySelector('[aria-label="Search"]');
30+
break;
31+
}
2632
}
33+
retries++;
34+
await new Promise((resolve) => setTimeout(resolve, 10));
2735
}
28-
retries++;
29-
await new Promise((resolve) => setTimeout(resolve, 10));
30-
}
31-
defaultSearchBar = removeAllClickListeners(defaultSearchBar);
36+
defaultSearchBar = removeAllClickListeners(defaultSearchBar);
3237

33-
defaultSearchBar.onclick = () => {
34-
const event = new CustomEvent("trieve-open-with-text", {
35-
detail: { text: "" },
36-
});
37-
window.dispatchEvent(event);
38-
};
38+
defaultSearchBar.onclick = () => {
39+
const event = new CustomEvent("trieve-open-with-text", {
40+
detail: { text: "" },
41+
});
42+
window.dispatchEvent(event);
43+
};
44+
} catch (e) {
45+
console.error(e);
46+
}
3947
};
40-
await makeDefaultSearchTrieve();
41-
setTimeout(makeDefaultSearchTrieve, 50);
42-
setTimeout(makeDefaultSearchTrieve, 100);
43-
setTimeout(makeDefaultSearchTrieve, 250);
44-
setTimeout(makeDefaultSearchTrieve, 500);
45-
setTimeout(makeDefaultSearchTrieve, 1000);
46-
setTimeout(makeDefaultSearchTrieve, 2000);
47-
setTimeout(makeDefaultSearchTrieve, 3000);
48+
try {
49+
await makeDefaultSearchTrieve();
50+
setTimeout(makeDefaultSearchTrieve, 50);
51+
setTimeout(makeDefaultSearchTrieve, 100);
52+
setTimeout(makeDefaultSearchTrieve, 250);
53+
setTimeout(makeDefaultSearchTrieve, 500);
54+
setTimeout(makeDefaultSearchTrieve, 1000);
55+
setTimeout(makeDefaultSearchTrieve, 2000);
56+
setTimeout(makeDefaultSearchTrieve, 3000);
57+
} catch (e) {
58+
console.error(e);
59+
}
4860

4961
(async function () {
5062
"use strict";
51-
const link = document.createElement("link");
52-
link.rel = "stylesheet";
53-
link.href = "https://cdn.trieve.ai/beta/search-component/index.css";
54-
document.head.appendChild(link);
55-
63+
try {
64+
const link = document.createElement("link");
65+
link.rel = "stylesheet";
66+
link.href = "https://cdn.trieve.ai/beta/search-component/index.css";
67+
document.head.appendChild(link);
68+
} catch (e) {
69+
console.error(e);
70+
}
5671
import("https://cdn.trieve.ai/beta/search-component/vanilla/index.js").then(
5772
async (module) => {
58-
const { renderToDiv } = module;
59-
const root = document.createElement("div");
60-
root.classList.add("trigger");
61-
document.body.appendChild(root);
62-
const colorScheme = document.documentElement?.style?.colorScheme;
63-
const brandColor = colorScheme === "dark" ? "#47ffb6d5" : "#00551dcd";
73+
try {
74+
const { renderToDiv } = module;
75+
const root = document.createElement("div");
76+
root.classList.add("trigger");
77+
document.body.appendChild(root);
78+
const colorScheme = document.documentElement?.style?.colorScheme;
79+
const brandColor = colorScheme === "dark" ? "#47ffb6d5" : "#00551dcd";
6480

65-
renderToDiv(root, {
66-
apiKey: "tr-hZMKSsTf3ML9hJbAAqPO8K91p9IVe9Oc",
67-
datasetId: "d3493dc0-2b5c-4c6e-a8ee-b18feeed5b06",
68-
baseUrl: "https://api.trieve.ai",
69-
type: "docs",
70-
analytics: true,
71-
theme: colorScheme === "dark" ? "dark" : null,
72-
brandLogoImgSrcUrl:
73-
"https://storage.googleapis.com/organization-image-assets/vapi-botAvatarDarkSrcUrl-1709929110474.png",
74-
brandName: "Vapi",
75-
brandColor: brandColor,
76-
placeholder: "How can I help?",
77-
defaultSearchQueries: ["quickstart", "assistant", "tools"],
78-
defaultAiQuestions: [
79-
"What voices are supported?",
80-
"What languages are supported?",
81-
"How do I connect a custom LLM?",
82-
"How do I fetch the prompt dynamically?",
83-
],
84-
defaultSearchMode: "search",
85-
showFloatingButton: "true",
86-
cssRelease: "none",
87-
hideOpenButton: true,
88-
});
81+
renderToDiv(root, {
82+
apiKey: "tr-hZMKSsTf3ML9hJbAAqPO8K91p9IVe9Oc",
83+
datasetId: "d3493dc0-2b5c-4c6e-a8ee-b18feeed5b06",
84+
baseUrl: "https://api.trieve.ai",
85+
type: "docs",
86+
analytics: true,
87+
theme: colorScheme === "dark" ? "dark" : null,
88+
brandLogoImgSrcUrl:
89+
"https://storage.googleapis.com/organization-image-assets/vapi-botAvatarDarkSrcUrl-1709929110474.png",
90+
brandName: "Vapi",
91+
brandColor: brandColor,
92+
placeholder: "How can I help?",
93+
defaultSearchQueries: ["quickstart", "assistant", "tools"],
94+
defaultAiQuestions: [
95+
"What voices are supported?",
96+
"What languages are supported?",
97+
"How do I connect a custom LLM?",
98+
"How do I fetch the prompt dynamically?",
99+
],
100+
defaultSearchMode: "search",
101+
showFloatingButton: "true",
102+
cssRelease: "none",
103+
hideOpenButton: true,
104+
});
105+
} catch (e) {
106+
console.error(e);
107+
}
89108
},
90109
(error) => {
91110
console.error("Failed to load module:", error);

0 commit comments

Comments
 (0)