| layout | default |
|---|---|
| title | PubMed Search Tool |
Search Term:
<div class="input-group">
<label for="api-key">API Key (optional):</label>
<input type="password" id="api-key" placeholder="Leave blank to use default">
</div>
<button id="search-button">Search PubMed</button>
Searching PubMed...
'; resultsTable.innerHTML = ''; try { // Step 1: Search for PMIDs progressBar.innerHTML = 'Finding articles...
'; const pmids = await searchPMIDs(apiKey, searchTerm); if (pmids.length === 0) { progressBar.innerHTML = 'No articles found for this search term.
'; return; } // Step 2: Fetch metadata progressBar.innerHTML = `Fetching details for ${pmids.length} articles...
`; const metadata = await fetchMetadata(apiKey, pmids); // Step 3: Display results displayResults(metadata); progressBar.innerHTML = `Found ${Object.keys(metadata).length} articles
`; } catch (error) { progressBar.innerHTML = `Error: ${error.message}
`; console.error(error); } } // API functions async function searchPMIDs(apiKey, searchTerm) { const params = new URLSearchParams({ db: 'pubmed', term: searchTerm, retmax: 100000, retmode: 'json', api_key: apiKey }); const response = await fetch(`${BASE_URL}esearch.fcgi?${params}`); const data = await response.json(); return data.esearchresult.idlist || []; } async function fetchMetadata(apiKey, pmids) { const allData = {}; for (let i = 0; i < pmids.length; i += BATCH_SIZE) { const batch = pmids.slice(i, i + BATCH_SIZE); const ids = batch.join(','); const params = new URLSearchParams({ db: 'pubmed', id: ids, retmode: 'json', api_key: apiKey }); const response = await fetch(`${BASE_URL}esummary.fcgi?${params}`); const data = await response.json(); for (const pid of batch) { if (data.result[pid]) { allData[pid] = data.result[pid]; } } // Update progress const progress = Math.min(i + BATCH_SIZE, pmids.length); document.getElementById('progress-bar').innerHTML = `Processed ${progress}/${pmids.length} records...
`; // Rate limiting await new Promise(resolve => setTimeout(resolve, 400)); } return allData; } // Display results in HTML table function displayResults(metadata) { const resultsTable = document.getElementById('results-table'); let html = ` `; Object.entries(metadata).forEach(([pmid, meta]) => { // Format authors let authors = []; try { const authorsList = typeof meta.authors === 'string' ? JSON.parse(meta.authors) : meta.authors || []; authors = authorsList .filter(a => a.authtype === 'Author') .map(a => a.name) .slice(0, 3); } catch (e) { console.error("Error parsing authors", e); } // Format year let year = ''; if (meta.pubdate) { const yearMatch = meta.pubdate.match(/\d{4}/); year = yearMatch ? yearMatch[0] : ''; } html += ` `; }); html += `| PMID | Title | Authors | Year | Journal |
|---|---|---|---|---|
| ${pmid} | ${meta.title || ''} | ${authors.join(', ')}${authors.length > 3 ? '...' : ''} | ${year} | ${meta.source || ''} |