Загрузка данных


(() => {
    const rows = [...document.querySelectorAll('.rt-tr')];

    const selected = rows.filter(row => {
        const checkbox = row.querySelector('input[type="checkbox"]');
        return checkbox && checkbox.checked;
    });

    console.log('Выбрано:', selected.length);

    if (!selected.length) {
        alert('Не выбрано ни одной строки');
        return;
    }

    // Сначала создаём пустые вкладки.
    // Это важно: браузер должен считать их частью одного действия.
    const tabs = selected.map(() => window.open('about:blank', '_blank'));

    console.log('Создано вкладок:', tabs.filter(Boolean).length);

    if (tabs.some(tab => !tab)) {
        alert(
            'Браузер заблокировал часть вкладок.\n' +
            'Разреши этому сайту всплывающие окна.'
        );
    }

    let index = 0;

    for (const row of selected) {

        const target = row.querySelector('span[aria-label]');

        if (!target) {
            console.warn('Не найден товар:', row);
            continue;
        }

        console.log(
            `Товар ${index + 1}:`,
            target.getAttribute('aria-label')
        );

        // Перехватываем переходы React Router
        let url = null;

        const oldPush = history.pushState;
        const oldReplace = history.replaceState;

        history.pushState = function(state, title, newUrl) {
            if (newUrl) {
                url = new URL(newUrl, location.href).href;
            }
            return oldPush.apply(this, arguments);
        };

        history.replaceState = function(state, title, newUrl) {
            if (newUrl) {
                url = new URL(newUrl, location.href).href;
            }
            return oldReplace.apply(this, arguments);
        };

        try {
            target.dispatchEvent(new MouseEvent('click', {
                bubbles: true,
                cancelable: true,
                view: window,
                ctrlKey: true,
                button: 0
            }));
        } catch (e) {
            console.error(e);
        }

        history.pushState = oldPush;
        history.replaceState = oldReplace;

        console.log('Полученный URL:', url);

        if (url && tabs[index]) {
            tabs[index].location.href = url;
        }

        index++;
    }
})();