// Newburyport Public Library — A-Z List Only (() => { // 1. Find the accordion title matching "All Resources in Alphabetical Order" const HEADING_TEXT = /all resources in alphabetical order/i; const titleEl = Array.from(document.querySelectorAll('.wp-block-ub-content-toggle-accordion-title')) .find(el => HEADING_TEXT.test(el.textContent.trim())); if (!titleEl) { console.error('❌ Could not find the "All Resources in Alphabetical Order" accordion title.'); return; } // 2. The title lives in .accordion-title-wrap; its sibling .accordion-content-wrap holds the actual list const titleWrap = titleEl.closest('.wp-block-ub-content-toggle-accordion-title-wrap'); const accordion = titleWrap?.closest('.wp-block-ub-content-toggle-accordion'); const contentWrap = accordion?.querySelector('.wp-block-ub-content-toggle-accordion-content-wrap'); if (!contentWrap) { console.error('❌ Found the title but not its content panel. Structure may have changed.'); return; } // 3. Collect links only from within that accordion's content panel const links = contentWrap.querySelectorAll('a[href]'); const seen = new Set(); const results = []; // for console.table let output = ''; // tab-separated, for clipboard const accessTagPattern = /^\[?\s*(in library only|in library|from home|access)\s*\]?$/i; const junkLinkPattern = /^(discontinued|click here|here|learn more|faqs?( here)?|read more|find out more|more info|details|video tutorial|tutorial)$/i; links.forEach(link => { const linkText = link.innerText.trim(); const url = link.href; if (!linkText || !url) return; if (junkLinkPattern.test(linkText.replace(/[\[\]]/g, '').trim())) return; if (/discontinued/i.test(linkText)) return; let baseName = linkText; let accessTag = ''; if (accessTagPattern.test(linkText)) { accessTag = linkText.replace(/[\[\]]/g, '').trim(); const container = link.closest('li, p') || link.parentElement; if (container) { let containerText = container.innerText .replace(linkText, '') .replace(/\[.*?\]/g, '') .replace(/\(.*?\)/g, '') .trim(); baseName = containerText.split(/[:.\n]/)[0].trim() || linkText; } } let label = ''; if (/in library only/i.test(accessTag)) label = 'In Library Only'; else if (/^in library$/i.test(accessTag)) label = 'In Library'; else if (/from home/i.test(accessTag)) label = 'From Home'; const name = label ? `${baseName} (${label})` : baseName; const key = name + '|' + url; if (seen.has(key)) return; seen.add(key); output += `${name}\t${url}\n`; results.push({ Name: name, Access: label || '', URL: url }); }); console.table(results); copy(output); console.log(`✅ Copied to clipboard as tab-separated text! (${seen.size} resources from the A-Z list)`); })();