A Chrome extension to quickly capture URLs into Semble Collections at https://semble.so semble.so
at-proto semble chrome-extension
at feature/custom-pds-support 77 lines 2.0 kB view raw
1/** 2 * Metadata Extractor Content Script 3 * Extracts page metadata from Open Graph tags, meta tags, and document properties 4 */ 5 6/** 7 * Extract metadata from the current page 8 * @returns {object} Metadata object 9 */ 10function extractMetadata() { 11 // Helper function to get meta tag content 12 const getMeta = (selector) => { 13 const element = document.querySelector(selector); 14 return element ? element.getAttribute('content') || '' : ''; 15 }; 16 17 // Extract title 18 const title = 19 getMeta('meta[property="og:title"]') || 20 getMeta('meta[name="twitter:title"]') || 21 document.querySelector('title')?.textContent || 22 document.URL; 23 24 // Extract description 25 const description = 26 getMeta('meta[property="og:description"]') || 27 getMeta('meta[name="twitter:description"]') || 28 getMeta('meta[name="description"]') || 29 ''; 30 31 // Extract image 32 const imageUrl = 33 getMeta('meta[property="og:image"]') || 34 getMeta('meta[name="twitter:image"]') || 35 ''; 36 37 // Extract site name 38 const hostname = new URL(document.URL).hostname; 39 const siteName = 40 getMeta('meta[property="og:site_name"]') || 41 hostname; 42 43 // Extract author 44 const author = 45 getMeta('meta[name="author"]') || 46 getMeta('meta[property="article:author"]') || 47 ''; 48 49 // Extract type 50 const type = 51 getMeta('meta[property="og:type"]') || 52 'website'; 53 54 return { 55 url: document.URL, 56 title: title.trim(), 57 description: description.trim(), 58 imageUrl: imageUrl.trim(), 59 siteName: siteName.trim(), 60 author: author.trim(), 61 type: type.trim(), 62 }; 63} 64 65// Listen for messages from popup 66chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { 67 if (request.action === 'extractMetadata') { 68 const metadata = extractMetadata(); 69 sendResponse(metadata); 70 } 71}); 72 73// Also send metadata automatically if requested 74if (window === window.top) { 75 // Only run in top frame, not iframes 76 console.log('Metadata extractor loaded'); 77}