USD to INR script

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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.

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

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

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

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

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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();
})();