This repository was archived by the owner on Jul 1, 2023. It is now read-only.
forked from wong2/chatgpt-google-extension
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent-script.js
42 lines (38 loc) · 1.4 KB
/
content-script.js
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
function getMainContainerWidth() {
const resultContainer = document.getElementById("center_col");
if (resultContainer) {
return resultContainer.offsetWidth + "px";
}
}
async function run(question) {
const container = document.createElement("div");
container.className = "chat-gpt-container";
container.innerHTML = '<p class="loading">Waiting for ChatGPT response...</p>';
const siderbarContainer = document.getElementById("rhs");
if (siderbarContainer) {
siderbarContainer.prepend(container);
} else {
container.classList.add("sidebar-free");
document.getElementById("rcnt").appendChild(container);
}
const port = browser.runtime.connect();
port.onMessage.addListener(function (msg) {
if (msg.answer) {
container.innerHTML = `<p><span class="prefix">ChatGPT:</span><pre>${msg.answer}</pre></p>`;
} else if (msg.error === "UNAUTHORIZED") {
container.innerHTML =
'<p>Please login at <a href="https://chat.openai.com" target="_blank">chat.openai.com</a> first</p>';
} else {
container.innerHTML = "<p>Failed to load response from ChatGPT</p>";
}
});
port.postMessage({ question });
}
const searchInput = document.getElementsByName("q")[0];
if (searchInput && searchInput.value) {
// only run on first page
const startParam = new URL(location.href).searchParams.get("start") || "0";
if (startParam === "0") {
run(searchInput.value);
}
}