-
Notifications
You must be signed in to change notification settings - Fork 212
/
index.js
171 lines (140 loc) · 5.24 KB
/
index.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
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
let isRequesting = false;
const API_URL = "https://sharegpt.com/api/conversations";
const PAGE_URL = "https://sharegpt.com/c/";
// const API_URL = "http://localhost:3000/api/conversations";
// const PAGE_URL = "http://localhost:3000/c/";
function init() {
const shareButton = createBtn();
function appendShareButton() {
const buttonsWrapper = document.querySelector(
"#__next main form > div div:nth-of-type(1) > div"
);
const mobileWrapper = document.querySelector("#__next main form > div");
if (window.matchMedia("(max-width: 768px)").matches) {
shareButton.style.marginLeft = "6px";
mobileWrapper.appendChild(shareButton);
} else {
if (buttonsWrapper) {
buttonsWrapper.appendChild(shareButton);
console.log(buttonsWrapper);
} else {
}
}
}
appendShareButton();
const id = setInterval(() => {
if (
!document.querySelector("#export-button") ||
document.querySelector("#export-button").style.display === "none"
) {
appendShareButton();
}
}, 500);
shareButton.addEventListener("click", async () => {
if (isRequesting) return;
isRequesting = true;
shareButton.textContent = "Sharing...";
shareButton.style.cursor = "initial";
const threadContainer = document.getElementsByClassName(
"flex flex-col text-sm dark:bg-gray-800"
)[0];
// show the model for chatgpt+ users
let model;
const chatGptPlusElement = document.querySelector(".gold-new-button");
const isNotChatGptPlus =
chatGptPlusElement && chatGptPlusElement.innerText.includes("Upgrade");
if (!isNotChatGptPlus) {
const modelElement = threadContainer.firstChild;
model = modelElement.innerText;
}
const conversationData = {
title: document.title,
avatarUrl: getAvatarImage(),
model,
items: [],
};
for (const node of threadContainer.children) {
const markdown = node.querySelector(".markdown");
// tailwind class indicates human or gpt
if ([...node.classList].includes("dark:bg-gray-800")) {
const warning = node.querySelector(".text-orange-500");
if (warning) {
conversationData.items.push({
from: "human",
value: warning.innerText.split("\n")[0],
});
} else {
const text = node.querySelector(".whitespace-pre-wrap");
conversationData.items.push({
from: "human",
value: text.textContent,
});
}
// if it's a GPT response, it might contain code blocks
} else if (markdown) {
conversationData.items.push({
from: "gpt",
value: markdown.outerHTML,
});
}
}
const res = await fetch(API_URL, {
body: JSON.stringify(conversationData),
headers: { "Content-Type": "application/json" },
method: "POST",
}).catch((err) => {
isRequesting = false;
alert(`Error saving conversation: ${err.message}`);
});
const { id } = await res.json();
const url = PAGE_URL + id;
window.open(url, "_blank");
setTimeout(() => {
shareButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-3 h-3">
<path stroke-linecap="round" stroke-linejoin="round" d="M7.217 10.907a2.25 2.25 0 100 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186l9.566-5.314m-9.566 7.5l9.566 5.314m0 0a2.25 2.25 0 103.935 2.186 2.25 2.25 0 00-3.935-2.186zm0-12.814a2.25 2.25 0 103.933-2.185 2.25 2.25 0 00-3.933 2.185z" />
</svg>Share`;
shareButton.style.cursor = "pointer";
isRequesting = false;
}, 1000);
});
}
function showIfNotLoading(loadingElement, newElement) {
const timerId = setInterval(() => {
if (loadingElement.disabled) {
isLoading = true;
newElement.style.display = "none";
} else {
isLoading = false;
newElement.style.display = "flex";
clearInterval(timerId);
}
}, 100);
}
function getAvatarImage() {
// Create a canvas element
try {
const canvas = document.createElement("canvas");
const image = document.querySelectorAll("img")[3];
// Set the canvas size to 30x30 pixels
canvas.width = 48;
canvas.height = 48;
// Draw the img onto the canvas
canvas.getContext("2d").drawImage(image, 0, 0);
// Convert the canvas to a base64 string as a JPEG image
const base64 = canvas.toDataURL("image/jpeg");
return base64;
} catch (error) {
console.log("Error generating avatar image.");
return null;
}
}
function createBtn() {
const button = document.createElement("button");
button.id = "export-button";
button.classList.add("btn", "flex", "gap-2", "justify-center", "btn-neutral");
button.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-3 h-3">
<path stroke-linecap="round" stroke-linejoin="round" d="M7.217 10.907a2.25 2.25 0 100 2.186m0-2.186c.18.324.283.696.283 1.093s-.103.77-.283 1.093m0-2.186l9.566-5.314m-9.566 7.5l9.566 5.314m0 0a2.25 2.25 0 103.935 2.186 2.25 2.25 0 00-3.935-2.186zm0-12.814a2.25 2.25 0 103.933-2.185 2.25 2.25 0 00-3.933 2.185z" />
</svg>Share`;
return button;
}
init();