-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
325 lines (297 loc) · 10.9 KB
/
background.js
File metadata and controls
325 lines (297 loc) · 10.9 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
// background.js
// Create the context menu item when the extension is installed
chrome.runtime.onInstalled.addListener(() => {
chrome.contextMenus.create({
id: "open-in-drawio",
title: "Open in Draw.io",
contexts: ["selection"],
documentUrlPatterns: [
"https://gemini.google.com/*",
"https://aistudio.google.com/*",
"https://chatgpt.com/*",
"https://claude.ai/*",
"https://chat.deepseek.com/*",
"https://www.perplexity.ai/*",
"https://aistudio.xiaomimimo.com/*",
"https://hunyuan.tencent.com/*",
"https://www.qianwen.com/*",
"https://chat.qwen.ai/*",
"https://ima.qq.com/*",
"https://www.doubao.com/*",
"https://www.kimi.com/*",
"https://chatglm.cn/*"
]
});
});
// Handle messages from content script
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'open_drawio') {
// Process the diagram asynchronously
processDiagram(request.content || request.xml, request.type || 'xml')
.then(() => {
sendResponse({ success: true });
})
.catch((error) => {
console.error('Error in processDiagram:', error);
sendResponse({ success: false, error: error.message });
});
return true; // CRITICAL: Keep the message channel open for async response
}
// Handle other message types
return false;
});
// Handle the context menu click
chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "open-in-drawio") {
// Instead of using info.selectionText directly, ask the content script for the context
// This allows us to get the full code block content if the user selected part of it
if (tab && tab.id) {
chrome.tabs.sendMessage(tab.id, { action: 'get_selection_context' }, (response) => {
if (chrome.runtime.lastError) {
// Fallback if content script is not ready or error
console.warn("Content script error:", chrome.runtime.lastError);
if (info.selectionText) {
const type = detectTypeFromText(info.selectionText);
processDiagram(info.selectionText, type);
}
} else if (response && response.content) {
processDiagram(response.content, response.type || 'xml');
} else if (info.selectionText) {
// Fallback if no response content
const type = detectTypeFromText(info.selectionText);
processDiagram(info.selectionText, type);
}
});
}
}
});
function detectTypeFromText(text) {
if (!text) return 'xml';
// Mermaid keywords
if (/^\s*(graph|flowchart|sequenceDiagram|classDiagram|stateDiagram|erDiagram|gantt|pie|gitGraph)/.test(text)) {
return 'mermaid';
}
// XML check
if (text.includes('</mxfile>') || text.includes('</mxGraphModel>')) {
return 'xml';
}
// Default to XML if unsure (or maybe we should default to nothing? But existing behavior was XML)
return 'xml';
}
/**
* Processes the content: opens Draw.io with XML or Mermaid
* @param {string} content
* @param {string} type 'xml' or 'mermaid'
*/
async function processDiagram(content, type) {
try {
if (!content) return;
if (type === 'mermaid') {
// Mermaid: Use 'create' URL parameter with JSON
// https://app.diagrams.net/?create={type:'mermaid',data:'...'}
const config = {
type: 'mermaid',
data: content
};
const url = `https://app.diagrams.net/?create=${encodeURIComponent(JSON.stringify(config))}`;
chrome.tabs.create({ url: url });
} else {
// XML: Use #R compression
const xml = content.trim();
// 1. Sanitize
const sanitizedXml = sanitizeXml(xml);
// 2. Compress the XML using deflate-raw
const compressed = await compressData(sanitizedXml);
// 3. Convert to Base64
const base64 = arrayBufferToBase64(compressed);
// 4. URL Encode
// Draw.io #R format expects standard Base64 (deflate-raw).
const url = `https://app.diagrams.net/#R${base64}`;
chrome.tabs.create({ url: url });
}
} catch (error) {
console.error("Error processing Draw.io diagram:", error);
}
}
/**
* Compresses a string using Deflate (Raw) format.
* @param {string} str
* @returns {Promise<ArrayBuffer>}
*/
async function compressData(str) {
const stream = new Blob([str]).stream();
const compressedStream = stream.pipeThrough(new CompressionStream("deflate-raw"));
return await new Response(compressedStream).arrayBuffer();
}
/**
* Converts an ArrayBuffer to a Base64 string.
* @param {ArrayBuffer} buffer
* @returns {string}
*/
function arrayBufferToBase64(buffer) {
let binary = '';
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
}
/**
* Sanitizes the XML string by escaping special characters in attributes and text content.
* @param {string} xml
* @returns {string}
*/
function sanitizeXml(xml) {
// Detect if the ampersand already starts a valid entity so we avoid double-escaping.
function isPreEscapedEntity(source, index) {
if (source[index] !== '&') return false;
const semi = source.indexOf(';', index + 1);
if (semi === -1) return false;
const entityBody = source.slice(index + 1, semi);
if (!entityBody) return false;
return /^#x[0-9A-Fa-f]+$/.test(entityBody) ||
/^#\d+$/.test(entityBody) ||
/^[a-zA-Z][a-zA-Z0-9]+$/.test(entityBody);
}
// Determine if a quote character is likely the end of an attribute value.
function isAttributeTerminator(nextChar) {
return nextChar === undefined ||
nextChar === '' ||
nextChar === '>' ||
nextChar === '/' ||
nextChar === '?' ||
/\s/.test(nextChar);
}
let result = '';
let i = 0;
const len = xml.length;
// States
const STATE_TEXT = 0;
const STATE_TAG_OPEN = 1; // After <
const STATE_TAG_NAME = 2; // Inside <tagName ...
const STATE_ATTR_NAME = 3; // Inside <tagName attr=...
const STATE_ATTR_VALUE_Q = 4; // Inside <tagName attr="..."
const STATE_ATTR_VALUE_DQ = 5; // Inside <tagName attr='...'
const STATE_COMMENT = 6; // Inside <!-- ... -->
const STATE_CDATA = 7; // Inside <![CDATA[ ... ]]>
let state = STATE_TEXT;
while (i < len) {
const char = xml[i];
if (state === STATE_TEXT) {
if (char === '<') {
// Check for CDATA
if (xml.startsWith('<![CDATA[', i)) {
state = STATE_CDATA;
result += '<![CDATA[';
i += 9;
continue;
}
// Check for Comment
if (xml.startsWith('<!--', i)) {
state = STATE_COMMENT;
result += '<!--';
i += 4;
continue;
}
// Check if it's a valid tag start (alpha or / or ? or !)
// A loose check: next char should be a-z, A-Z, _, :, /, ?, !
const nextChar = i + 1 < len ? xml[i + 1] : '';
if (/[a-zA-Z0-9_:\/\?!]/.test(nextChar)) {
state = STATE_TAG_OPEN;
result += char;
} else {
// It's a loose < in text, escape it
result += '<';
}
} else if (char === '&') {
result += isPreEscapedEntity(xml, i) ? '&' : '&';
} else if (char === '>') {
// Loose > in text, escape it
result += '>';
} else {
result += char;
}
}
else if (state === STATE_TAG_OPEN) {
result += char;
if (/\s/.test(char)) {
state = STATE_TAG_NAME;
} else if (char === '>') {
state = STATE_TEXT;
} else {
state = STATE_TAG_NAME;
}
}
else if (state === STATE_TAG_NAME) {
result += char;
if (/\s/.test(char)) {
state = STATE_ATTR_NAME;
} else if (char === '>') {
state = STATE_TEXT;
}
}
else if (state === STATE_ATTR_NAME) {
result += char;
if (char === '"') {
state = STATE_ATTR_VALUE_DQ;
} else if (char === "'") {
state = STATE_ATTR_VALUE_Q;
} else if (char === '>') {
state = STATE_TEXT;
}
}
else if (state === STATE_ATTR_VALUE_DQ) {
if (char === '"') {
const nextChar = i + 1 < len ? xml[i + 1] : '';
if (isAttributeTerminator(nextChar)) {
state = STATE_ATTR_NAME;
result += char;
} else {
result += '"';
}
} else if (char === '&') {
result += isPreEscapedEntity(xml, i) ? '&' : '&';
} else if (char === '<') {
result += '<';
} else if (char === '>') {
result += '>';
} else {
result += char;
}
}
else if (state === STATE_ATTR_VALUE_Q) {
if (char === "'") {
const nextChar = i + 1 < len ? xml[i + 1] : '';
if (isAttributeTerminator(nextChar)) {
state = STATE_ATTR_NAME;
result += char;
} else {
result += ''';
}
} else if (char === '&') {
result += isPreEscapedEntity(xml, i) ? '&' : '&';
} else if (char === '<') {
result += '<';
} else if (char === '>') {
result += '>';
} else {
result += char;
}
}
else if (state === STATE_COMMENT) {
result += char;
if (char === '>' && result.endsWith('-->')) {
state = STATE_TEXT;
}
}
else if (state === STATE_CDATA) {
result += char;
if (char === '>' && result.endsWith(']]>')) {
state = STATE_TEXT;
}
}
i++;
}
return result;
}