<script>
(function() {
const CONFIG = {
summaryBlockSelector: '.summary-block-wrapper',
itemSelector: '.summary-item',
linkSelector: 'a.summary-title-link, a.summary-thumbnail-container, a.summary-read-more-link',
};
async function getSourceUrl(href) {
try {
const res = await fetch(href + '?format=json');
const data = await res.json();
return data?.item?.sourceUrl || null;
} catch {
return null;
}
}
async function processBlock(block) {
const items = block.querySelectorAll(CONFIG.itemSelector);
const promises = Array.from(items).map(async (item) => {
const firstLink = item.querySelector('a');
if (!firstLink) return;
const href = firstLink.getAttribute('href');
if (!href) return;
const sourceUrl = await getSourceUrl(href);
if (!sourceUrl) return;
const links = item.querySelectorAll(CONFIG.linkSelector);
links.forEach(link => {
link.setAttribute('href', sourceUrl);
link.setAttribute('target', '_blank');
link.setAttribute('rel', 'noopener noreferrer');
});
});
await Promise.all(promises);
}
function init() {
const blocks = document.querySelectorAll(CONFIG.summaryBlockSelector);
blocks.forEach(processBlock);
}
document.addEventListener('DOMContentLoaded', init);
window.addEventListener('mercury:load', init);
})();
</script>