Jump to content

User:Polygnotus/Scripts/ExtraLinks.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//I could probably add more identifiers but I don't know which are supported
//add more libs?
//https://en.wikipedia.org/wiki/User:PerfektesChaos/js/idResolver
function addLinks() {
    const isbnLinks = document.querySelectorAll('a[href^="/wiki/Special:BookSources/"]');
    
    isbnLinks.forEach(link => {
        const isbn = link.getAttribute('href').split('/').pop();
        addLink(link, isbn, 'isbn');
    });

    const doiLinks = document.querySelectorAll('a[href^="https://doi.org/"]');
    
    doiLinks.forEach(link => {
        const doi = link.getAttribute('href').split('https://doi.org/').pop();
        addLink(link, doi, 'doi');
    });
}

function addLink(originalLink, identifier, type) {
    const shLink = document.createElement('a');
    shLink.href = `https://s` + `ci` + `-hu` + `b.se/${identifier}`;
    shLink.textContent = '[SH]';
    shLink.style.marginLeft = '5px';
    shLink.style.fontSize = '0.8em';
    shLink.style.color = '#36c';
    
    const lgLink = document.createElement('a');
    if (type === 'isbn') {
        lgLink.href = `https://li` + `bg` + `en.` + `is/search.php?req=${identifier}`;
    } else if (type === 'doi') {
        lgLink.href = `https://li` + `bg` + `en.` + `is/sc` + `imag/?q=${identifier}`;
    }
    lgLink.textContent = '[LG]';
    lgLink.style.marginLeft = '5px';
    lgLink.style.fontSize = '0.8em';
    lgLink.style.color = '#36c';
    
    originalLink.parentNode.insertBefore(lgLink, originalLink.nextSibling);
    originalLink.parentNode.insertBefore(shLink, lgLink);
}

if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', addLinks);
} else {
    addLinks();
}