Walmart Mobile Share Button

Brings back the native share button on Walmart product pages!

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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

})();