-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathscript.js
180 lines (158 loc) · 5.49 KB
/
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
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
// --- CSS / UI definitions ---
const styles = {
toggleUI: `
position: fixed;
bottom: 20px;
right: 20px;
padding: 10px;
background-color: #007BFF;
border: none;
border-radius: 50%;
color: white;
font-weight: bold;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.25);
cursor: pointer;
z-index: 10000;
`,
downloadWidget: `
position: fixed;
bottom: 10px;
right: 10px;
background: #fff;
border: 1px solid #ccc;
padding: 20px;
border-radius: 10px;
z-index: 9999;
width: 300px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
`,
};
const toggleUIHTML = `
<button id="toggle-openai-ui" style="${styles.toggleUI}">⏬</button>
`;
const uiHTML = `
<div id="openai-download-widget" style="${styles.downloadWidget}">
<div style="margin-bottom: 15px">
<label for="numConversations" style="display: block; margin-bottom: 5px; font-weight: 600">Number of conversations:</label>
<input type="number" id="numConversations" value="50" style="width: 100%; padding: 5px; border: 1px solid #ccc; border-radius: 5px; box-sizing: border-box;" />
</div>
<div style="margin-bottom: 15px">
<label for="openai-download-progress" style="display: block; margin-bottom: 5px; font-weight: 600">Download Progress:</label>
<progress id="openai-download-progress" value="0" max="100" style="width: 100%; height: 15px; border-radius: 5px"></progress>
</div>
<div style="text-align: left">
<button id="openai-start-download" style="padding: 10px 15px; background-color: #007bff; color: #fff; border: none; border-radius: 5px; cursor: pointer;">Start Download</button>
</div>
</div>
`;
// Append UI components
document.body.insertAdjacentHTML("beforeend", toggleUIHTML + uiHTML);
// --- Utility functions ---
const toggleUIVisibility = () => {
const uiWidget = document.getElementById("openai-download-widget");
uiWidget.style.display = uiWidget.style.display === "none" ? "block" : "none";
};
function constructUrlWithParams(baseUrl, params) {
const url = new URL(baseUrl);
Object.keys(params).forEach((key) =>
url.searchParams.append(key, params[key])
);
return url.toString();
}
// --- Event Listeners ---
document
.getElementById("toggle-openai-ui")
.addEventListener("click", toggleUIVisibility);
document
.getElementById("openai-start-download")
.addEventListener("click", async () => {
const numConversations = parseInt(
document.getElementById("numConversations").value,
10
);
if (numConversations && numConversations > 0) {
document.getElementById("openai-start-download").disabled = true;
try {
await downloadConversationsAsJson(numConversations);
console.log("Download completed.");
} catch (error) {
console.error("An error occurred:", error);
}
document.getElementById("openai-start-download").disabled = false;
} else {
alert("Please enter a valid number of conversations.");
}
});
// --- Main functionality ---
const conversationsPerRequest = 50;
const delay = 63000;
async function fetchData(url, headers = {}) {
try {
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error(`Error fetching data from ${url}:`, error);
throw error;
}
}
async function fetchAccessToken() {
const data = await fetchData("https://chat.openai.com/api/auth/session");
return data.accessToken;
}
async function fetchConversationsData(accessToken, limit, offset) {
const baseUrl = "https://chat.openai.com/backend-api/conversations";
const url = constructUrlWithParams(baseUrl, {
offset: offset,
limit: limit,
order: "updated",
});
const data = await fetchData(url, { Authorization: `Bearer ${accessToken}` });
return data.items;
}
async function fetchConversationData(accessToken, conversationId) {
const url = `https://chat.openai.com/backend-api/conversation/${conversationId}`;
return fetchData(url, { Authorization: `Bearer ${accessToken}` });
}
async function downloadConversationsAsJson(numConversations) {
const conversationsArray = [];
const accessToken = await fetchAccessToken();
const numRequests = Math.ceil(numConversations / conversationsPerRequest);
for (let i = 0; i < numRequests; i++) {
const offset = i * conversationsPerRequest;
const limit =
i === numRequests - 1
? numConversations - offset
: conversationsPerRequest;
const conversationsData = await fetchConversationsData(
accessToken,
limit,
offset
);
for (let j = 0; j < conversationsData.length; j++) {
const conversation = conversationsData[j];
const conversationData = await fetchConversationData(
accessToken,
conversation.id
);
conversationsArray.push(conversationData);
const progress =
((i * conversationsPerRequest + j + 1) / numConversations) * 100;
document.getElementById("openai-download-progress").value = progress;
}
if (i < numRequests - 1) {
await new Promise((resolve) => setTimeout(resolve, delay));
}
}
const content = new Blob([JSON.stringify(conversationsArray)], {
type: "application/json",
});
const a = document.createElement("a");
a.href = URL.createObjectURL(content);
a.download = "chatgpt_bookmarklet_download.json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}