USD to INR script

6/20/2025, 11:46:35 PM

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

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name        USD to INR script
// @namespace   Sam-2503
// @match       https://greasyfork.org/en/script_versions/new*
// @grant       none
// @version     1.0
// @author      Sam-2503
// @description 6/20/2025, 11:46:35 PM
// ==/UserScript==

// this script will convert USD prices to INR prices

(function () {
  "use strict";
  let exchangeRate = null;

  async function fetchExchangeRate() {
    try {
      const response = await fetch(
        "https://api.exchangerate-api.com/v4/latest/USD"
      );
      const data = await response.json();
      exchangeRate = data.rates.INR;

      console.log("Exchange rate fetched: 1 USD =", exchangeRate, "INR");
      convertPrices();
    } catch (error) {
      console.error("Error fetching exchange rate:", error);
    }
  }

  function convertPrices() {
    if (!exchangeRate) {
      return;
    }

    const regex = /\$([\d,]+(?:\.\d{1,2})?)/g;

    function convert(match, p1) {
      const usd = parseFloat(p1.replace(/, /g, ""));
      const inr = Math.round(usd * exchangeRate);
      return `${match} (₹${inr.toLocaleString("en-IN")})`;
    }

    const walker = document.createTreeWalker(
      document.body,
      NodeFilter.SHOW_TEXT,
      null,
      false
    );
    while (walker.nextNode()) {
      const node = walker.currentNode;
      if (!node.nodeValue.match(regex)) {
        continue;
      }

      node.nodeValue = node.nodeValue.replace(regex, (match, p1) => {
        const usd = parseFloat(p1.replace(/,/g, ""));
        const inr = Math.round(usd * exchangeRate);
        return `${match} (₹${inr.toLocaleString("en-IN")})`;
      });
    }
  }

  const observer = new MutationObserver(() => convertPrices());
  observer.observe(document.body, { childList: true, subtree: true });

  fetchExchangeRate();
})();