// ==UserScript== // @name PMC Figure Embedder // @namespace http://tampermonkey.net/ // @version 0.2 // @description Embed PMC figures below PMC links on wiki pages // @author MuaddibLLM // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; console.log('PMC Figure Embedder loaded'); function embedPMCFigures() { // Updated regex to match the actual PMC URL structure const pmcLinks = document.querySelectorAll('a[href*="pmc.ncbi.nlm.nih.gov/articles/PMC"]'); console.log(`Found ${pmcLinks.length} PMC links`); pmcLinks.forEach(async (link) => { const pmcMatch = link.href.match(/pmc\.ncbi\.nlm\.nih\.gov\/articles\/(PMC\d+)/); if (!pmcMatch) { console.log('No PMC ID found in:', link.href); return; } const pmcId = pmcMatch[1].replace('PMC', ''); // Remove PMC prefix for API console.log(`Processing PMC ID: ${pmcId}`); try { // Fetch the PMC article page const response = await fetch(`https://pmc.ncbi.nlm.nih.gov/articles/PMC${pmcId}/`); const html = await response.text(); // Parse the HTML to find figure images const parser = new DOMParser(); const doc = parser.parseFromString(html, 'text/html'); // Look for figure images - PMC uses specific patterns const figures = doc.querySelectorAll('img[src*="cdn.ncbi.nlm.nih.gov/pmc/blobs"]'); console.log(`Found ${figures.length} figures in PMC${pmcId}`); if (figures.length === 0) { console.log(`No figures found for PMC${pmcId}`); return; } // Create container for embedded figures const figureContainer = document.createElement('div'); figureContainer.style.cssText = ` margin: 10px 0; padding: 10px; border: 1px solid #ddd; background: #f9f9f9; border-radius: 5px; `; const title = document.createElement('h4'); title.textContent = `Figures from PMC${pmcId}:`; title.style.margin = '0 0 10px 0'; figureContainer.appendChild(title); // Process each figure figures.forEach((fig, index) => { const imgContainer = document.createElement('div'); imgContainer.style.cssText = 'margin-bottom: 15px; text-align: center;'; const img = document.createElement('img'); img.src = fig.src; img.style.cssText = ` max-width: 300px; max-height: 200px; cursor: pointer; border: 1px solid #ccc; border-radius: 3px; `; img.title = `Click to view full size - Figure ${index + 1}`; // Make image clickable to view full size img.addEventListener('click', () => { window.open(fig.src, '_blank'); }); const caption = document.createElement('div'); caption.style.cssText = 'font-size: 12px; color: #666; margin-top: 5px;'; caption.textContent = `Figure ${index + 1} (click to enlarge)`; imgContainer.appendChild(img); imgContainer.appendChild(caption); figureContainer.appendChild(imgContainer); }); // Insert the figure container after the PMC link link.parentNode.insertBefore(figureContainer, link.nextSibling); } catch (error) { console.error(`Error fetching figures for PMC${pmcId}:`, error); // Show error message const errorDiv = document.createElement('div'); errorDiv.style.cssText = ` margin: 5px 0; padding: 5px; background: #ffe6e6; border: 1px solid #ff9999; border-radius: 3px; color: #cc0000; font-size: 12px; `; errorDiv.textContent = `Failed to load figures from PMC${pmcId}`; link.parentNode.insertBefore(errorDiv, link.nextSibling); } }); } // Run when page loads if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', embedPMCFigures); } else { embedPMCFigures(); } // Also run on dynamic content changes const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (mutation.addedNodes.length > 0) { embedPMCFigures(); } }); }); observer.observe(document.body, { childList: true, subtree: true }); })();