Google Scholar - Open Links in New Tab

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

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

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

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

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

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

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

(У мене вже є менеджер скриптів, дайте мені встановити його!)

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.

(I already have a user style manager, let me install it!)

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