Google Scholar - Open Links in New Tab

Open links in new tab when clicking on h3 elements and gs_or_ggsm class

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Google Scholar - Open Links in New Tab
// @namespace    https://violentmonkey.github.io/
// @version      1.3
// @description  Open links in new tab when clicking on h3 elements and gs_or_ggsm class
// @author       Bui Quoc Dung
// @match        https://scholar.google.*/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    function updateLinks() {
        // Selectors for elements that should open in a new tab
        const selectors = ['h3.gs_rt a', '.gs_or_ggsm a'];

        // Set target="_blank" for matching elements
        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(link => link.setAttribute('target', '_blank'));
        });

        // Handle .gs_nph and Related articles links that have valid hrefs and force open in new tab
        document.querySelectorAll('.gs_nph, a[href*="related:"]').forEach(link => {
            if (link.tagName === 'A' && link.getAttribute('href') && !link.getAttribute('href').startsWith('javascript')) {
                if (link.getAttribute('href').startsWith('/')) {
                    link.href = location.origin + link.getAttribute('href');
                }
                link.setAttribute('target', '_blank');
                link.addEventListener('click', function (event) {
                    event.preventDefault();
                    window.open(link.href, '_blank');
                });
            }
        });
    }

    // Run once on page load
    updateLinks();

    // Observe DOM changes to handle dynamically loaded content
    new MutationObserver(updateLinks).observe(document.body, { childList: true, subtree: true });
})();