Last active
April 11, 2023 17:02
-
-
Save Markkop/d118f3ee703a8536f8eccf720d3792ac to your computer and use it in GitHub Desktop.
Show FIPE Price on WebMotors Car Card
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Show FIPE Price on WebMotors Car Card | |
// @namespace Violentmonkey Scripts | |
// @match https://www.webmotors.com.br/* | |
// @grant none | |
// @version 1.0 | |
// @author Marcelo "Mark" Kopmann | |
// ==/UserScript== | |
(async () => { | |
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | |
const processVehicleCard = async (vehicleCard) => { | |
async function getTabelaFipe(carId) { | |
const response = await fetch(`https://www.webmotors.com.br/api/detail/averageprice/car/${carId}`); | |
const json = await response.json(); | |
const fipePrice = json.FipePrice | |
return fipePrice | |
} | |
const addFipePrice = (fipePrice) => { | |
const priceElement = vehicleCard.querySelector('#valorVerParcelas'); | |
const fipePriceElement = document.createElement('p'); | |
const formattedFipePrice = fipePrice.toLocaleString('pt-BR', { | |
style: 'currency', | |
currency: 'BRL', | |
}); | |
fipePriceElement.innerText = `FIPE: R$ ${formattedFipePrice}`; | |
fipePriceElement.style.marginBottom = '1rem'; | |
fipePriceElement.style.marginLeft = 'auto'; | |
fipePriceElement.style.marginRight = 'auto'; | |
fipePriceElement.style.paddingLeft = '13px'; | |
priceElement.parentElement.insertBefore(fipePriceElement, priceElement); | |
}; | |
try { | |
await delay(1000); | |
const link = vehicleCard.querySelector('a').href; | |
const carId = link.split('/').pop().split('?')[0]; | |
const tabelaFipe = await getTabelaFipe(carId); | |
console.log({ tabelaFipe }); | |
addFipePrice(tabelaFipe); | |
} catch (error) { | |
console.error('Error:', error); | |
} | |
}; | |
const runLoopCode = async () => { | |
const vehicleCards = document.querySelectorAll('[data-qa^="vehicle_card_"]') | |
for (const vehicleCard of vehicleCards) { | |
await processVehicleCard(vehicleCard); | |
} | |
}; | |
const addButton = () => { | |
const button = document.createElement('button'); | |
button.className = 'Button'; | |
button.innerText = 'Mostrar FIPE'; | |
button.addEventListener('click', runLoopCode); | |
button.style.zIndex = '1000'; | |
button.style.width = '100px'; | |
button.style.margin = 'auto'; | |
document.querySelector('header div').appendChild(button); | |
}; | |
await delay(2000) | |
addButton(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment