diff --git a/code.js b/code.js index c665b3f..40c94c8 100644 --- a/code.js +++ b/code.js @@ -1,13 +1,13 @@ /** * Скрипт для парсинга значков Steam - * @version 0.1.0 + * @version 1.0.0 * @license Unlicense * @author Alexander Popov */ /** * Получает данные из элемента `div.badge_row` - * @param {Array} value - div.badge_row. + * @param {Array} value - div.badge_row * @returns {Object} */ function getData(value) { @@ -22,23 +22,26 @@ function getData(value) { // check left cards // для металлических карточек нет параметра 'карточек выпадет' let cardsDropLeft = 0; - if (value[1].getElementsByClassName('progress_info_bold')[0]) { - cardsDropLeft = value[1].getElementsByClassName('progress_info_bold')[0].match(/\d/g); + if (value[1].getElementsByClassName('progress_info_bold').length > 0) { + cardsDropsNumber = value[1].getElementsByClassName('progress_info_bold')[0].innerText.match(/\d/g); + if (cardsDropsNumber !== null) { cardsDropLeft = [0]; } } let badgeItem = { - id: value[1].childNodes[1].href.split('/')[6], + id: parseInt(value[1].childNodes[1].href.split('/')[6]), uri: value[1].childNodes[1].href, - name: value[1].childNodes[3].childNodes[1].childNodes[3].childNodes[0].nodeValue.replace(/[\r\n\t]/g, ''), - dropLeft: cardsDropLeft, + // name: value[1].childNodes[3].childNodes[1].childNodes[3].childNodes[0].nodeValue.trim().replace(/[\r\n\t]/g, ''), + dropLeft: parseInt(cardsDropLeft), }; + // console.log(`${badgeItem.name} :: ${badgeItem.dropLeft}`); + return badgeItem; } /** * Преобразовывает массив данных в JSON и возвращает к консоль - * @param {Array} badgesArray - массив данных о значках. + * @param {Array} badgesArray - массив данных о значках */ function exportJson(badgesArray) { console.log(JSON.stringify(badgesArray)); @@ -47,7 +50,7 @@ function exportJson(badgesArray) { /** * Функция main() */ -function getBadges(page, pages) { +function getBadges() { let badges = []; let currentPage = 1; @@ -62,24 +65,23 @@ function getBadges(page, pages) { while (currentPage <= totalPages) { console.log(`Parse ${currentPage} page...`); - const collection = document.getElementsByClassName('badge_row'); + fetch(`${baseURI}?p=${currentPage}`) + .then((response) => { return response.text(); }) + .then((html) => { + let parser = new DOMParser(); + let doc = parser.parseFromString(html, 'text/html'); + const collection = doc.getElementsByClassName('badge_row'); - for (const value of Object.entries(collection)) { - badges.push(getData(value)); - } + for (const value of Object.entries(collection)) { + badges.push(getData(value)); + } + }) + .catch((err) => console.warn('Something went wrong.', err)); currentPage++; - - if (currentPage <= totalPages) { - document.location.href = `${baseURI}?p=${currentPage}`; - - - } } - exportJson(badges); + setTimeout(() => { exportJson(badges); }, 10000); } -window.addEventListener('load', function() { - getBadges(1); -}); +getBadges();