Greasy Fork is available in English.

OLX Link Target Blank

Opening an advertisement in a new browser tab while maintaining the page position in the previous tab.

// ==UserScript==
// @name                    OLX Link Target Blank
// @name:uk                 OLX — відкриття об'яв у новій вкладці
// @name:ru                 OLX — открытие объявлений в новой вкладке
// @name:pl                 OLX - otwieraj ogłoszenia w nowej karcie
// @name:ro                 OLX - deschideți anunțuri într-o filă nouă
// @description             Opening an advertisement in a new browser tab while maintaining the page position in the previous tab.
// @description:uk          Відкриття об'яв у новій вкладці браузера зі збереженням позиції сторінки на попередній вкладці.
// @description:ru          Открытие объявлений в новой вкладке браузера с сохранением позиции страницы на предыдущей вкладке.
// @description:pl          Otwieranie ogłoszenie w nowej karcie przeglądarki przy zachowaniu pozycji strony w poprzedniej karcie.
// @description:ro          Deschiderea anunțurilor într-o filă nouă de browser, menținând în același timp poziția paginii în fila anterioară.
// @match                   *://*.olx.ua/*
// @match                   *://*.olx.pl/*
// @match                   *://*.olx.kz/*
// @match                   *://*.olx.ro/*
// @grant                   GM_openInTab
// @icon                    https://www.olx.ua/favicon.ico
// @namespace               https://greasyfork.org/users/1221433
// @author                  Sitego
// @date                    2023-12-03
// @license                 MIT
// @version                 1.0
// ==/UserScript==

(function () {
    'use strict';
    function handleMutation(mutationsList, observer) {
        for (const mutation of mutationsList) {
            if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
                openLinksInNewTab();
            }
        }
    }
    function openLinksInNewTab() {
        const adRows = document.querySelectorAll('[data-cy="ad-row"]');
        const lCardLinks = document.querySelectorAll('[data-cy="l-card"] a');
        adRows.forEach(adRow => {
            adRow.addEventListener('click', handleLinkClick);
        });
        lCardLinks.forEach(link => {
            link.addEventListener('click', handleLinkClick);
        });
    }
    function handleLinkClick(event) {
        const targetElement = event.target;
        const link = targetElement.closest('a');
        if (link) {
            event.preventDefault();
            const scrollPosition = window.scrollY;
            const newTab = window.open(link.href, '_blank');
            newTab.addEventListener('load', () => {
                window.scrollTo(0, scrollPosition);
            });
        }
    }
    const observer = new MutationObserver(handleMutation);
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
    openLinksInNewTab();
})();