Last active
November 18, 2024 19:02
-
-
Save mgaitan/601fddfdee51dc0bbdd47b835b1500c9 to your computer and use it in GitHub Desktop.
UserScript Mercado Libre comparar precios de mis compras
This file contains hidden or 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 MercadoLibre: Comparar precios de mis compras | |
// @namespace mercadolibre-precios-de-mis-compras | |
// @version 1.5.1 | |
// @description In the MercadoLibre purchases list, display the price when the product was purchased and the current price. | |
// @author Martín Gaítan and ChatGPT | |
// @match https://myaccount.mercadolibre.com.ar/my_purchases/list* | |
// @grant GM_xmlhttpRequest | |
// @connect mercadolibre.com.ar | |
// @connect mercadoshops.com.ar | |
// @connect api.bluelytics.com.ar | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js | |
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/locale/es.min.js | |
// ==/UserScript== | |
// Variable global para almacenar los datos del dólar | |
let blueDollarDataCache = null; | |
function loadDollarData(callback) { | |
if (blueDollarDataCache === null) { | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: 'https://api.bluelytics.com.ar/v2/evolution.json', | |
onload: function(response) { | |
const dollarData = JSON.parse(response.responseText); | |
blueDollarDataCache = dollarData.filter(data => data.source === 'Blue'); | |
callback(); | |
} | |
}); | |
} else { | |
callback(); | |
} | |
} | |
function findDollarValueAtDate(date) { | |
// Convertimos la fecha a un formato comparable | |
const formattedDate = date.format('YYYY-MM-DD'); | |
// Buscamos un valor que coincida exactamente con la fecha dada | |
let dollarValue = blueDollarDataCache.find(data => data.date === formattedDate); | |
// Si no encontramos un valor exacto, buscamos el valor más reciente antes de esa fecha | |
if (!dollarValue) { | |
// Filtramos los registros que sean anteriores a la fecha de búsqueda | |
const previousDates = blueDollarDataCache.filter(data => moment(data.date, 'YYYY-MM-DD').isBefore(date)); | |
// Ordenamos las fechas filtradas de más reciente a más antigua y tomamos la más reciente | |
dollarValue = previousDates.sort((a, b) => moment(b.date, 'YYYY-MM-DD').diff(moment(a.date, 'YYYY-MM-DD')))[0]; | |
} | |
// Si encontramos un valor de dólar, calculamos el promedio entre compra y venta | |
if (dollarValue) { | |
const averageValue = (parseFloat(dollarValue.value_buy) + parseFloat(dollarValue.value_sell)) / 2; | |
return averageValue; | |
} else { | |
// Si no encontramos ningún valor, retornamos null o un valor por defecto | |
return null; | |
} | |
} | |
loadDollarData(function() { | |
'use strict'; | |
const articles = document.querySelectorAll('article'); | |
articles.forEach(article => { | |
const dateElement = article.querySelector('div.list-item-grouper__header span.bf-ui-rich-text.bf-ui-rich-text--bold'); | |
const dateText = dateElement ? dateElement.textContent.trim() : null; | |
let purchaseDate = null; | |
if (dateText) { | |
purchaseDate = moment(dateText, 'D [de] MMM [de] YYYY', 'es'); | |
} | |
if (purchaseDate && purchaseDate.isValid()) { | |
const dolarPurchaseDate = findDollarValueAtDate(purchaseDate); | |
const dolarToday = findDollarValueAtDate(moment()); | |
const listItems = article.querySelectorAll('div.list-item'); | |
listItems.forEach(listItem => { | |
const link_compra = listItem.querySelector('a.andes-button--loud'); | |
if (link_compra) { | |
const url_compra = link_compra.href; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url_compra, | |
onload: function(responseCompra) { | |
const parserCompra = new DOMParser(); | |
const htmlDocCompra = parserCompra.parseFromString(responseCompra.responseText, 'text/html'); | |
const purchase_price = parseFloat(htmlDocCompra.querySelector('span.bf-ui-price-small').textContent.trim().replace('.', '').replace(',', '.')); | |
const purchase_price_dolar = purchase_price / dolarPurchaseDate; | |
const containerDiv = document.createElement('div'); | |
containerDiv.style.margin = '5px'; | |
const pCompra = document.createElement('p'); | |
pCompra.textContent = "Precio de compra: $" + purchase_price.toFixed(2) | |
containerDiv.appendChild(pCompra); | |
const coti = document.createElement('p'); | |
coti.textContent = "Cotizacion blue hoy: $" + dolarToday + " / Cotizacion blue el " + dateText + ": $ " + dolarPurchaseDate; | |
containerDiv.appendChild(coti); | |
const pCompraU = document.createElement('p'); | |
pCompraU.textContent = "Precio de compra en dolares: u$s" + purchase_price_dolar.toFixed(2); | |
containerDiv.appendChild(pCompraU); | |
const today = moment(); | |
const daysDiff = today.diff(purchaseDate, 'days'); | |
const pDays = document.createElement('p'); | |
pDays.textContent = "Días desde la compra: " + daysDiff; | |
containerDiv.appendChild(pDays); | |
const link_actual = listItem.querySelector('a.andes-button--quiet'); | |
if (link_actual) { | |
const url_actual = link_actual.href; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url_actual, | |
onload: function(responseActual) { | |
const parserActual = new DOMParser(); | |
const htmlDocActual = parserActual.parseFromString(responseActual.responseText, 'text/html'); | |
const current_price = parseFloat(htmlDocActual.querySelector('span.andes-money-amount__fraction').textContent.trim().replace('.', '').replace(',', '.')); | |
const current_price_dolar = current_price / dolarToday; | |
const pActual = document.createElement('p'); | |
pActual.textContent = "Precio actual: $" + current_price.toFixed(2); | |
containerDiv.appendChild(pActual); | |
const pActualD = document.createElement('p'); | |
pActualD.textContent = "Precio actual en dólares: u$s" + current_price_dolar.toFixed(2); | |
containerDiv.appendChild(pActualD); | |
const percentageIncrease = ((current_price - purchase_price) / purchase_price) * 100; | |
const pIncrease = document.createElement('p'); | |
pIncrease.textContent = "Porcentaje de aumento en pesos: " + percentageIncrease.toFixed(2) + '%'; | |
containerDiv.appendChild(pIncrease); | |
const percentageIncreaseD = ((current_price_dolar - purchase_price_dolar) / purchase_price_dolar) * 100; | |
const pIncreaseD = document.createElement('p'); | |
pIncreaseD.textContent = "Porcentaje de aumento en dólares: " + percentageIncreaseD.toFixed(2) + '%'; | |
containerDiv.appendChild(pIncreaseD); | |
const annualInflation = (Math.pow((current_price / purchase_price), (365.25 / daysDiff)) - 1) * 100; | |
const pInflation = document.createElement('p'); | |
pInflation.textContent = "Inflación anualizada: " + annualInflation.toFixed(2) + '%'; | |
containerDiv.appendChild(pInflation); | |
} | |
}); | |
} | |
listItem.parentNode.insertBefore(containerDiv, listItem.nextSibling); | |
} | |
}); | |
} | |
}); | |
} | |
}); | |
})(); |
No funciona bien para compras de años pasados. No sé nada de JS pero parece que falta el año en línea 69??.
purchaseDate = moment(dateText, 'D [de] MMMM', 'es');
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cuando sea grande quiero ser como vos