-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
206 lines (188 loc) · 6.99 KB
/
Copy pathcontent.js
File metadata and controls
206 lines (188 loc) · 6.99 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
// content.js
const WID = "gpt5-sma-subject-matter-agents-builder";
/* ---------- selection helper ---------- */
function getSelectionText() {
const ae = document.activeElement;
if (ae && (ae.tagName === "TEXTAREA" || (ae.tagName === "INPUT" && /text|search|url|tel|password/i.test(ae.type)))) {
const { selectionStart: s = 0, selectionEnd: e = 0, value = "" } = ae;
return value.substring(Math.min(s, e), Math.max(s, e)).trim();
}
const t = (window.getSelection && window.getSelection().toString()) || "";
return t.trim();
}
/* ---------- safe postMessage into iframe ---------- */
function postToPanel(iframe, payload) {
if (!iframe) return;
const send = () => {
try { iframe.contentWindow.postMessage(payload, "*"); } catch {}
};
if (iframe.contentDocument && iframe.contentDocument.readyState === "complete") {
send();
} else {
const once = () => { iframe.removeEventListener("load", once); send(); };
iframe.addEventListener("load", once, { once: true });
}
}
/* ---------- update (or append) ?sel param safely ---------- */
function setIframeSel(iframe, sel) {
if (!iframe || !sel) return;
try {
const u = new URL(iframe.src || chrome.runtime.getURL("panel.html"));
u.searchParams.set("sel", sel);
iframe.src = u.toString();
} catch {
// last-ditch: if URL constructor fails, just rebuild from base
const base = chrome.runtime.getURL("panel.html");
iframe.src = `${base}?sel=${encodeURIComponent(sel)}`;
}
}
/* ---------- ensure panel exists / open ---------- */
function ensurePanel(params = {}) {
let root = document.getElementById(WID);
if (root) {
const iframe = root.querySelector("iframe");
if (params.sel) {
// prefer messaging (no reload); if panel expects URL param, fallback to URL
postToPanel(iframe, { type: "OPEN_REPORT_MODAL", text: params.sel || "" });
setIframeSel(iframe, params.sel);
}
if (params.openReport) {
postToPanel(iframe, { type: "OPEN_REPORT_MODAL", text: params.sel || "" });
}
root.style.display = "block";
return root;
}
// shell
root = document.createElement("div");
root.id = WID;
Object.assign(root.style, {
position: "fixed",
zIndex: 2147483647,
top: "10px",
right: "10px",
width: "520px",
height: "680px",
borderRadius: "16px",
boxShadow: "0 12px 28px rgba(0,0,0,.25)",
overflow: "hidden",
background: "transparent"
});
// title bar
const bar = document.createElement("div");
Object.assign(bar.style, {
height: "36px",
background: "linear-gradient(135deg,#2fe273,#09742a)",
color: "#fff",
fontWeight: "800",
display: "flex",
alignItems: "center",
justifyContent: "space-between",
padding: "0 10px",
cursor: "move",
fontFamily: "ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu"
});
bar.textContent = "SMA - Subject Matter Agents";
const mk = (t, title) => {
const b = document.createElement("button");
Object.assign(b.style, { border: "0", borderRadius: "8px", width: "26px", height: "24px", fontWeight: "900", cursor: "pointer" });
b.textContent = t;
if (title) b.title = title;
return b;
};
const btns = document.createElement("div");
btns.style.display = "flex";
btns.style.gap = "8px";
const reportBtn = mk("R", "Open Report Modal (uses page selection)");
const minBtn = mk("—", "Minimize");
const closeBtn = mk("×", "Close");
btns.append(reportBtn, minBtn, closeBtn);
bar.appendChild(btns);
// iframe
const iframe = document.createElement("iframe");
const baseUrl = chrome.runtime.getURL("panel.html");
try {
const u = new URL(baseUrl);
if (params.sel) u.searchParams.set("sel", params.sel);
iframe.src = u.toString();
} catch {
iframe.src = params.sel ? `${baseUrl}?sel=${encodeURIComponent(params.sel)}` : baseUrl;
}
Object.assign(iframe.style, { width: "100%", height: "calc(100% - 36px)", border: "0" });
// drag
let drag = false, sx = 0, sy = 0, st = 0, sl = 0;
bar.addEventListener("mousedown", e => {
drag = true; sx = e.clientX; sy = e.clientY;
st = parseInt(root.style.top || "10", 10);
// switch to left-based positioning on first drag to avoid right/left fight
root.style.left = `${window.innerWidth - root.offsetWidth - 10}px`;
root.style.right = "";
sl = parseInt(root.style.left || "10", 10);
e.preventDefault();
});
document.addEventListener("mousemove", e => {
if (!drag) return;
root.style.top = (st + (e.clientY - sy)) + "px";
root.style.left = (sl + (e.clientX - sx)) + "px";
});
document.addEventListener("mouseup", () => { drag = false; });
// resize (bottom-right)
const rz = document.createElement("div");
Object.assign(rz.style, { position: "absolute", right: "0", bottom: "0", width: "16px", height: "16px", cursor: "nwse-resize", background: "rgba(0,0,0,.15)" });
let rs = false, rx = 0, ry = 0, rw = 0, rh = 0;
rz.addEventListener("mousedown", e => { rs = true; rx = e.clientX; ry = e.clientY; rw = root.offsetWidth; rh = root.offsetHeight; e.preventDefault(); });
document.addEventListener("mousemove", e => {
if (!rs) return;
root.style.width = Math.max(360, rw + (e.clientX - rx)) + "px";
root.style.height = Math.max(360, rh + (e.clientY - ry)) + "px";
});
document.addEventListener("mouseup", () => { rs = false; });
// buttons
let lastHeight = root.style.height;
minBtn.addEventListener("click", () => {
if (iframe.style.display === "none") {
iframe.style.display = "block";
root.style.height = lastHeight || "680px";
} else {
lastHeight = root.style.height;
iframe.style.display = "none";
root.style.height = "36px";
}
});
closeBtn.addEventListener("click", () => root.remove());
reportBtn.addEventListener("click", () => {
const sel = getSelectionText();
postToPanel(iframe, { type: "OPEN_REPORT_MODAL", text: sel || "" });
// also keep URL param in sync (non-breaking)
if (sel) setIframeSel(iframe, sel);
});
root.append(bar, iframe, rz);
document.documentElement.appendChild(root);
// optionally open modal immediately
if (params.openReport) {
postToPanel(iframe, { type: "OPEN_REPORT_MODAL", text: params.sel || "" });
}
return root;
}
/* ---------- hotkeys ---------- */
document.addEventListener("keydown", (e) => {
const isMac = navigator.platform.toLowerCase().includes("mac");
const meta = isMac ? e.metaKey : e.ctrlKey;
// open/toggle panel
if (meta && e.shiftKey && e.key.toLowerCase() === "y") {
ensurePanel();
}
// open Report Modal with selection
if (meta && e.shiftKey && e.key.toLowerCase() === "r") {
const sel = getSelectionText();
ensurePanel({ sel, openReport: true });
}
});
/* ---------- messages from background ---------- */
chrome.runtime.onMessage.addListener((msg) => {
if (msg?.type === "GPT5_OPEN_WITH_SELECTION") {
ensurePanel({ sel: (msg.text || "").trim() });
}
if (msg?.type === "GPT5_REPORT_FROM_SELECTION") {
ensurePanel({ sel: (msg.text || "").trim(), openReport: true });
}
});