-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.js
108 lines (101 loc) · 3.27 KB
/
backend.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
// ==UserScript==
// @name ChatGPT Chat Monitor
// @namespace ChatGPT
// @version 1.1
// @description Monitors the OpenAI chat usage
// @author DevArq.Sangoi
// @match https://chat.openai.com/*
// ==/UserScript==
(function () {
"use strict";
const scriptContent = `
const originalFetch = window.fetch;
const OriginalAbortController = window.AbortController;
let apiArgs;
let apiModel;
let apiChatId;
let apiFullMsg;
let apiResponse;
let userMsg;
let globalData;
window.fetch = function (...args) {
return originalFetch.apply(this, args).then(async (response) => {
apiArgs = args;
apiResponse = response.clone();
if (apiArgs && apiResponse) {
await getSentMsg();
await getApiResponse();
}
return response;
});
};
window.AbortController = class extends OriginalAbortController {
constructor(...args) {
super(...args);
}
abort(...args) {
return "nice try";
}
};
async function getSentMsg() {
if (apiArgs[1].method === "POST" && apiArgs[0] === "https://chat.openai.com/backend-api/conversation") {
let argsToObj = JSON.parse(apiArgs[1].body);
userMsg = argsToObj.messages[0].content.parts[0];
window.localStorage.setItem("[das]_lastUserMsg", userMsg);
} else if (
apiArgs[1].method === "GET" &&
apiArgs[0].includes("https://chat.openai.com/backend-api/conversation/") &&
!apiArgs[0].includes("/interpreter")
) {
apiUrlId = apiArgs[0];
window.localStorage.setItem("[das]_apiUrlId", apiUrlId);
window.postMessage({ source: "Tampermonkey", data: "urlWithGet" }, "*");
}
}
async function getApiResponse() {
apiResponse
.clone()
.text()
.then((data) => {
globalData = data;
const lines = data.split("\\n");
for (const line of lines) {
if (line.startsWith("data: ")) {
const json = line.slice(6);
if (json.startsWith('{"message":')) {
let parsedData = JSON.parse(json);
let isComplete = parsedData.message?.metadata?.is_complete;
if (isComplete) {
apiChatId = parsedData.conversation_id;
apiModel = parsedData.message.metadata.model_slug;
switch (parsedData.message.content.content_type) {
case "text":
apiFullMsg = parsedData.message.content.parts[0];
break;
case "code":
case "execution_output":
apiFullMsg = parsedData.message.content.text;
break;
default:
console.log("UNKNOWN CONTENT TYPE:", parsedData.message.content.content_type);
break;
}
window.localStorage.setItem("[das]_apiFullMsg", apiFullMsg);
window.localStorage.setItem("[das]_apiChatId", apiChatId);
window.localStorage.setItem("[das]_lastModelUsed", apiModel);
window.postMessage({ source: "Tampermonkey", data: "fetchDone" }, "*");
}
}
}
}
})
.catch((error) => {
console.log("ERRO LENDO ESSA CACETA:", error);
});
}
`;
let script = document.createElement("script");
script.textContent = scriptContent;
(document.head || document.documentElement).appendChild(script);
script.remove();
})();