-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
660 lines (588 loc) · 20.4 KB
/
content.js
File metadata and controls
660 lines (588 loc) · 20.4 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
(() => {
// ---- Constants ----
const BTN_ID = "injectgpt-settings-btn";
const PANEL_ID = "injectgpt-settings-panel";
// Available models
const MODELS = [
{ value: "gpt-5-2", label: "GPT-5-2" },
{ value: "gpt-5-2-instant", label: "GPT-5-2 Instant" },
{ value: "gpt-5-2-thinking", label: "GPT-5-2 Thinking" },
{ value: "gpt-5-mini-thinking", label: "GPT-5 Thinking Mini (Alpha)" },
];
// Default profile
const DEFAULT_PROFILE = {
id: "default",
name: "Default",
systemPrompt: "You are a helpful assistant.",
developerPrompt: ""
};
// ---- Helpers ----
function el(tag, attrs = {}, children = []) {
const n = document.createElement(tag);
for (const [k, v] of Object.entries(attrs)) {
if (k === "class") n.className = v;
else if (k === "text") n.textContent = v;
else if (k === "html") n.innerHTML = v;
else n.setAttribute(k, v);
}
for (const c of children) n.appendChild(c);
return n;
}
function generateId() {
return 'profile_' + Date.now() + '_' + Math.random().toString(36).substr(2, 9);
}
function injectInterceptor() {
if (document.getElementById("chatgpt-injector")) return;
const script = document.createElement("script");
script.id = "chatgpt-injector";
script.src = chrome.runtime.getURL("injector.js");
(document.head || document.documentElement).appendChild(script);
}
function updateModel(model) {
window.dispatchEvent(
new CustomEvent("chatgpt-update-model", {
detail: { model },
})
);
}
function updateSystemPrompt(prompt) {
window.dispatchEvent(
new CustomEvent("chatgpt-update-system-prompt", {
detail: { prompt },
})
);
}
function updateDeveloperPrompt(prompt) {
window.dispatchEvent(
new CustomEvent("chatgpt-update-developer-prompt", {
detail: { prompt },
})
);
}
function updateEnabled(enabled) {
window.dispatchEvent(
new CustomEvent("chatgpt-update-enabled", {
detail: { enabled },
})
);
}
function ensureStyles() {
if (document.getElementById("injectgpt-settings-styles")) return;
const style = el("style", { id: "injectgpt-settings-styles" });
style.textContent = `
#${BTN_ID}{
position:fixed;
right:16px;
bottom:16px;
z-index:2147483647;
border:0;
padding:10px 12px;
border-radius:999px;
background:#111;
color:#fff;
font:600 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
cursor:pointer;
box-shadow:0 10px 30px rgba(0,0,0,.25);
transition: background 0.2s;
}
#${BTN_ID}:hover{ transform: translateY(-1px); }
#${BTN_ID}:active{ transform: translateY(0px); }
#${BTN_ID}.disabled{ background:#666; }
#${PANEL_ID}{
position:fixed;
inset:0;
z-index:2147483647;
display:none;
background:rgba(0,0,0,.35);
font-family:system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}
#${PANEL_ID}.show{ display:block; }
#${PANEL_ID} .box{
position:absolute;
right:16px;
bottom:72px;
width:min(520px, calc(100vw - 32px));
max-height:calc(100vh - 100px);
background:#fff;
color:#111;
border-radius:16px;
box-shadow:0 20px 60px rgba(0,0,0,.35);
overflow:hidden;
display:flex;
flex-direction:column;
}
#${PANEL_ID} .hdr{
display:flex;
align-items:center;
justify-content:space-between;
padding:12px 14px;
border-bottom:1px solid #eee;
flex-shrink:0;
}
#${PANEL_ID} .hdr h3{
margin:0;
font-size:14px;
}
#${PANEL_ID} .hdr-right{
display:flex;
align-items:center;
gap:8px;
}
#${PANEL_ID} .close{
border:0;
background:transparent;
font-size:18px;
cursor:pointer;
line-height:1;
padding:6px 10px;
border-radius:10px;
}
#${PANEL_ID} .close:hover{ background:#f3f3f3; }
#${PANEL_ID} .toggle-container{
display:flex;
align-items:center;
gap:8px;
}
#${PANEL_ID} .toggle-label{
font-size:12px;
font-weight:600;
color:#555;
}
#${PANEL_ID} .toggle{
position:relative;
width:44px;
height:24px;
background:#ccc;
border-radius:24px;
cursor:pointer;
transition:background 0.2s;
}
#${PANEL_ID} .toggle.on{ background:#22c55e; }
#${PANEL_ID} .toggle::after{
content:'';
position:absolute;
top:2px;
left:2px;
width:20px;
height:20px;
background:#fff;
border-radius:50%;
transition:transform 0.2s;
box-shadow:0 1px 3px rgba(0,0,0,.2);
}
#${PANEL_ID} .toggle.on::after{ transform:translateX(20px); }
#${PANEL_ID} .body{
padding:14px;
display:flex;
flex-direction:column;
gap:12px;
overflow-y:auto;
flex:1;
}
#${PANEL_ID} label{ font-size:12px; font-weight:700; display:block; margin-bottom:6px; }
#${PANEL_ID} textarea{
width:100%;
min-height:92px;
resize:vertical;
padding:10px 12px;
border-radius:12px;
border:1px solid #ddd;
font: 13px/1.35 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
outline:none;
}
#${PANEL_ID} textarea:focus{ border-color:#999; }
#${PANEL_ID} select{
width:100%;
padding:10px 12px;
border-radius:12px;
border:1px solid #ddd;
font: 13px/1.35 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
outline:none;
background:#fff;
cursor:pointer;
}
#${PANEL_ID} select:focus{ border-color:#999; }
#${PANEL_ID} input[type="text"]{
width:100%;
padding:10px 12px;
border-radius:12px;
border:1px solid #ddd;
font: 13px/1.35 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
outline:none;
}
#${PANEL_ID} input[type="text"]:focus{ border-color:#999; }
#${PANEL_ID} .actions{
display:flex;
gap:10px;
justify-content:flex-end;
padding:12px 14px;
border-top:1px solid #eee;
background:#fafafa;
flex-shrink:0;
}
#${PANEL_ID} .btn{
border:0;
border-radius:12px;
padding:9px 12px;
cursor:pointer;
font:600 13px/1 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}
#${PANEL_ID} .btn.secondary{ background:#eee; }
#${PANEL_ID} .btn.primary{ background:#111; color:#fff; }
#${PANEL_ID} .btn.danger{ background:#fee2e2; color:#dc2626; border:1px solid #fecaca; transition: background 0.15s; }
#${PANEL_ID} .btn.danger:hover{ background:#fecaca; }
#${PANEL_ID} .btn.danger:disabled{ background:#f5f5f5; color:#ccc; border-color:#eee; }
#${PANEL_ID} .btn:disabled{ opacity:0.5; cursor:not-allowed; }
#${PANEL_ID} .hint{
font-size:12px;
color:#555;
margin-top:6px;
}
#${PANEL_ID} .profile-section{
background:#f8f8f8;
border-radius:12px;
padding:12px;
display:flex;
flex-direction:column;
gap:10px;
}
#${PANEL_ID} .profile-row{
display:flex;
gap:8px;
align-items:center;
}
#${PANEL_ID} .profile-row select{
flex:1;
}
#${PANEL_ID} .profile-row .btn{
flex-shrink:0;
padding:10px 12px;
}
#${PANEL_ID} .profile-name-row{
display:none;
gap:8px;
align-items:center;
}
#${PANEL_ID} .profile-name-row.show{
display:flex;
}
#${PANEL_ID} .profile-name-row input{
flex:1;
}
#${PANEL_ID} .divider{
height:1px;
background:#e5e5e5;
margin:4px 0;
}
#${PANEL_ID} .bmc-btn{
display:inline-flex;
align-items:center;
gap:6px;
background:#FFDD00;
color:#000;
border:0;
border-radius:10px;
padding:6px 12px;
font:600 12px/1 system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
cursor:pointer;
text-decoration:none;
transition: background 0.15s, transform 0.1s;
white-space:nowrap;
}
#${PANEL_ID} .bmc-btn:hover{ background:#ffca00; transform:translateY(-1px); }
#${PANEL_ID} .bmc-btn:active{ transform:translateY(0); }
`;
document.documentElement.appendChild(style);
}
// ---- Storage helpers ----
function storageGet(keys) {
return new Promise((resolve) => chrome.storage.local.get(keys, resolve));
}
function storageSet(obj) {
return new Promise((resolve) => chrome.storage.local.set(obj, resolve));
}
// ---- UI ----
async function ensureUI() {
if (document.getElementById(BTN_ID) && document.getElementById(PANEL_ID)) return;
ensureStyles();
// Load saved values (with defaults)
const saved = await storageGet(["selectedModel", "enabled", "profiles", "activeProfileId"]);
const selectedModel = typeof saved.selectedModel === "string" ? saved.selectedModel : "gpt-5-2";
const enabled = typeof saved.enabled === "boolean" ? saved.enabled : true;
let profiles = Array.isArray(saved.profiles) ? saved.profiles : [DEFAULT_PROFILE];
let activeProfileId = typeof saved.activeProfileId === "string" ? saved.activeProfileId : "default";
// Ensure default profile exists
if (!profiles.find(p => p.id === "default")) {
profiles.unshift(DEFAULT_PROFILE);
}
// Ensure active profile exists
let activeProfile = profiles.find(p => p.id === activeProfileId);
if (!activeProfile) {
activeProfileId = "default";
activeProfile = profiles.find(p => p.id === "default");
}
// Button
if (!document.getElementById(BTN_ID)) {
const btn = el("button", { id: BTN_ID, type: "button", text: enabled ? "⚙️ InjectGPT" : "⚙️ InjectGPT (Off)" });
if (!enabled) btn.classList.add("disabled");
btn.addEventListener("click", () => {
const panel = document.getElementById(PANEL_ID);
panel.classList.toggle("show");
});
document.documentElement.appendChild(btn);
}
// Panel
if (!document.getElementById(PANEL_ID)) {
// Toggle switch
const toggle = el("div", { class: enabled ? "toggle on" : "toggle" });
// Model select
const modelSelect = el("select");
for (const model of MODELS) {
const option = el("option", { value: model.value, text: model.label });
if (model.value === selectedModel) {
option.selected = true;
}
modelSelect.appendChild(option);
}
// Profile select
const profileSelect = el("select");
function updateProfileSelect() {
profileSelect.innerHTML = "";
for (const profile of profiles) {
const option = el("option", { value: profile.id, text: profile.name });
if (profile.id === activeProfileId) {
option.selected = true;
}
profileSelect.appendChild(option);
}
}
updateProfileSelect();
// Profile name input (for new/rename)
const profileNameInput = el("input", { type: "text", placeholder: "Profile name..." });
const profileNameRow = el("div", { class: "profile-name-row" }, [
profileNameInput,
el("button", { class: "btn primary", type: "button", text: "Create" }),
el("button", { class: "btn secondary", type: "button", text: "Cancel" }),
]);
const newProfileBtn = el("button", { class: "btn secondary", type: "button", text: "+ New" });
const deleteProfileBtn = el("button", { class: "btn danger", type: "button", html: "✕ Delete" });
// Disable delete for default profile
function updateDeleteBtn() {
deleteProfileBtn.disabled = activeProfileId === "default";
}
updateDeleteBtn();
const sysTa = el("textarea");
sysTa.value = activeProfile.systemPrompt;
const devTa = el("textarea");
devTa.value = activeProfile.developerPrompt;
// Buy Me a Coffee link
const bmcLink = el("a", {
class: "bmc-btn",
href: "https://buymeacoffee.com/jonathanyly",
target: "_blank",
rel: "noopener noreferrer",
html: '☕'
});
const panel = el("div", { id: PANEL_ID }, [
el("div", { class: "box" }, [
el("div", { class: "hdr" }, [
el("h3", { text: "InjectGPT Settings" }),
el("div", { class: "hdr-right" }, [
bmcLink,
el("div", { class: "toggle-container" }, [
el("span", { class: "toggle-label", text: "Enabled" }),
toggle,
]),
el("button", { class: "close", type: "button", text: "✕", title: "Close" }),
]),
]),
el("div", { class: "body" }, [
el("div", {}, [
el("label", { text: "Model" }),
modelSelect,
el("div", { class: "hint", text: "Select the GPT model to use." }),
]),
el("div", { class: "profile-section" }, [
el("label", { text: "Profile", style: "margin-bottom:0" }),
el("div", { class: "profile-row" }, [
profileSelect,
newProfileBtn,
deleteProfileBtn,
]),
profileNameRow,
]),
el("div", { class: "divider" }),
el("div", {}, [
el("label", { text: "System prompt" }),
sysTa,
el("div", { class: "hint", text: "Highest priority instruction for the assistant." }),
]),
el("div", {}, [
el("label", { text: "Developer prompt" }),
devTa,
el("div", { class: "hint", text: "Additional instruction after system, before user." }),
]),
]),
el("div", { class: "actions" }, [
el("button", { class: "btn secondary", type: "button", html: "Reset Profile" }),
el("button", { class: "btn primary", type: "button", text: "Save" }),
]),
]),
]);
// Profile name row buttons
const [createBtn, cancelBtn] = profileNameRow.querySelectorAll(".btn");
newProfileBtn.addEventListener("click", () => {
profileNameInput.value = "";
profileNameRow.classList.add("show");
profileNameInput.focus();
});
cancelBtn.addEventListener("click", () => {
profileNameRow.classList.remove("show");
});
createBtn.addEventListener("click", async () => {
const name = profileNameInput.value.trim();
if (!name) return;
const newProfile = {
id: generateId(),
name: name,
systemPrompt: sysTa.value,
developerPrompt: devTa.value
};
profiles.push(newProfile);
activeProfileId = newProfile.id;
await storageSet({ profiles, activeProfileId });
updateProfileSelect();
updateDeleteBtn();
profileNameRow.classList.remove("show");
});
profileNameInput.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
createBtn.click();
} else if (e.key === "Escape") {
cancelBtn.click();
}
});
deleteProfileBtn.addEventListener("click", async () => {
if (activeProfileId === "default") return;
if (!confirm(`Delete profile "${profiles.find(p => p.id === activeProfileId)?.name}"?`)) return;
profiles = profiles.filter(p => p.id !== activeProfileId);
activeProfileId = "default";
activeProfile = profiles.find(p => p.id === "default");
sysTa.value = activeProfile.systemPrompt;
devTa.value = activeProfile.developerPrompt;
await storageSet({ profiles, activeProfileId });
updateProfileSelect();
updateDeleteBtn();
// Update injector
updateSystemPrompt(sysTa.value);
updateDeveloperPrompt(devTa.value);
});
profileSelect.addEventListener("change", async () => {
// Save current profile first
const currentProfile = profiles.find(p => p.id === activeProfileId);
if (currentProfile) {
currentProfile.systemPrompt = sysTa.value;
currentProfile.developerPrompt = devTa.value;
}
// Switch to new profile
activeProfileId = profileSelect.value;
activeProfile = profiles.find(p => p.id === activeProfileId);
if (activeProfile) {
sysTa.value = activeProfile.systemPrompt;
devTa.value = activeProfile.developerPrompt;
}
await storageSet({ profiles, activeProfileId });
updateDeleteBtn();
// Update injector
updateSystemPrompt(sysTa.value);
updateDeveloperPrompt(devTa.value);
});
// Helper to save current settings
async function saveSettings() {
const model = modelSelect.value;
const sys = sysTa.value ?? "";
const dev = devTa.value ?? "";
const isEnabled = toggle.classList.contains("on");
// Update active profile
const currentProfile = profiles.find(p => p.id === activeProfileId);
if (currentProfile) {
currentProfile.systemPrompt = sys;
currentProfile.developerPrompt = dev;
}
await storageSet({
selectedModel: model,
enabled: isEnabled,
profiles,
activeProfileId
});
updateModel(model);
updateSystemPrompt(sys);
updateDeveloperPrompt(dev);
updateEnabled(isEnabled);
// Update button text
const btn = document.getElementById(BTN_ID);
if (btn) {
btn.textContent = isEnabled ? "⚙️ InjectGPT" : "⚙️ InjectGPT (Off)";
btn.classList.toggle("disabled", !isEnabled);
}
}
// Toggle click handler
toggle.addEventListener("click", async () => {
toggle.classList.toggle("on");
await saveSettings();
});
// Close interactions - save on close
const closeBtn = panel.querySelector(".close");
closeBtn.addEventListener("click", async () => {
await saveSettings();
panel.classList.remove("show");
});
// Save when clicking outside (on backdrop)
panel.addEventListener("click", async (e) => {
if (e.target === panel) {
await saveSettings();
panel.classList.remove("show");
}
});
// Buttons
const [resetBtn, saveBtn] = panel.querySelectorAll(".actions .btn");
resetBtn.addEventListener("click", async () => {
// Reset current profile to defaults
if (activeProfileId === "default") {
sysTa.value = "You are a helpful assistant.";
devTa.value = "";
} else {
sysTa.value = "";
devTa.value = "";
}
// Update profile
const currentProfile = profiles.find(p => p.id === activeProfileId);
if (currentProfile) {
currentProfile.systemPrompt = sysTa.value;
currentProfile.developerPrompt = devTa.value;
}
await storageSet({ profiles });
updateSystemPrompt(sysTa.value);
updateDeveloperPrompt(devTa.value);
});
saveBtn.addEventListener("click", async () => {
await saveSettings();
panel.classList.remove("show");
});
document.documentElement.appendChild(panel);
// Push current settings to page context once injector is likely present
setTimeout(() => {
updateModel(selectedModel);
updateSystemPrompt(activeProfile.systemPrompt);
updateDeveloperPrompt(activeProfile.developerPrompt);
updateEnabled(enabled);
}, 300);
}
}
// ---- Boot ----
injectInterceptor();
// Ensure UI now + keep it alive across SPA navigations
ensureUI();
const obs = new MutationObserver(() => ensureUI());
obs.observe(document.documentElement, { childList: true, subtree: true });
})();