-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
269 lines (247 loc) · 8.19 KB
/
popup.js
File metadata and controls
269 lines (247 loc) · 8.19 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
// Provide a minimal compatibility shim: if `browser` is missing, alias it to `chrome`.
if (typeof browser === "undefined" && typeof chrome !== "undefined") {
globalThis.browser = chrome;
}
async function findMatchesForUrl(url) {
const manifestUrl = browser.runtime.getURL("translators/manifest.json");
const resp = await fetch(manifestUrl);
const list = await resp.json();
const matches = [];
for (const entry of list) {
const target = (entry && entry.target) || "";
if (!target) continue;
try {
const re = new RegExp(target);
if (re.test(url)) matches.push(entry);
} catch (e) {
// ignore invalid regex patterns
console.warn("Invalid target regex", target, e);
}
}
return matches;
}
function renderResults(url, matches) {
// Log the URL
appendLog(`URL: ${url}`);
if (!matches || !matches.length) {
appendLog("No matching translators found.");
return;
}
}
async function ensureOffscreen() {
if (!browser.offscreen) return false;
const has = await browser.offscreen.hasDocument();
if (has) return true;
try {
await browser.offscreen.createDocument({
url: browser.runtime.getURL("offscreen.html"),
reasons: ["DOM_PARSER"],
justification: "Run translators offscreen",
});
return true;
} catch (e) {
console.warn("Failed to create offscreen document", e);
return false;
}
}
async function runTranslatorOffscreen(translatorPaths, url) {
await ensureOffscreen();
const payload = { type: "runTranslator", translators: translatorPaths, url };
try {
appendLog(`Requesting translator run for ${url}`, "info");
const resp = await browser.runtime.sendMessage(payload);
if (resp && resp.ok) appendLog("Background acknowledged run request", "info");
else appendLog(`Background error: ${resp && resp.error ? resp.error : "unknown"}`, "error");
} catch (e) {
console.error("Failed to send runTranslator message", e);
}
}
// Listen for offscreen results
browser.runtime.onMessage.addListener((msg) => {
if (!msg || msg.type !== "offscreenResult") return;
const bib = document.getElementById("bibEntry");
const error = msg.error;
const result = msg.result;
if (error) {
appendLog(`Error: ${error}`);
return;
}
appendLog(`Received result for ${msg.url}`);
if (bib) {
if (typeof result === "string") bib.value = result;
else bib.value = JSON.stringify(result, null, 2);
// Send to JabRef automatically
sendBibEntry();
} else {
appendLog(
typeof result === "string" ? result : JSON.stringify(result, null, 2),
);
}
});
function appendLog(text) {
const log = document.getElementById("log");
if (!log) return;
const d = document.createElement("div");
d.className = "log-line";
// Convert URLs in the text into clickable links
// Split the text keeping URLs (captures https?://...)
const parts = text.split(/(https?:\/\/(docs.jabref.org|github.com)[^\s]+)/);
for (const part of parts) {
if (!part) continue;
if (part.startsWith("http://") || part.startsWith("https://")) {
const a = document.createElement("a");
a.href = part;
a.textContent = part;
a.target = "_blank";
a.rel = "noopener noreferrer";
d.appendChild(a);
} else {
d.appendChild(document.createTextNode(part));
}
}
log.appendChild(d);
log.scrollTop = log.scrollHeight;
}
// Update connection status
function updateStatus(status, className) {
const statusEl = document.getElementById("status");
statusEl.textContent = status;
statusEl.className = `status-${className}`;
}
// Connect to JabRef via HTTP POST
let jabrefBaseUrl = null;
async function getBaseUrl() {
const res = await browser.storage.local.get({ jabrefPort: 23119 });
const port = res.jabrefPort || 23119;
return `http://localhost:${port}/`;
}
async function connectToJabRef() {
const base = await getBaseUrl();
if (!base) {
appendLog("No JabRef base URL configured", "error");
return;
}
jabrefBaseUrl = base;
appendLog(`Checking JabRef at ${base}...`, "info");
try {
// Try a simple GET to the base URL to detect availability.
const resp = await fetch(base, { method: "GET", cache: "no-store" });
if (resp && (resp.ok || resp.status === 404)) {
appendLog("JabRef reachable (HTTP)", "success");
updateStatus("Connected", "connected");
} else {
appendLog(`JabRef responded with status ${resp.status}`, "warning");
updateStatus("Connected (no OK)", "connected");
}
} catch (error) {
appendLog(
`Connection failed: ${error && error.message ? error.message : error}`,
"error",
);
console.error("HTTP connection error:", error);
updateStatus("Disconnected", "disconnected");
jabrefBaseUrl = null;
return false;
}
return true;
}
// Send BibTeX entry to JabRef
async function sendBibEntry() {
const bibEntryTextarea = document.getElementById("bibEntry");
const bibEntry = bibEntryTextarea.value.trim();
if (!bibEntry) {
appendLog("BibTeX entry is empty", "error");
return;
}
const base = jabrefBaseUrl || (await getBaseUrl());
if (!base) {
appendLog("JabRef base URL not configured", "error");
return;
}
const url = base + "libraries/current/entries";
appendLog(`Sending BibTeX entry to JabRef at ${url}...`, "info");
if (!bibEntry.startsWith("@")) {
appendLog("BibTeX entry does not start with '@'", "error");
return;
}
try {
console.log("Sending to JabRef (HTTP POST):", bibEntry);
const resp = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/x-bibtex" },
body: bibEntry,
});
if (resp.ok) {
appendLog("BibTeX entry sent successfully!", "success");
appendLog(`Sent: ${bibEntry.substring(0, 50)}...`, "info");
} else {
let text;
try {
text = await resp.text();
} catch (e) {
text = String(e);
}
appendLog(`Failed to send (HTTP ${resp.status}): ${text}`, "error");
console.error("HTTP send failed", resp.status, text);
}
} catch (error) {
appendLog(
`Failed to send: ${error && error.message ? error.message : error}`,
"error",
);
console.error("Send error:", error);
}
}
document.addEventListener("DOMContentLoaded", async () => {
// Auto-connect when popup opens
const connected = await connectToJabRef();
const urlEl = document.getElementById("url");
if (!connected) {
appendLog(
"If JabRef is running, enable the HTTP server (Options → Preferences → Advanced → Remote operation → enable) and ensure the configured port is correct: https://github.com/JabRef/JabRef-Browser-Extension/blob/main/SETUP.md",
"warning",
);
document.getElementById("log-box").open = true;
// show fallback UI
const noneEl = document.getElementById("none");
if (noneEl) noneEl.style.display = "block";
return;
}
try {
if (!window.browser || !browser.tabs) {
urlEl.textContent = "Extension APIs not available.";
document.getElementById("none").style.display = "block";
return;
}
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
const tab = tabs && tabs[0];
const url = tab && tab.url ? tab.url : "";
if (!url) {
urlEl.textContent = "Unable to determine active tab URL.";
document.getElementById("none").style.display = "block";
return;
}
let matches;
try {
matches = await findMatchesForUrl(url);
} catch (e) {
console.error("Error fetching translators manifest", e);
urlEl.textContent =
"Error reading translators manifest: " +
(e && e.message ? e.message : String(e));
document.getElementById("none").style.display = "block";
return;
}
renderResults(url, matches || []);
if (matches && matches.length) {
// Build array of translator paths and request background/offscreen
const translatorPaths = matches.map((m) => m.path || "");
runTranslatorOffscreen(translatorPaths, url);
}
} catch (e) {
console.error("Popup initialization error", e);
urlEl.textContent =
"Popup error: " + (e && e.message ? e.message : String(e));
document.getElementById("none").style.display = "block";
}
});