Walmart Business Name Extractor

Extracts business name from Walmart.com seller page and appends it after the "Sold by" div

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

1// ==UserScript==
// @name         Walmart Business Name Extractor
// @namespace    talus.dev
// @version      1.2
// @description  Extracts business name from Walmart.com seller page and appends it after the "Sold by" div
// @match        https://www.walmart.com/ip/*
// @grant        none
// @license      GPL-3.0-or-later
// ==/UserScript==

(function() {
    'use strict';
  function decode(str) {
    let txt = document.createElement("textarea");
    txt.innerHTML = str;
    return txt.value;
  }

    // Function to extract the business name from the seller page
    function extractBusinessName() {
        const linkElement = document.querySelector('a[data-testid="view-seller-info-link"]');
        if (linkElement) {
            const sellerUrl = linkElement.href;
            fetch(sellerUrl)
                .then(response => response.text())
                .then(html => {
                    const businessNameRegex = /Business Name: <!-- -->\s*(.*?)<\/div>/;
                    const match = html.match(businessNameRegex);
                    if (match && match.length > 1) {
                        const businessName = match[1].trim();
                        console.log('Business Name:', businessName);

                        // Find the "Sold by" div on the original page
                        const soldByDivs = document.getElementsByClassName('lh-copy');
                        for (let i = 0; i < soldByDivs.length; i++) {
                            const soldByDiv = soldByDivs[i];
                            if (soldByDiv.textContent.match(/Sold.+by/)) {
                                // Create a new span element for the business name
                                const businessNameDiv = document.createElement('div');
                                businessNameDiv.className = 'mr0 black-90 b';
                                businessNameDiv.textContent = 'Business Name: ' + decode(businessName);

                                // Append the business name after the "Sold by" div
                                soldByDiv.appendChild(document.createTextNode(' '));
                                soldByDiv.appendChild(businessNameDiv);
                                break;
                            }
                        }
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                });
        }
    }

    // Call the extractBusinessName function when the page finishes loading
    window.addEventListener('load', extractBusinessName);
})();