Skip to content

Instantly share code, notes, and snippets.

@depau
Last active May 16, 2024 19:16
Show Gist options
  • Save depau/247984a68069c335da21eb31e4839681 to your computer and use it in GitHub Desktop.
Save depau/247984a68069c335da21eb31e4839681 to your computer and use it in GitHub Desktop.
Download scontrini Esselunga - userscript

Download scontrini Esselunga in massa

Questo userscript aggiunge un bottone per scaricare tutti gli scontrini disponibili nell'area personale di Esselunga.

Lo script si ricorda dei download già effettuati, e li ignora le volte successive.

Funziona solo su browser Chrome-based perché il sito di Esselunga è stato sviluppato poggiando la tastiera sulla sedia e digitando con una penna infilata nell'apertura posteriore.

Licenza

WTFPL – Do What the Fuck You Want to Public License

// ==UserScript==
// @name Download all Esselunga receipts
// @namespace Violentmonkey Scripts
// @match https://www.esselunga.it/area-utenti/ist35/myesselunga/shoppingMovements*
// @grant GM_download
// @version 1.0
// @author -
// @description 5/16/2024, 8:19:41 PM
// ==/UserScript==
function rememberDownloadedPDF(id) {
const downloadedPDFs = JSON.parse(localStorage.getItem("downloadedPDFs") || "[]");
downloadedPDFs.push(id);
localStorage.setItem("downloadedPDFs", JSON.stringify(downloadedPDFs));
}
function isPDFDownloaded(id) {
const downloadedPDFs = JSON.parse(localStorage.getItem("downloadedPDFs") || "[]");
return downloadedPDFs.includes(id);
}
function downloadPDF(m, pdfURL) {
return new Promise((resolve, reject) => {
jQuery.blockUI();
jQuery.ajax({
url: pdfURL,
method: "GET",
dataType: "JSON"
}).success(function () {
jQuery.unblockUI();
resolve();
}).error(function (data) {
jQuery.unblockUI();
if (data.status === 308) {
const relativeUrl = data.responseText;
const url = document.location.origin + relativeUrl;
const filename = m.descrizioneCommerciale + " - " + m.dataOperazione + " - " + m.importo + ".pdf";
GM_download({
url: url,
name: filename,
onload: function () {
console.log("Downloaded PDF: ", url);
rememberDownloadedPDF(m.id);
resolve();
},
onerror: function (error) {
console.error("Failed to download PDF: ", error);
resolve();
}
});
return;
} else {
console.error("Failed to download PDF: ", data);
}
resolve();
});
}
)
;
}
async function downloadAll() {
const el = document.getElementById("wrapper");
const scope = angular.element(el).scope();
const ctrl = scope.ctrl;
// See if we should ignore already downloaded PDFs
const ignoreDownloadedCheckbox = document.getElementById("ignoreDownloadedCheckbox");
const ignoreDownloaded = ignoreDownloadedCheckbox.checked;
for (let m of ctrl.shoppingMovements) {
if (ignoreDownloaded && isPDFDownloaded(m.id)) {
console.log("PDF already downloaded: ", m.descrizioneCommerciale, m.dataOperazione, m.importo, m.id);
continue;
}
try {
const url = "/area-utenti/services/myesselunga/shoppingMovementsAjax?";
await downloadPDF(m, url + (m.id !== undefined ? "pdfId=" + m.id : "pdf=" + m.nomeFile) + "&codCarta=" + ctrl.currentCard + "&vediNonAttive=true");
} catch (e) {
console.error("Failed to download PDF: ", e);
}
}
alert("All available PDFs have been downloaded");
}
function injectController() {
'use strict';
const el = document.getElementById("wrapper");
if (!el) {
console.log("Element not found");
setTimeout(injectController, 100);
return;
}
const scope = angular.element(el).scope();
const ctrl = scope.ctrl;
window.ctrl = ctrl;
ctrl.maxResults = 1000000000;
const monthsBefore = document.getElementById("monthsBefore");
monthsBefore.value = "number:2"
monthsBefore.dispatchEvent(new Event("change"));
const div = document.createElement("div");
monthsBefore.parentNode.appendChild(div);
// Add download button after #monthsBefore element
const downloadButton = document.createElement("a");
downloadButton.href = "#";
downloadButton.textContent = "Download all PDFs";
downloadButton.style = "margin-left: 10px;";
downloadButton.onclick = async function () {
await downloadAll();
};
div.appendChild(downloadButton);
// Make the div children display as a row
div.style = "display: flex; flex-direction: row; align-items: center;";
// Add "ignore already downloaded" checkbox
const ignoreDownloadedCheckbox = document.createElement("input");
ignoreDownloadedCheckbox.type = "checkbox";
ignoreDownloadedCheckbox.id = "ignoreDownloadedCheckbox";
ignoreDownloadedCheckbox.style = "margin-left: 10px;";
ignoreDownloadedCheckbox.checked = true;
const ignoreDownloadedLabel = document.createElement("span");
ignoreDownloadedLabel.textContent = "Ignore already downloaded";
ignoreDownloadedLabel.style = "margin-left: 5px; font-size: initial; color: black;";
div.appendChild(ignoreDownloadedCheckbox);
div.appendChild(ignoreDownloadedLabel);
}
injectController();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment