Product Availability Checker

Check product availability and send DM to @mym_mart on Instagram

当前为 2024-03-17 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Product Availability Checker
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Check product availability and send DM to @mym_mart on Instagram
// @author       Your Name
// @license      MIT
// @match        https://www.amazon.in/*
// @match        https://www.flipkart.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to fetch data from Google Sheets
    function fetchDataFromGoogleSheets() {
        return new Promise((resolve, reject) => {
            const googleSheetsUrl = 'https://docs.google.com/spreadsheets/d/1I_O0KOBUUqANW09KBLTA3Kadj3IvlzDJuSCLHFPrx9U/export?format=csv';

            fetch(googleSheetsUrl)
                .then(response => response.text())
                .then(data => resolve(data))
                .catch(error => reject(error));
        });
    }

    // Function to extract product name from Amazon page
    function extractProductNameAmazon() {
        const productNameElement = document.querySelector('span#productTitle');
        if (productNameElement) {
            return productNameElement.textContent.trim();
        }
        return null;
    }

    // Function to extract product name from Flipkart page
    function extractProductNameFlipkart() {
        const productNameElement = document.querySelector('span[class="_35KyD6"]');
        if (productNameElement) {
            return productNameElement.textContent.trim();
        }
        return null;
    }

    // Function to check product availability and display Instagram icon with dot
    async function checkProductAvailability() {
        // Fetch data from Google Sheets
        const googleSheetsData = await fetchDataFromGoogleSheets();

        let productName;

        // Extract product name from Amazon or Flipkart page
        if (window.location.hostname === 'www.amazon.in') {
            productName = extractProductNameAmazon();
        } else if (window.location.hostname === 'www.flipkart.com') {
            productName = extractProductNameFlipkart();
        }

        if (!productName) {
            return; // Exit if product name is not found
        }

        // Extract product name and quantity from Google Sheets data
        const rows = googleSheetsData.split('\n');
        for (let i = 1; i < rows.length; i++) {
            const columns = rows[i].split(',');
            const sheetProductName = columns[1].trim();
            const quantity = parseInt(columns[2].trim());

            // Check if product name matches and quantity is greater than 0
            if (productName.toLowerCase() === sheetProductName.toLowerCase() && quantity > 0) {
                // Display Instagram icon with green dot and send DM
                displayInstagramIcon(true);
                sendInstagramDM(productName);
                return;
            }
        }

        // If no matching product is found or quantity is 0, display Instagram icon without dot
        displayInstagramIcon(false);
    }

    // Function to display Instagram icon with or without dot based on availability
    function displayInstagramIcon(available) {
        const icon = document.createElement('a');
        icon.href = 'https://www.instagram.com/mym_mart/';
        icon.target = '_blank';
        icon.style.position = 'fixed';
        icon.style.bottom = '20px'; // Adjust position as needed
        icon.style.left = '20px'; // Adjust position as needed
        icon.style.zIndex = '9999';
        icon.style.display = 'block';
        icon.style.width = '50px';
        icon.style.height = '50px';
        icon.style.background = `url('https://cdn-icons-png.flaticon.com/512/174/174855.png') no-repeat center center`;
        icon.style.backgroundSize = 'contain';
        icon.style.borderRadius = '50%';
        icon.style.textDecoration = 'none';
        icon.style.color = 'white';
        icon.style.textAlign = 'center';
        icon.style.lineHeight = '50px';
        icon.style.fontWeight = 'bold';

        if (available) {
            icon.style.backgroundColor = 'green';
            icon.textContent = '•'; // Green dot
        } else {
            icon.style.backgroundColor = 'transparent';
            icon.textContent = ''; // No dot
        }

        document.body.appendChild(icon);
    }

    // Function to send Instagram DM to @mym_mart
    function sendInstagramDM(productName) {
        // Format the message
        const message = `Hey, I was looking at "${productName}" and found out that you were selling it. Is it still available?`;

        // Open Instagram DM link in a new tab
        window.open(`https://www.instagram.com/direct/t/mym_mart/?text=${encodeURIComponent(message)}`, '_blank');
    }

    // Run the script
    checkProductAvailability();
})();