-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathservice-worker.js
More file actions
152 lines (130 loc) · 3.96 KB
/
service-worker.js
File metadata and controls
152 lines (130 loc) · 3.96 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
import { normalizeExtractedStyles } from "./lib/normalize.mjs";
import { generateDesignMarkdown } from "./lib/generate-design-md.mjs";
import { generateSkillMarkdown } from "./lib/generate-skill-md.mjs";
import { validateMarkdownOutput } from "./lib/validate.mjs";
const EXTRACTION_MESSAGE = "TYPEUI_EXTRACT_STYLES";
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({
outputMode: "design"
});
});
chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
if (!message || !message.type) {
return;
}
if (message.type === "RUN_EXTRACTION") {
handleExtraction(message)
.then((result) => sendResponse({ ok: true, ...result }))
.catch((error) => sendResponse({ ok: false, error: stringifyError(error) }));
return true;
}
if (message.type === "DOWNLOAD_MARKDOWN") {
handleDownload(message)
.then((downloadId) => sendResponse({ ok: true, downloadId }))
.catch((error) => sendResponse({ ok: false, error: stringifyError(error) }));
return true;
}
});
async function handleExtraction(message) {
const mode = message.mode === "skill" ? "skill" : "design";
const tab = await getActiveTab();
await injectExtractor(tab.id);
const payload = await requestExtractionPayload(tab.id);
const normalized = normalizeExtractedStyles(payload);
const context = {
normalized
};
const markdown =
mode === "skill"
? generateSkillMarkdown(context)
: generateDesignMarkdown(context);
const validation = validateMarkdownOutput(mode, markdown);
const filename = mode === "skill" ? "SKILL.md" : "DESIGN.md";
if (message.persistOutputMode !== false) {
await chrome.storage.local.set({
outputMode: mode
});
}
return {
mode,
filename,
markdown,
normalized,
validation
};
}
async function handleDownload(message) {
if (!message.markdown) {
throw new Error("Cannot download empty markdown.");
}
const filename = normalizeMarkdownFilename(message.filename, message.mode);
const url = `data:text/markdown;charset=utf-8,${encodeURIComponent(message.markdown)}`;
return chrome.downloads.download({
url,
filename,
saveAs: true,
conflictAction: "uniquify"
});
}
async function getActiveTab() {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true
});
if (!tab || !tab.id) {
throw new Error("No active tab available.");
}
if (String(tab.url || "").startsWith("chrome://")) {
throw new Error("Extraction is not available on chrome:// pages.");
}
return tab;
}
async function injectExtractor(tabId) {
await chrome.scripting.executeScript({
target: { tabId },
files: ["content-script.js"]
});
}
function requestExtractionPayload(tabId) {
return new Promise((resolve, reject) => {
chrome.tabs.sendMessage(tabId, { type: EXTRACTION_MESSAGE }, (response) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
if (!response || !response.ok) {
reject(new Error(response?.error || "No extraction response from tab."));
return;
}
resolve(response.payload);
});
});
}
function stringifyError(error) {
if (error instanceof Error) {
return error.message;
}
return String(error || "Unknown error");
}
function normalizeMarkdownFilename(inputName, mode) {
const normalizedMode = mode === "skill" ? "skill" : "design";
const fallback = normalizedMode === "skill" ? "SKILL.md" : "DESIGN.md";
const raw = String(inputName || "").trim();
if (!raw) {
return fallback;
}
const name = raw.replace(/[\\/]/g, "").trim();
if (!name) {
return fallback;
}
if (normalizedMode === "skill") {
if (/^skill(\.md)?$/i.test(name)) {
return "SKILL.md";
}
return name.toLowerCase().endsWith(".md") ? name : `${name}.md`;
}
if (/^design(\.md)?$/i.test(name)) {
return "DESIGN.md";
}
return name.toLowerCase().endsWith(".md") ? name : `${name}.md`;
}