-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinjector.js
More file actions
162 lines (139 loc) · 5.24 KB
/
injector.js
File metadata and controls
162 lines (139 loc) · 5.24 KB
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
// This script runs in the MAIN world (page context) and intercepts fetch requests
(function() {
if (window.__CHATGPT_TRANSLATOR_INTERCEPTED__) return;
window.__CHATGPT_TRANSLATOR_INTERCEPTED__ = true;
window.__CHATGPT_TRANSLATOR_MODEL__ = "gpt-5-2";
window.__CHATGPT_TRANSLATOR_SYSTEM_PROMPT__ = "You are a helpful assistant.";
window.__CHATGPT_TRANSLATOR_DEVELOPER_PROMPT__ = "";
// Helper function to check if URL matches /conversation endpoint
function isConversationUrl(url) {
if (!url) return false;
const urlString = url.toString();
return urlString.includes('/conversation');
}
// Function to modify the request body
function modifyRequestBody(bodyData) {
const currentModel = window.__CHATGPT_TRANSLATOR_MODEL__;
const systemPrompt = window.__CHATGPT_TRANSLATOR_SYSTEM_PROMPT__;
const developerPrompt = window.__CHATGPT_TRANSLATOR_DEVELOPER_PROMPT__;
// Modify the model
if (bodyData.model !== undefined) {
bodyData.model = currentModel;
}
// Modify messages array if it exists
if (bodyData.messages && Array.isArray(bodyData.messages)) {
bodyData.messages = bodyData.messages.map(msg => {
// Modify system message
if (msg.author && msg.author.role === 'system') {
return {
...msg,
content: {
...msg.content,
parts: [systemPrompt]
}
};
}
// Modify developer message
if (msg.author && msg.author.role === 'developer') {
return {
...msg,
content: {
...msg.content,
parts: [developerPrompt]
}
};
}
return msg;
});
// If there's no developer message but we have a developer prompt, add one
if (developerPrompt) {
const hasDeveloperMsg = bodyData.messages.some(msg => msg.author && msg.author.role === 'developer');
if (!hasDeveloperMsg) {
bodyData.messages.push({
id: crypto.randomUUID(),
author: { role: 'developer' },
content: {
content_type: 'text',
parts: [developerPrompt]
}
});
}
}
// If there's no system message but we have a system prompt, add one at the beginning
if (systemPrompt) {
const hasSystemMsg = bodyData.messages.some(msg => msg.author && msg.author.role === 'system');
if (!hasSystemMsg) {
bodyData.messages.unshift({
id: crypto.randomUUID(),
author: { role: 'system' },
content: {
content_type: 'text',
parts: [systemPrompt]
}
});
}
}
}
return bodyData;
}
// Intercept fetch
const originalFetch = window.fetch;
window.fetch = function(...args) {
let [url, options] = args;
// Check if this is a conversation request based on URL
if (isConversationUrl(url) && options && options.body && typeof options.body === 'string') {
try {
let bodyData = JSON.parse(options.body);
bodyData = modifyRequestBody(bodyData);
// Create new options with modified body
options = { ...options, body: JSON.stringify(bodyData) };
} catch (e) {
console.log('Could not parse body:', e.message);
}
}
return originalFetch.apply(this, [url, options]);
};
// Intercept XMLHttpRequest
const originalXHROpen = XMLHttpRequest.prototype.open;
const originalXHRSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.open = function(...args) {
this._url = args[1];
return originalXHROpen.apply(this, args);
};
XMLHttpRequest.prototype.send = function(body) {
// Check if this is a conversation request based on URL
if (isConversationUrl(this._url) && body && typeof body === 'string') {
try {
let bodyData = JSON.parse(body);
// Modify the request body
bodyData = modifyRequestBody(bodyData);
body = JSON.stringify(bodyData);
} catch (e) {
console.log('Could not parse XHR body:', e.message);
}
}
return originalXHRSend.call(this, body);
};
// Listen for model updates from content script
window.addEventListener('chatgpt-translator-update-model', function(e) {
if (e.detail && e.detail.model) {
window.__CHATGPT_TRANSLATOR_MODEL__ = e.detail.model;
console.log('Model updated to:', e.detail.model);
}
});
// Listen for system prompt updates from content script
window.addEventListener('chatgpt-translator-update-system-prompt', function(e) {
if (e.detail && e.detail.prompt !== undefined) {
window.__CHATGPT_TRANSLATOR_SYSTEM_PROMPT__ = e.detail.prompt;
console.log('System prompt updated to:', e.detail.prompt);
}
});
// Listen for developer prompt updates from content script
window.addEventListener('chatgpt-translator-update-developer-prompt', function(e) {
if (e.detail && e.detail.prompt !== undefined) {
window.__CHATGPT_TRANSLATOR_DEVELOPER_PROMPT__ = e.detail.prompt;
console.log('Developer prompt updated to:', e.detail.prompt);
}
});
console.log('Request interception enabled');
})();