// ==UserScript== // @name My My Nintendo History // @namespace https://my.nintendo.com/ // @description Récupère les données de l'historique My Nintendo en JSON. // @include http://my.nintendo.com/* // @include https://my.nintendo.com/* // @version 1 // @grant none // @author Desmu // ==/UserScript== function getMyNintendoHistoryPage(items, cursor, currentPage, limit = 0, oldHistory = []) { // Traitement d'une page d'historique. let url = 'https://my.nintendo.com/api/point_transactions'; if (cursor) { url += `?cursor=${cursor}`; } const req = new XMLHttpRequest(); req.open('GET', url, true); req.onload = () => { const data = JSON.parse(req.responseText); let stop = false; data.items.forEach((item) => { if (limit < item.createdAt) { items.push(item); } else { // Retrait des actions déjà enregistrées dans le fichier d'historique initial. stop = true; } }); if (data.nextCursor && !stop) { // Passage à la page d'historique suivante. document.getElementById('mymynintendo_history_infos').innerHTML = `My My Nintendo en cours d'exécution. Pages d'historique récupérées : ${currentPage}.`; const nextPage = currentPage + 1; setTimeout(() => { // Cinq secondes d'attente sont imposées entre chaque page, // pour éviter d'envoyer trop de requêtes trop vite. getMyNintendoHistoryPage(items, data.nextCursor, nextPage, limit, oldHistory); }, 5000); } else { // Dernière page d'historique traitée. document.getElementById('mymynintendo_history_infos').innerHTML = 'Exécution de My My Nintendo terminée. Génération du fichier en cours.'; let history = items.reverse(); if (oldHistory.length > 0) { // Fusion de l'ancien fichier d'historique avec le nouveau. history = oldHistory.concat(history); } const dataStr = `data:text/json;charset=utf-8,${encodeURIComponent(JSON.stringify({ history }))}`; const downloadAnchorNode = document.createElement('a'); downloadAnchorNode.setAttribute('href', dataStr); downloadAnchorNode.setAttribute('download', 'mymynintendo_history.json'); document.body.appendChild(downloadAnchorNode); downloadAnchorNode.click(); downloadAnchorNode.remove(); document.getElementById('mymynintendo_history_infos').innerHTML = 'Exécution de My My Nintendo terminée. Fichier généré.'; } }; req.send(); } function setMyNintendoHistoryLimit() { // Import d'un fichier d'historique existant. const file = document.getElementById('history_json_part').files[0]; if (file) { document.getElementById('mymynintendo_history_infos').innerHTML = 'Fichier détecté, lecture en cours.'; const reader = new FileReader(); reader.readAsText(file, 'UTF-8'); reader.onload = (evt) => { const content = JSON.parse(evt.target.result); if (content.history) { const limit = content.history[content.history.length - 1].createdAt; getMyNintendoHistoryPage([], null, 1, limit, content.history); } else { document.getElementById('mymynintendo_history_infos').innerHTML = 'Erreur : fichier non issu de My My Nintendo.'; } }; reader.onerror = () => { document.getElementById('mymynintendo_history_infos').innerHTML = 'Erreur de lecture du fichier.'; }; } } window.onload = () => { // Ajout de la barre d'outils au site My Nintendo. if (document.getElementsByClassName('NHeader_login').length === 0) { document.getElementsByClassName('Layout-header')[0].insertAdjacentHTML('beforebegin', `