Loading...
Loading...
Chrome extension that captures webpages into Figma's clipboard format with font fixes and DOM cleanup
npx skill4agent add aradotso/design-skills figma-capture-extensionSkill by ara.so — Design Skills collection
PingFang SCNoto Serif SCfont-map.jsonNoto Sans SCmakecapture.jscapture.js:
curl -o capture.js https://www.figma.com/community/plugin/1159123024924461424/capture.jscp font-map.example.json font-map.jsonfont-map.json{
"Arial": "Inter",
"Helvetica": "Inter",
"SF Pro Display": "Google Sans Flex",
"Roboto": "Noto Sans SC",
"system-ui": "Inter"
}chrome://extensionsfigma-captureAlt+Shift+FCtrl+VCmd+VAlt+Shift+Fchrome://extensions/shortcuts{
"manifest_version": 3,
"name": "Figma Capture",
"version": "1.0.0",
"permissions": [
"activeTab",
"clipboardWrite"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start"
}
],
"action": {
"default_icon": "icon.png"
},
"commands": {
"_execute_action": {
"suggested_key": {
"default": "Alt+Shift+F"
}
}
}
}font-map.json{
"SegoeUI": "Inter",
"San Francisco": "Google Sans Flex",
"PingFang": "PingFang SC",
"Microsoft YaHei": "Noto Sans SC",
"Hiragino Sans": "Noto Sans JP"
}PingFang SCNoto Serif SCfont-map.jsonNoto Sans SCUser clicks extension icon
↓
background.js activates content script
↓
content.js injects capture.js
↓
capture.js serializes DOM → clipboard payload
↓
Clipboard interceptor transforms payload:
- Font correction
- DOM cleanup
- Wrapper flattening
↓
Modified payload written to clipboard
↓
User pastes into FigmaattachShadow// Override clipboard write to transform Figma payload
const originalWrite = navigator.clipboard.write;
navigator.clipboard.write = async function(data) {
const items = await Promise.all(data.map(async item => {
if (item.types.includes('text/html')) {
const blob = await item.getType('text/html');
const html = await blob.text();
// Transform Figma's clipboard payload
const transformed = transformFigmaPayload(html);
return new ClipboardItem({
'text/html': new Blob([transformed], { type: 'text/html' })
});
}
return item;
}));
return originalWrite.call(this, items);
};function detectAndFixFont(element, computedStyle) {
const text = element.textContent || '';
const fontFamily = computedStyle.fontFamily;
// CJK detection
const cjkRegex = /[\u4E00-\u9FFF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF]/;
if (cjkRegex.test(text)) {
// Check if serif
const isSerif = /serif/i.test(fontFamily);
return isSerif ? 'Noto Serif SC' : 'PingFang SC';
}
// Load font map
const fontMap = loadFontMap(); // from font-map.json
// Check for mapped font
for (const [original, replacement] of Object.entries(fontMap)) {
if (fontFamily.includes(original)) {
return replacement;
}
}
// Icon font detection (preserve)
if (/icon|symbol|awesome|material/i.test(fontFamily)) {
return fontFamily;
}
// Default fallback
return fontFamily || 'Noto Sans SC';
}function flattenWrappers(node) {
// Skip if not a wrapper candidate
if (node.children.length !== 1) return false;
const parent = node;
const child = node.children[0];
// Check if parent is just a pass-through container
const parentStyle = getComputedStyle(parent);
const childStyle = getComputedStyle(child);
const isPassThrough =
parentStyle.backgroundColor === 'transparent' &&
parentStyle.border === 'none' &&
parent.getBoundingClientRect().width === child.getBoundingClientRect().width &&
parent.getBoundingClientRect().height === child.getBoundingClientRect().height;
if (isPassThrough) {
// Promote child to parent's position
parent.replaceWith(child);
return true;
}
return false;
}// In background.js - save closed shadow roots
const shadowRoots = new WeakMap();
const originalAttachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function(init) {
const shadowRoot = originalAttachShadow.call(this, init);
if (init.mode === 'closed') {
shadowRoots.set(this, shadowRoot);
}
return shadowRoot;
};
// In content.js - intercept toolbar clicks
window.addEventListener('click', (event) => {
const toolbar = document.querySelector('.figma-capture-toolbar');
if (!toolbar) return;
const toolbarHost = toolbar.host;
const shadowRoot = shadowRoots.get(toolbarHost);
if (shadowRoot) {
event.stopPropagation();
event.preventDefault();
// Re-dispatch inside shadow DOM
const point = shadowRoot.elementFromPoint(event.clientX, event.clientY);
if (point) {
point.dispatchEvent(new MouseEvent('click', event));
}
}
}, true); // Capture phase// Skip script/style/svg from processing
const SKIP_TAGS = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'LINK', 'META'];
function shouldProcessElement(element) {
if (SKIP_TAGS.includes(element.tagName)) {
return false;
}
// Skip hidden elements
const style = getComputedStyle(element);
if (style.display === 'none' || style.visibility === 'hidden') {
return false;
}
// Skip zero-size elements
const rect = element.getBoundingClientRect();
if (rect.width === 0 && rect.height === 0) {
return false;
}
return true;
}// Create site-specific font mappings
const siteSpecificMaps = {
'github.com': {
'-apple-system': 'Inter',
'BlinkMacSystemFont': 'Inter',
'Segoe UI': 'Inter'
},
'twitter.com': {
'TwitterChirp': 'Google Sans Flex',
'Helvetica Neue': 'Inter'
}
};
function getFontMap() {
const hostname = window.location.hostname;
const baseMap = JSON.parse(localStorage.getItem('font-map.json') || '{}');
const siteMap = siteSpecificMaps[hostname] || {};
return { ...baseMap, ...siteMap };
}// Add debug logging to see what fonts are being applied
console.log('Applied font:', appliedFont);
console.log('Original font:', originalFont);
console.log('Font map:', fontMap);font-map.json{
"ProblematicFont": "Inter",
"AnotherBadFont": "Roboto"
}chrome://extensionscapture.jsls -la capture.js
# If missing:
make// In browser console on target page:
console.log(navigator.clipboard.write.toString());
// Should show wrapped function, not native codechrome://extensions// Disable wrapper flattening temporarily
const ENABLE_FLATTENING = false;
if (ENABLE_FLATTENING) {
flattenWrappers(node);
}const text = "你好世界";
const cjkRegex = /[\u4E00-\u9FFF\u3040-\u309F\u30A0-\u30FF\uAC00-\uD7AF]/;
console.log(cjkRegex.test(text)); // Should be truefont-map.json{
"SimSun": "Noto Sans SC",
"Microsoft YaHei": "PingFang SC"
}content.jsconst DEFAULT_FALLBACK_FONT = 'Inter'; // Change from 'Noto Sans SC'function customCleanup(element) {
// Remove all data attributes
for (const attr of element.attributes) {
if (attr.name.startsWith('data-')) {
element.removeAttribute(attr.name);
}
}
// Remove specific classes
const REMOVE_CLASSES = ['ad', 'banner', 'cookie-notice'];
element.classList.remove(...REMOVE_CLASSES);
}content.jsconst DEBUG = true;
function debug(...args) {
if (DEBUG) {
console.log('[Figma Capture]', ...args);
}
}
debug('Font applied:', fontFamily);
debug('Element cleaned:', element.tagName);capture.js