-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbrowser-extract.js
More file actions
327 lines (281 loc) · 9.21 KB
/
browser-extract.js
File metadata and controls
327 lines (281 loc) · 9.21 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
326
327
/**
* Browser Console Script: Extract Page to JSON for Figma
*
* USAGE:
* 1. Open any website in your browser
* 2. Open DevTools Console (F12 or Cmd+Option+I)
* 3. Paste this entire script and press Enter
* 4. Copy the generated JSON
* 5. Paste into the Figma plugin
*
* This script captures the visual structure of the page with accurate:
* - Positions and dimensions
* - Colors and backgrounds
* - Text content and styles
* - Borders, shadows, and effects
* - Layout properties (flexbox, grid)
*/
(function() {
console.log('🎨 Starting page extraction for Figma...');
/**
* Get computed styles for an element with all relevant CSS properties
*/
function getRelevantStyles(element, computed) {
const styles = {};
// Layout
styles.display = computed.display;
styles.position = computed.position;
styles.flexDirection = computed.flexDirection;
styles.justifyContent = computed.justifyContent;
styles.alignItems = computed.alignItems;
styles.gap = computed.gap;
styles.gridGap = computed.gridGap;
// Colors
styles.color = computed.color;
styles.backgroundColor = computed.backgroundColor;
// Typography
styles.fontFamily = computed.fontFamily;
styles.fontSize = computed.fontSize;
styles.fontWeight = computed.fontWeight;
styles.lineHeight = computed.lineHeight;
styles.letterSpacing = computed.letterSpacing;
styles.textAlign = computed.textAlign;
styles.textDecoration = computed.textDecoration;
styles.textTransform = computed.textTransform;
styles.textShadow = computed.textShadow;
styles.whiteSpace = computed.whiteSpace;
// Borders and radius
styles.borderWidth = computed.borderWidth;
styles.borderColor = computed.borderColor;
styles.borderStyle = computed.borderStyle;
styles.borderRadius = computed.borderRadius;
// Effects
styles.boxShadow = computed.boxShadow;
styles.opacity = computed.opacity;
styles.visibility = computed.visibility;
// Spacing
styles.padding = computed.padding;
styles.margin = computed.margin;
styles.paddingTop = computed.paddingTop;
styles.paddingRight = computed.paddingRight;
styles.paddingBottom = computed.paddingBottom;
styles.paddingLeft = computed.paddingLeft;
// Overflow
styles.overflow = computed.overflow;
styles.overflowX = computed.overflowX;
styles.overflowY = computed.overflowY;
// Z-index
styles.zIndex = computed.zIndex;
// Object fit for images
if (element.tagName === 'IMG') {
styles.objectFit = computed.objectFit;
}
// Remove default/inherited values that don't affect layout
Object.keys(styles).forEach(key => {
if (styles[key] === 'none' ||
styles[key] === 'normal' ||
styles[key] === 'auto' ||
styles[key] === 'static' ||
styles[key] === 'rgba(0, 0, 0, 0)' ||
styles[key] === '') {
delete styles[key];
}
});
return styles;
}
/**
* Check if element should be captured
*/
function shouldCapture(element) {
if (!element || !(element instanceof Element)) return false;
// Skip script, style, noscript tags
const skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'META', 'LINK', 'HEAD'];
if (skipTags.includes(element.tagName)) return false;
// Skip hidden elements
const computed = window.getComputedStyle(element);
if (computed.display === 'none' ||
computed.visibility === 'hidden' ||
computed.opacity === '0') {
return false;
}
// Skip elements with no size
const rect = element.getBoundingClientRect();
if (rect.width === 0 || rect.height === 0) {
return false;
}
return true;
}
/**
* Get text content from element (excluding children)
*/
function getDirectTextContent(element) {
let text = '';
for (let node of element.childNodes) {
if (node.nodeType === Node.TEXT_NODE) {
text += node.textContent;
}
}
return text.trim();
}
/**
* Convert DOM element to JSON node
*/
function elementToJson(element, depth = 0, parentRect = null) {
if (!shouldCapture(element) || depth > 50) return null;
const rect = element.getBoundingClientRect();
const computed = window.getComputedStyle(element);
const styles = getRelevantStyles(element, computed);
// Determine node type
let nodeType = element.tagName.toLowerCase();
// Handle text nodes specially
const directText = getDirectTextContent(element);
const hasOnlyText = element.childNodes.length === 1 &&
element.childNodes[0].nodeType === Node.TEXT_NODE;
if (hasOnlyText && directText) {
nodeType = 'text';
}
// Create node object
const node = {
type: nodeType,
id: element.id || undefined,
classes: element.className ? element.className.split(' ').filter(Boolean) : undefined,
styles: styles,
position: {
absolute: {
x: parentRect ? rect.left - parentRect.left : rect.left,
y: parentRect ? rect.top - parentRect.top : rect.top,
width: rect.width,
height: rect.height
}
}
};
// Add text content
if (nodeType === 'text' || hasOnlyText) {
node.text = directText || element.textContent?.trim();
}
// Add special attributes
if (element.tagName === 'IMG') {
node.alt = element.alt;
node.src = element.src;
}
if (element.tagName === 'INPUT' || element.tagName === 'TEXTAREA') {
node.placeholder = element.placeholder;
node.value = element.value;
node.styles.type = element.type;
}
if (element.tagName === 'A') {
node.href = element.href;
}
// Process children
const children = [];
for (let child of element.children) {
const childNode = elementToJson(child, depth + 1, rect);
if (childNode) {
children.push(childNode);
}
}
// Add text-only children as text nodes
for (let childNode of element.childNodes) {
if (childNode.nodeType === Node.TEXT_NODE) {
const text = childNode.textContent?.trim();
if (text && text.length > 0 && !hasOnlyText) {
children.push({
type: 'text',
text: text,
styles: {
fontFamily: computed.fontFamily,
fontSize: computed.fontSize,
fontWeight: computed.fontWeight,
color: computed.color
},
position: {
absolute: {
width: rect.width,
height: parseFloat(computed.fontSize) || 16
}
}
});
}
}
}
if (children.length > 0) {
node.children = children;
}
return node;
}
/**
* Extract the entire page or a specific element
*/
function extractPage(selector = 'body') {
const rootElement = document.querySelector(selector);
if (!rootElement) {
console.error(`❌ Element not found: ${selector}`);
return null;
}
console.log(`📦 Extracting: ${selector}`);
const json = elementToJson(rootElement);
if (!json) {
console.error('❌ Failed to extract page structure');
return null;
}
// Add metadata
const result = {
type: 'CANVAS',
name: document.title || 'Extracted Page',
url: window.location.href,
extractedAt: new Date().toISOString(),
viewport: {
width: window.innerWidth,
height: window.innerHeight
},
children: [json]
};
return result;
}
// Extract the page
const pageJson = extractPage('body');
if (pageJson) {
// Copy to clipboard
const jsonString = JSON.stringify(pageJson, null, 2);
// Try to copy to clipboard
if (navigator.clipboard) {
navigator.clipboard.writeText(jsonString).then(() => {
console.log('✅ Page extracted successfully!');
console.log('📋 JSON copied to clipboard!');
console.log('📊 Stats:', {
totalNodes: countNodes(pageJson),
url: pageJson.url,
viewport: pageJson.viewport
});
console.log('');
console.log('🎨 Now paste this into the Figma plugin!');
}).catch(err => {
console.log('✅ Page extracted successfully!');
console.log('⚠️ Could not copy to clipboard automatically.');
console.log('📋 Copy the JSON below:');
console.log(jsonString);
});
} else {
console.log('✅ Page extracted successfully!');
console.log('📋 Copy the JSON below:');
console.log(jsonString);
}
// Store in global variable for easy access
window.figmaJson = pageJson;
console.log('💾 Also saved to: window.figmaJson');
}
function countNodes(node) {
let count = 1;
if (node.children) {
for (let child of node.children) {
count += countNodes(child);
}
}
return count;
}
console.log('');
console.log('💡 Tip: To extract a specific element, run:');
console.log(' extractPage("#my-element-id")');
// Export function for custom extractions
window.extractPage = extractPage;
})();