Farmazon Amazon Button

Adds a button on Amazon product pages to compare prices on farmazon.app

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Farmazon Amazon Button
// @namespace    https://farmazon.app/
// @version      1.0.2
// @description  Adds a button on Amazon product pages to compare prices on farmazon.app
// @author       farmazon.app
// @match        https://amazon.com/*
// @match        https://amazon.co.uk/*
// @match        https://amazon.de/*
// @match        https://amazon.fr/*
// @match        https://amazon.it/*
// @match        https://amazon.es/*
// @match        https://amazon.ca/*
// @match        https://amazon.com.mx/*
// @match        https://amazon.com.br/*
// @match        https://amazon.in/*
// @match        https://amazon.cn/*
// @match        https://amazon.co.jp/*
// @match        https://amazon.com.au/*
// @match        https://amazon.nl/*
// @match        https://amazon.se/*
// @match        https://amazon.pl/*
// @match        https://amazon.sg/*
// @match        https://amazon.com.tr/*
// @match        https://amazon.sa/*
// @match        https://amazon.ae/*
// @match        https://www.amazon.com/*
// @match        https://www.amazon.co.uk/*
// @match        https://www.amazon.de/*
// @match        https://www.amazon.fr/*
// @match        https://www.amazon.it/*
// @match        https://www.amazon.es/*
// @match        https://www.amazon.ca/*
// @match        https://www.amazon.com.mx/*
// @match        https://www.amazon.com.br/*
// @match        https://www.amazon.in/*
// @match        https://www.amazon.cn/*
// @match        https://www.amazon.co.jp/*
// @match        https://www.amazon.com.au/*
// @match        https://www.amazon.nl/*
// @match        https://www.amazon.se/*
// @match        https://www.amazon.pl/*
// @match        https://www.amazon.sg/*
// @match        https://www.amazon.com.tr/*
// @match        https://www.amazon.sa/*
// @match        https://www.amazon.ae/*
// @grant        none
// @run-at       document-end
// @homepageURL  https://farmazon.app/userscript
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Extract ASIN from the current Amazon product page URL
     * @returns {string|null} ASIN if found, null otherwise
     */
    function extractAsin() {
        const url = window.location.href;

        // Pattern to match ASIN in various Amazon URL formats
        // ASIN is typically 10 characters: uppercase letters and digits
        const patterns = [
            /\/dp\/([A-Z0-9]{10})/,
            /\/gp\/product\/([A-Z0-9]{10})/,
            /\/product\/([A-Z0-9]{10})/,
            /\/([A-Z0-9]{10})(?:\/|\?|$)/,
        ];

        for (const pattern of patterns) {
            const match = url.match(pattern);
            if (match) {
                return match[1];
            }
        }

        return null;
    }

    /**
     * Create and inject the Farmazon button
     */
    function createFarmazonButton() {
        const asin = extractAsin();

        if (!asin) {
            console.log('Farmazon: No ASIN found on this page');
            return;
        }

        // Check if button already exists
        if (document.getElementById('farmazon-button')) {
            return;
        }

        console.log('Farmazon: Found ASIN:', asin);

        // Create button container
        const buttonContainer = document.createElement('div');
        buttonContainer.id = 'farmazon-button';
        buttonContainer.style.cssText = `
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 10000;
            animation: fadeIn 0.3s ease-in;
        `;

        // Create the button
        const button = document.createElement('a');
        button.href = `https://farmazon.app/dp/${asin}?autoRefresh=true`;
        button.target = '_blank';
        button.rel = 'noopener noreferrer';
        button.innerHTML = `
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" style="margin-right: 8px; vertical-align: middle;">
                <path d="M12 2L2 7L12 12L22 7L12 2Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M2 17L12 22L22 17" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                <path d="M2 12L12 17L22 12" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
            </svg>
            Porównaj cenę na Farmazon.app
        `;
        button.style.cssText = `
            display: inline-flex;
            align-items: center;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 12px 24px;
            border-radius: 8px;
            text-decoration: none;
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
            font-size: 14px;
            font-weight: 600;
            box-shadow: 0 4px 12px rgba(102, 126, 234, 0.4);
            transition: all 0.3s ease;
            cursor: pointer;
            border: none;
        `;

        // Hover effect
        button.addEventListener('mouseenter', () => {
            button.style.transform = 'translateY(-2px)';
            button.style.boxShadow = '0 6px 16px rgba(102, 126, 234, 0.6)';
        });

        button.addEventListener('mouseleave', () => {
            button.style.transform = 'translateY(0)';
            button.style.boxShadow = '0 4px 12px rgba(102, 126, 234, 0.4)';
        });

        // Add animation keyframes
        const style = document.createElement('style');
        style.textContent = `
            @keyframes fadeIn {
                from {
                    opacity: 0;
                    transform: translateY(-10px);
                }
                to {
                    opacity: 1;
                    transform: translateY(0);
                }
            }
        `;
        document.head.appendChild(style);

        buttonContainer.appendChild(button);
        document.body.appendChild(buttonContainer);

        console.log('Farmazon: Button added successfully');
    }

    // Wait for page to be fully loaded
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', createFarmazonButton);
    } else {
        createFarmazonButton();
    }

    // Also watch for URL changes (for SPAs)
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            // Remove old button if exists
            const oldButton = document.getElementById('farmazon-button');
            if (oldButton) {
                oldButton.remove();
            }
            // Add new button after a short delay to allow page to load
            setTimeout(createFarmazonButton, 500);
        }
    }).observe(document, { subtree: true, childList: true });

})();