-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject-styles.js
More file actions
193 lines (170 loc) · 6.56 KB
/
inject-styles.js
File metadata and controls
193 lines (170 loc) · 6.56 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
/* StyleCraft v1.5.0 — Style Injector (document_start) */
(function() {
if (window.__stylecraft_injected) return;
window.__stylecraft_injected = true;
const THEME_ID = 'stylecraft-theme-styles';
const CUSTOM_ID = 'stylecraft-custom-styles';
const PREVIEW_ID = 'stylecraft-preview-styles';
function ensureEl(id) {
let el = document.getElementById(id);
if (!el) {
el = document.createElement('style');
el.id = id;
(document.head || document.documentElement).appendChild(el);
}
return el;
}
function getDomain() {
try { return new URL(location.href).hostname; } catch { return location.hostname; }
}
function domainMatches(pageDomain, storedKey) {
if (!storedKey) return false;
if (storedKey.includes(',')) return storedKey.split(',').map(p => p.trim()).some(p => domainMatches(pageDomain, p));
if (storedKey.includes('*')) {
const re = new RegExp('^' + storedKey.replace(/[.+?{}|()[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$');
return re.test(pageDomain);
}
return pageDomain === storedKey || pageDomain.endsWith('.' + storedKey);
}
/* Advanced URL pattern matching via appliesTo array */
function entryMatchesPage(storedKey, data, pageDomain, pageUrl) {
const patterns = data.appliesTo;
if (!patterns || !patterns.length) return domainMatches(pageDomain, storedKey);
for (const p of patterns) {
if (patternMatchesUrl(p, pageDomain, pageUrl)) return true;
}
return false;
}
function patternMatchesUrl(p, pageDomain, pageUrl) {
if (!p || !p.value) return false;
const v = p.value;
switch (p.type) {
case 'domain':
return pageDomain === v || pageDomain.endsWith('.' + v);
case 'url':
return pageUrl === v;
case 'url-prefix':
return pageUrl.startsWith(v);
case 'regexp':
try { return new RegExp(v).test(pageUrl); } catch { return false; }
case 'wildcard': {
if (v.includes('://') || v.includes('/')) {
const re = new RegExp('^' + v.replace(/[.+?{}|()[\]\\]/g, '\\$&').replace(/\*/g, '.*') + '$');
return re.test(pageUrl);
}
return domainMatches(pageDomain, v);
}
default:
return domainMatches(pageDomain, v);
}
}
/* Read directly from storage — no service worker needed */
function applyFromStorage() {
chrome.storage.local.get(['stylecraft_data', 'stylecraft_settings'], (result) => {
if (chrome.runtime.lastError) {
setTimeout(() => chrome.storage.local.get(['stylecraft_data', 'stylecraft_settings'], applyData), 200);
return;
}
applyData(result);
});
}
function applyData(result) {
const allData = (result && result.stylecraft_data) || {};
const settings = (result && result.stylecraft_settings) || {};
const pageDomain = getDomain();
const pageUrl = location.href;
let themeCSS = '', customCSS = '', customEnabled = true;
let foundCustom = false;
// Find matching domain entry
for (const [pattern, data] of Object.entries(allData)) {
if (entryMatchesPage(pattern, data, pageDomain, pageUrl)) {
for (const [id, theme] of Object.entries(data.themes || {})) {
if (theme.enabled !== false) {
const raw = theme.rawCSS || theme.css || '';
const resolved = simpleResolve(raw, pageUrl, pageDomain);
if (resolved.trim()) themeCSS += (themeCSS ? '\n' : '') + resolved;
}
}
if (!foundCustom && data.customCSS) {
customCSS = data.customCSS;
customEnabled = data.customEnabled !== false;
foundCustom = true;
}
}
}
const globalCSS = settings.globalCSS || '';
const themeEl = ensureEl(THEME_ID);
themeEl.textContent = (globalCSS ? globalCSS + '\n' : '') + themeCSS;
const customEl = ensureEl(CUSTOM_ID);
customEl.textContent = (customCSS && customEnabled) ? customCSS : '';
const parent = document.head || document.documentElement;
if (themeEl.nextSibling !== customEl) {
parent.appendChild(themeEl);
parent.appendChild(customEl);
}
}
/* Lightweight @-moz-document resolver */
function simpleResolve(raw, pageUrl, pageDomain) {
if (!raw || !raw.trim()) return '';
if (!/@(-moz-)?document\b/.test(raw)) return raw;
let out = '';
const re = /@(?:-moz-)?document\s+([^{]+)\{/g;
let match, lastEnd = 0;
while ((match = re.exec(raw)) !== null) {
// Plain CSS before this block
const plain = raw.substring(lastEnd, match.index).trim();
if (plain) out += (out ? '\n' : '') + plain;
const condStr = match[1].trim();
const bodyStart = match.index + match[0].length;
let depth = 1, i = bodyStart;
while (i < raw.length && depth > 0) {
if (raw[i] === '{') depth++;
else if (raw[i] === '}') depth--;
i++;
}
const body = raw.substring(bodyStart, i - 1).trim();
lastEnd = i;
re.lastIndex = i;
if (matchConditions(condStr, pageUrl, pageDomain) && body) {
out += (out ? '\n' : '') + body;
}
}
// Trailing plain CSS
const trail = raw.substring(lastEnd).trim();
if (trail) out += (out ? '\n' : '') + trail;
return out;
}
function matchConditions(condStr, pageUrl, pageDomain) {
const conds = condStr.match(/(domain|url|url-prefix|regexp)\s*\(["']?([^"')]+)["']?\)/g);
if (!conds || conds.length === 0) return true;
for (const c of conds) {
const m = c.match(/(domain|url|url-prefix|regexp)\s*\(["']?([^"')]+)["']?\)/);
if (!m) continue;
const [, fn, val] = m;
if (fn === 'domain' && (pageDomain === val || pageDomain.endsWith('.' + val))) return true;
if (fn === 'url' && pageUrl === val) return true;
if (fn === 'url-prefix' && pageUrl.startsWith(val)) return true;
if (fn === 'regexp') { try { if (new RegExp(val).test(pageUrl)) return true; } catch {} }
}
return false;
}
/* Init */
applyFromStorage();
/* Re-apply once head is available */
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', applyFromStorage, { once: true });
}
/* Listen for live updates */
chrome.runtime.onMessage.addListener((msg) => {
if (msg.action === 'sc-styles-updated') applyFromStorage();
if (msg.action === 'sc-apply-preview') {
const el = ensureEl(PREVIEW_ID);
el.textContent = msg.css || '';
(document.head || document.documentElement).appendChild(el);
}
if (msg.action === 'sc-end-preview') {
const el = document.getElementById(PREVIEW_ID);
if (el) el.remove();
}
});
})();