Amazon Global Price Comparator

It shows global prices in amazon item page

目前為 2021-06-21 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Amazon Global Price Comparator
// @description It shows global prices in amazon item page
// @match       https://www.amazon.it/*
// @match       https://www.amazon.de/*
// @match       https://www.amazon.co.uk/*
// @match       https://www.amazon.fr/*
// @match       https://www.amazon.es/*
// @match       https://www.amazon.com/*
// @grant       none
// @version     1.5
// @author      SH3LL
// @grant       GM_xmlhttpRequest
// @namespace https://greasyfork.org/users/762057
// ==/UserScript==

function get_price(url,location) {
    return new Promise(function (resolve, reject) {
        GM_xmlhttpRequest({
            method: 'GET',
            responseType: 'document',
            synchronous: false,
            url: url,
            onload: (resp) => {
                  const doc = document.implementation.createHTMLDocument().documentElement;
                  doc.innerHTML = resp.responseText;
              
                  let price_block = doc.querySelector('#priceblock_ourprice'); // id prezzo normale
                  if(price_block === null) price_block = doc.querySelector('#priceblock_saleprice') // id prezzo scontato
                  if(price_block === null) price_block = doc.querySelector('#priceblock_dealprice') // id prezzo offerta flash
                  if(price_block === null) price_block = doc.querySelector('#priceblock_pospromoprice') // id prezzo offerta flash
                  if(price_block === null){ // se c'è solo la scritta "nuovo e usato da"
                    let etichette = doc.getElementsByTagName("span");
                    for(let el of etichette){
                      if(   (location==="es" && el.innerText.includes("Nuevos") && el.innerText.includes("desde") && !el.innerText.includes("€")) ||
                            (location==="it" && el.innerText.includes("Nuovo") && el.innerText.includes("da") && !el.innerText.includes("€")) ||
                            (location==="fr" && el.innerText.includes("Neufs") && el.innerText.includes("occasions") && !el.innerText.includes("€")) ||
                            (location==="de" && el.innerText.includes("Neu") && el.innerText.includes("ab") && !el.innerText.includes("€")) ||
                            //(location==="us" && el.innerText.includes("Nuevos") && el.innerText.includes("desde")) || // USA non hanno il campo "Nuovo e usato da ..€"
                            (location==="co.uk" && el.innerText.includes("New") && el.innerText.includes("from") && !el.innerText.includes("£"))    
                        ) {
                         
                            price_block = el.nextSibling;
                            break;
                         }
                         
                    }
                  }
              
                  if(price_block !== null && price_block !== undefined && price_block.innerText !== null && price_block.innerText !== undefined && price_block.innerText!== "") {
                    resolve(price_block.innerText);
                  }
                  resolve("error");
           }
        });
    });
}

async function main(){  
  
    if(window.location.href.includes("/dp/") || window.location.href.includes("/gp/product/") ){
      
      let amz_code; //get amazon product code
      if(window.location.href.includes("/gp/product/") && window.location.href.includes("?") ){
        amz_code=(window.location.href).split("?")[0].split('/gp/product/')[1];
        
      }else if(window.location.href.includes("/gp/product/") && !window.location.href.includes("?")){
        amz_code=(window.location.href).split('/gp/product/')[1];

      }else if(window.location.href.includes("/dp/") && window.location.href.includes("?")){
         amz_code=(window.location.href).split("?")[0].split('/dp/')[1].split('/')[0];
        
      }else if(window.location.href.includes("/dp/") && !window.location.href.includes("?")){ 
         amz_code=(window.location.href).split('/dp/')[1].split('/')[0];
        
      }
      
      
      //let price_block = document.querySelector('#price');  //VECCHIO HOOK (che non esiste quando il prezzo è fuori stock)
      let price_block = document.querySelector('#desktop_unifiedPrice'); //HOOK
      if(price_block===null) price_block = document.querySelector('#productOverview_feature_div').children[0];
      
      let tr1 = document.createElement("tr");
      
      let div1 = document.createElement("div");
      div1.style.position = "relative";
      div1.style.left="100%";
      
      let message1= document.createElement("label");
      message1.innerText="⏳ Loading Prices... ⏳";
      message1.style.color="firebrick";
      
      tr1.append(div1);
      div1.append(message1);
      //price_block.children[0].children[0].append(tr1);  //VECCHIO HOOK (che non esiste quando il prezzo è fuori stock)
      price_block.append(tr1);
      
      
      let locations = ["it","de","fr","es","co.uk","com"];
      let flags = { "it":"🇮🇹", "de":"🇩🇪", "fr":"🇫🇷", "es":"🇪🇸", "co.uk":"🇬🇧", "com":"🇺🇸" };
      let prices=[],link,min_price=999999999999999999999999999;
      
      for(let curr_location of locations){
        let curr_price = await get_price("https://www.amazon."+curr_location+"/dp/"+amz_code , curr_location);
        console.log(curr_location+": price-> " + curr_price)
        if(curr_price!=="error"){
          //get min price
          let cleaned_price=curr_price.replace(",",".");
          cleaned_price=cleaned_price.replace("$","");
          cleaned_price=cleaned_price.replace("£","");
          cleaned_price=cleaned_price.replace("€","");
          cleaned_price=cleaned_price.trim();//remove spaces
          
          if(parseFloat(cleaned_price)<parseFloat(min_price)){min_price=cleaned_price}
          
          link= document.createElement("a");
          link.innerText= "["+ flags[curr_location] + " " + curr_price.replace(".",",")+"]";
          link.href="https://www.amazon."+curr_location+"/dp/"+amz_code;
          link.style.color="green";
          link.style.paddingLeft = "5px";
          link.style.paddingRight = "5px";
          
          prices.push(link);
          
        }else{
          
          link= document.createElement("a");
          link.innerText= "["+ flags[curr_location] + " stock-out]";
          link.href="https://www.amazon."+curr_location+"/dp/"+amz_code;
          link.style.color="black";
          link.style.paddingLeft = "5px";
          link.style.paddingRight = "5px";
          //message.style.color="firebrick";
          
          prices.push(link);
          
        }
      }
      
      //REMOVE LOADING
      //price_block.children[0].children[0].removeChild(price_block.children[0].children[0].lastElementChild); //VECCHIO HOOK (che non esiste quando il prezzo è fuori stock)
      price_block.removeChild(price_block.lastElementChild);
      
      let tr2 = document.createElement("tr");
      let div2 = document.createElement("div");
      tr2.append(div2);
      
      for(let curr_price_link of prices){
        
        let cleaned_price=curr_price_link.innerText.replace("CO.UK","");
        cleaned_price=cleaned_price.replace("🇮🇹","");
        cleaned_price=cleaned_price.replace("🇩🇪","");
        cleaned_price=cleaned_price.replace("🇫🇷","");
        cleaned_price=cleaned_price.replace("🇪🇸","");
        cleaned_price=cleaned_price.replace("🇬🇧","");
        cleaned_price=cleaned_price.replace("🇺🇸","");
        cleaned_price=cleaned_price.replace(",",".");
        cleaned_price=cleaned_price.replace("[","");
        cleaned_price=cleaned_price.replace("]","");
        cleaned_price=cleaned_price.replace("$","");
        cleaned_price=cleaned_price.replace("£","");
        cleaned_price=cleaned_price.replace("€","");
        cleaned_price=cleaned_price.trim();//remove spaces
        
        if(cleaned_price===min_price){curr_price_link.style.color="red"}
        div2.append(curr_price_link);
        
      }
      
      price_block.append(tr2);
    }
}

main();