|
| 1 | +import rules from '@/public/rules/figma.json' |
| 2 | +import { applyGroups, RULES_URL, REWRITE_RULE_ID } from '@/rewrite/shared' |
| 3 | +import { GROUPS } from '@/rewrite/config' |
| 4 | +import type { BlobHandle, CacheEntry, Rules } from './types' |
| 5 | + |
| 6 | +export default defineContentScript({ |
| 7 | + matches: [ |
| 8 | + 'https://www.figma.com/file/*', |
| 9 | + 'https://www.figma.com/design/*', |
| 10 | + 'https://www.figma.com/deck/*', |
| 11 | + 'https://www.figma.com/proto/*' |
| 12 | + ], |
| 13 | + runAt: 'document_start', |
| 14 | + world: 'MAIN', |
| 15 | + main() { |
| 16 | + function extractRegexFilter(source: Rules): RegExp | null { |
| 17 | + try { |
| 18 | + const rule = source.find((r) => r.id === REWRITE_RULE_ID) |
| 19 | + return rule?.condition?.regexFilter ? new RegExp(rule.condition.regexFilter, 'i') : null |
| 20 | + } catch { |
| 21 | + return null |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + async function loadRemoteRegex(url: string): Promise<RegExp | null> { |
| 26 | + try { |
| 27 | + const resp = await fetch(url, { credentials: 'omit', cache: 'no-cache' }) |
| 28 | + if (!resp.ok) { |
| 29 | + return null |
| 30 | + } |
| 31 | + return extractRegexFilter(await resp.json()) |
| 32 | + } catch { |
| 33 | + return null |
| 34 | + } |
| 35 | + } |
| 36 | + let targetRegex: RegExp | null = extractRegexFilter(rules as Rules) |
| 37 | + |
| 38 | + loadRemoteRegex(RULES_URL).then((remoteRegex) => { |
| 39 | + if (remoteRegex) { |
| 40 | + targetRegex = remoteRegex |
| 41 | + console.log('[tempad-dev] Loaded remote rewrite rules.') |
| 42 | + } else { |
| 43 | + console.warn('[tempad-dev] Failed to fetch rewrite rules; using bundled rules.') |
| 44 | + } |
| 45 | + }) |
| 46 | + |
| 47 | + const { appendChild, insertBefore } = Element.prototype |
| 48 | + |
| 49 | + const processedScripts = new WeakSet<HTMLScriptElement>() |
| 50 | + |
| 51 | + const blobCache = new Map<string, CacheEntry>() |
| 52 | + |
| 53 | + function shouldRewrite(url: string): boolean { |
| 54 | + return !!targetRegex && targetRegex.test(url) |
| 55 | + } |
| 56 | + |
| 57 | + function isRewritableScript(node: Node): node is HTMLScriptElement { |
| 58 | + return ( |
| 59 | + node instanceof HTMLScriptElement && typeof node.src === 'string' && node.src.length > 0 |
| 60 | + ) |
| 61 | + } |
| 62 | + |
| 63 | + function releaseBlobUrl(src: string, usedUrl: string): void { |
| 64 | + const entry = blobCache.get(src) |
| 65 | + if (!entry || entry.url !== usedUrl) return |
| 66 | + entry.ref -= 1 |
| 67 | + if (entry.ref <= 0) { |
| 68 | + try { |
| 69 | + URL.revokeObjectURL(entry.url) |
| 70 | + } catch {} |
| 71 | + blobCache.delete(src) |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + async function acquireBlobUrl(src: string): Promise<BlobHandle> { |
| 76 | + const existing = blobCache.get(src) |
| 77 | + if (existing) { |
| 78 | + existing.ref += 1 |
| 79 | + return { url: existing.url, release: () => releaseBlobUrl(src, existing.url) } |
| 80 | + } |
| 81 | + |
| 82 | + const resp = await fetch(src, { credentials: 'include', cache: 'force-cache' }) |
| 83 | + const originalText = await resp.text() |
| 84 | + const { content, changed } = applyGroups(originalText, GROUPS) |
| 85 | + |
| 86 | + if (changed) { |
| 87 | + console.log(`[tempad-dev] Rewrote async script: ${src}`) |
| 88 | + } |
| 89 | + |
| 90 | + const blob = new Blob([content], { type: 'application/javascript; charset=utf-8' }) |
| 91 | + const url = URL.createObjectURL(blob) |
| 92 | + blobCache.set(src, { url, ref: 1 }) |
| 93 | + return { url, release: () => releaseBlobUrl(src, url) } |
| 94 | + } |
| 95 | + |
| 96 | + function normalizedInsert(parent: Element, node: Node, before: Node | null): void { |
| 97 | + if (before) insertBefore.call(parent, node, before) |
| 98 | + else appendChild.call(parent, node) |
| 99 | + } |
| 100 | + |
| 101 | + async function rewriteAndInsert( |
| 102 | + parent: Element, |
| 103 | + script: HTMLScriptElement, |
| 104 | + before: Node | null |
| 105 | + ): Promise<void> { |
| 106 | + if (processedScripts.has(script)) { |
| 107 | + normalizedInsert(parent, script, before) |
| 108 | + return |
| 109 | + } |
| 110 | + processedScripts.add(script) |
| 111 | + |
| 112 | + if (!shouldRewrite(script.src)) { |
| 113 | + normalizedInsert(parent, script, before) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + try { |
| 118 | + const { url, release } = await acquireBlobUrl(script.src) |
| 119 | + script.removeAttribute('integrity') |
| 120 | + script.addEventListener('load', release, { once: true }) |
| 121 | + script.addEventListener('error', release, { once: true }) |
| 122 | + script.src = url |
| 123 | + } catch {} |
| 124 | + |
| 125 | + normalizedInsert(parent, script, before) |
| 126 | + } |
| 127 | + |
| 128 | + const newAppendChild: typeof Element.prototype.appendChild = function <T extends Node>( |
| 129 | + this: Element, |
| 130 | + node: T |
| 131 | + ): T { |
| 132 | + if (isRewritableScript(node)) { |
| 133 | + void rewriteAndInsert(this, node, null) |
| 134 | + return node |
| 135 | + } |
| 136 | + appendChild.call(this, node) |
| 137 | + return node |
| 138 | + } |
| 139 | + |
| 140 | + const newInsertBefore: typeof Element.prototype.insertBefore = function <T extends Node>( |
| 141 | + this: Element, |
| 142 | + node: T, |
| 143 | + before: Node | null |
| 144 | + ): T { |
| 145 | + if (isRewritableScript(node)) { |
| 146 | + void rewriteAndInsert(this, node, before) |
| 147 | + return node |
| 148 | + } |
| 149 | + insertBefore.call(this, node, before) |
| 150 | + return node |
| 151 | + } |
| 152 | + |
| 153 | + Element.prototype.appendChild = newAppendChild |
| 154 | + Element.prototype.insertBefore = newInsertBefore |
| 155 | + } |
| 156 | +}) |
0 commit comments