Walmart Mobile Share Button

Brings back the native share button on Walmart product pages!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Walmart Mobile Share Button
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Brings back the native share button on Walmart product pages!
// @match        *://*.walmart.com/*
// @grant        none
// @license      MIT
// @esversion    8
// ==/UserScript==

(function() {
    'use strict';

    // This function builds our shiny new button
    function injectShareButton() {
        // If our button is already hanging out on the page, don't make a clone!
        if (document.getElementById('rogue-share-btn')) return;

        // Create the button element
        const shareBtn = document.createElement('button');
        shareBtn.id = 'rogue-share-btn';
        shareBtn.innerHTML = '🔗 Share'; 
        
        // Let's make it look like a native app button (High contrast, easy to tap!)
        Object.assign(shareBtn.style, {
            position: 'fixed',
            bottom: '90px', // Sits perfectly above the bottom navigation bar
            right: '20px',
            zIndex: '99999',
            backgroundColor: '#0071dc', // Official Walmart Blue
            color: '#ffffff', // Crisp white text for readability
            border: '2px solid #004c91', // Darker border for contrast
            borderRadius: '50px',
            padding: '14px 24px',
            fontSize: '18px',
            fontWeight: 'bold',
            boxShadow: '0 6px 12px rgba(0,0,0,0.4)', // Nice drop shadow so it floats
            cursor: 'pointer'
        });

        // The magic that happens when you tap it
        shareBtn.addEventListener('click', async () => {
            // Grab the product title (Walmart usually puts this in an <h1> tag)
            const titleElement = document.querySelector('h1');
            const productTitle = titleElement ? titleElement.innerText : 'Check out this item at Walmart';
            
            // Grab the URL, but strip out all the messy tracking garbage at the end
            const cleanUrl = window.location.origin + window.location.pathname;

            try {
                // Call the phone's native Share menu (The cool part!)
                if (navigator.share) {
                    await navigator.share({
                        title: productTitle,
                        text: `Hey! Check this out on Walmart: ${productTitle}`,
                        url: cleanUrl
                    });
                } else {
                    // Plan B: Just in case the browser gets grumpy
                    navigator.clipboard.writeText(cleanUrl);
                    alert('Link copied to clipboard! Ready to paste.');
                }
            } catch (err) {
                // If they close the share menu without sending, just silently ignore it
                console.log('Share cancelled by user.');
            }
        });

        // Slap the button onto the webpage
        document.body.appendChild(shareBtn);
    }

    // Because Walmart's site loads pages dynamically without refreshing, 
    // we need a little night-watchman to check if we are on a product page.
    setInterval(() => {
        // If the URL has "/ip/" in it, we are looking at an item!
        if (window.location.href.includes('/ip/')) {
            injectShareButton();
        } else {
            // If we went back to the home page, hide the evidence
            const existingBtn = document.getElementById('rogue-share-btn');
            if (existingBtn) existingBtn.remove();
        }
    }, 1000); // The watchman checks the aisle every 1 second

})();