1/15/2022, 4:21:26 AM Convert prices to EUR. You have to get a free API key from https://free.currencyconverterapi.com/ in order to use this script. The first time you use it on allegro, you will receive a prompt for the API key. It will keep prompting until you provide a valid key. If you don't want to see the prompt, turn off the script.
当前为
// ==UserScript==
// @name Allegro.pl - convert prices to EUR
// @namespace Violentmonkey Scripts
// @match https://allegro.pl/oferta/*
// @grant none
// @version 1.0
// @author -
// @description 1/15/2022, 4:21:26 AM Convert prices to EUR. You have to get a free API key from https://free.currencyconverterapi.com/ in order to use this script. The first time you use it on allegro, you will receive a prompt for the API key. It will keep prompting until you provide a valid key. If you don't want to see the prompt, turn off the script.
// ==/UserScript==
async function main() {
// We can only await inside an async function, so the whole script has to be an async function.
// I'm not indenting this function because it is the whole file.
storage = window.localStorage
//console.log("mystorage", storage)
const messageTitle = "Allegro convert to EUR greasemonkey script: "
const forexApiBase = "https://free.currconv.com/api/v7/convert"
const forexApi = "".concat(forexApiBase, "?q=PLN_EUR&compact=ultra&apiKey=")
// If the foreign exchange api key is not present, request it from the user, and save it in storage.
if(storage.getItem("forexApiKey") === null) {
const newForexApiKey = prompt("".concat(messageTitle, "Please enter API Key. You can request it for free from https://free.currencyconverterapi.com/"), "")
const apiCallValidate = "".concat(forexApi, newForexApiKey)
console.log("".concat(messageTitle, "about to create the following request to validate the API key: ", apiCallValidate))
// validate the key
fetch(apiCallValidate).then(function(response) {
console.log(response)
if (response.status !== 200) {
// Apparently errors >= 400 do not count to trigger onerror
throw new Error("Response HTTP code not 200 during api key validation") // fixme: use a typed error
}
storage.setItem("forexApiKey", newForexApiKey)
}).catch(function(error) {
alert("".concat(messageTitle, "Error performing API request. Is the key correct?"))
console.log("".concat(messageTitle, "Error performing API request. Is the key correct?"))
console.log(messageTitle, error)
})
}
// get the api key from storage and convert the prices.
const forexApiKey = storage.getItem("forexApiKey")
if(forexApiKey !== null) {
// perform actual call with the api key
apiCall = "".concat(forexApi, forexApiKey)
console.log("".concat(messageTitle, "about to create the following request to get the conversion rate: ", apiCall))
fetch(apiCall).then(function(response) {
if (response.status !== 200) {
// Apparently errors >= 400 do not count to trigger onerror
throw new Error("Response HTTP code not 200 during API call") // fixme: use a typed error
}
return response.json()
}).then(function(data) {
// convert stuff
console.log("".concat(messageTitle, "found conversion rate: ", data["PLN_EUR"]))
// find the price:
const list = document.querySelectorAll('[aria-label^="cena "]') // ^= means "begins with".
console.log("".concat(messageTitle, "found the following elements: "))
console.log(list)
if (list.length !== 1) {
const elmErr = "".concat(messageTitle, "Found too many or no elements when searching for price. Aborting.")
console.log(elmErr)
throw new Error(elmErr) // fixme: use a typed error
}
const priceDiv = list[0]
const priceLabel = priceDiv.attributes["aria-label"].value
const matches = priceLabel.match(/^cena ([0-9]+)\.([0-9]{2}) zł/)
const priceStr = "".concat(matches[1], matches[2])
const priceGrosze = parseInt(priceStr) // price expressed as an integer amount of Grosz
console.log(priceGrosze)
const priceCents = Math.round(priceGrosze * data["PLN_EUR"]) // price expressed as an integer amount of Cents. Round to nearest (i.e. 16.5 -> 17)
console.log(priceCents)
const priceCentsStr = priceCents.toString() // string version of price expressed as an integer amount of Cents
const priceEurStr = priceCentsStr.slice(0, -2) // whole Euros
const priceCentsFracStr = priceCentsStr.slice(-2) // Cents after decimal point
const priceEurStrFull = "".concat(priceEurStr.padStart(1, "0"), ".", priceCentsFracStr.padStart(2, "0")) // price in Euro expressed as string. the padding is to prevent prices like 1 cent or 44 cents from showing up with necessary leading zeros.
const priceEurSpan = document.createElement('span')
priceEurSpan.textContent = "".concat("\u00a0=\u00a0", priceEurStrFull, "\u00a0EUR")
priceDiv.appendChild(priceEurSpan) // add label that contains euro price
}).catch(function(error) {
// fixme: make this better
console.log("".concat(messageTitle, "Error performing API request. Is the key correct?"))
console.log(messageTitle, error)
})
}
}
main ()