-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
62 lines (54 loc) · 1.97 KB
/
Copy pathbackground.js
File metadata and controls
62 lines (54 loc) · 1.97 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
// Background service worker — injects scripts and bootstraps figma reference
// chrome.scripting.executeScript with world: 'MAIN' + files runs in extension scope.
// The inline func form runs in the TRUE page context with figma access.
// So: inject scripts via files, then bootstrap __figb.f = figma via func.
const injectedTabs = new Set();
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete') return;
if (!tab.url || !/figma\.com\/(file|design|board|proto)/.test(tab.url)) return;
if (injectedTabs.has(tabId)) return;
injectedTabs.add(tabId);
setTimeout(async () => {
try {
const existingTab = await chrome.tabs.get(tabId).catch(() => null);
if (!existingTab) {
injectedTabs.delete(tabId);
return;
}
await chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
files: ['figma-bridge.js'],
});
await chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
files: ['figma-status.js'],
});
// Bootstrap: pass real figma reference (inline func runs in true page context)
await chrome.scripting.executeScript({
target: { tabId },
world: 'MAIN',
func: () => {
if (typeof figma !== 'undefined' && window.__figb) {
window.__figb.f = figma;
console.log('[Figma Bridge] v' + window.__figb.version + ' — ready');
} else {
console.warn('[Figma Bridge] figma not available yet');
}
},
});
} catch (e) {
console.error('[Figma Bridge] injection failed:', e);
injectedTabs.delete(tabId);
}
}, 3000);
});
// Clear tracking when tab navigates to a new page
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (changeInfo.url) injectedTabs.delete(tabId);
});
// Clear tracking when tab is closed
chrome.tabs.onRemoved.addListener((tabId) => {
injectedTabs.delete(tabId);
});