Google Scholar - Open Links in New Tab

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

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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 });
})();