Linkowanie numeru oferty na Allegro

Konwertuj numer oferty na klikalny link

当前为 2023-10-23 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Linkowanie numeru oferty na Allegro
// @version      0.1
// @description  Konwertuj numer oferty na klikalny link
// @author       Vomar
// @match        *://*.allegro.pl/*
// @grant        none
// @namespace https://greasyfork.org/users/156999
// ==/UserScript==

(function() {
    'use strict';

    // Funkcja przekształcająca tekst w link
    function convertToLink(element) {
        let textContent = element.textContent;
        let match = textContent.match(/nr oferty: (\d+)/);
        if (match) {
            let offerNumber = match[1];
            let link = document.createElement('a');
            link.href = 'https://allegro.pl/listing?string=' + offerNumber;
            link.textContent = 'nr oferty: ' + offerNumber;
            element.parentNode.replaceChild(link, element);
        }
    }

    // Obserwuj zmiany w DOM
    const observer = new MutationObserver((mutationsList, observer) => {
        for(let mutation of mutationsList) {
            if(mutation.addedNodes && mutation.addedNodes.length > 0) {
                for(let node of mutation.addedNodes) {
                    if(node.nodeType === 1) { // ELEMENT_NODE
                        if(node.matches('[data-test-id="item-offer-id"]')) {
                            convertToLink(node);
                        } else {
                            let elements = node.querySelectorAll('[data-test-id="item-offer-id"]');
                            elements.forEach(convertToLink);
                        }
                    }
                }
            }
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

})();