Last active
January 29, 2025 08:21
-
-
Save pongo/9bfe29249d77abc071ab8a88c4136aea to your computer and use it in GitHub Desktop.
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 ozon.ru - для таблицы книжкой полки | |
| // @namespace http://tampermonkey.net/ | |
| // @version 0.1 | |
| // @match https://www.ozon.ru/context/detail/id/* | |
| // @grant GM_setClipboard | |
| // @grant unsafeWindow | |
| // ==/UserScript== | |
| function trim(s) { | |
| return s.trim().replace(/[\.,\s]+$/g, '').trim(); | |
| } | |
| function parse_author(author) { | |
| const split = author.split(' '); | |
| if (split.length <= 1) { | |
| return {author_first_name: '', author_last_name: author}; | |
| } | |
| const author_last_name = trim(split.slice(-1)[0]); | |
| const author_first_name = trim(split.slice(0, -1).join(' ')); | |
| return {author_first_name, author_last_name}; | |
| } | |
| function parse_genre(text, categories) { | |
| const genres = new Map([ | |
| ['Детективы. Боевики. Триллеры', 'Детективы'], | |
| ['Фантастика. Фэнтези. Мистика', 'Фантастика'], | |
| ['Сказки, народное творчество для детей', 'Сказки'], | |
| ['Биографии российских знаменитостей', 'Биографии'], | |
| ['Биографии. Мемуары', 'Биографии'], | |
| ['Биографии зарубежных знаменитостей', 'Биографии'], | |
| ['Искусствоведение. История искусств', 'Искусствоведение'], | |
| ['Тайны (таинственные явления в природе и истории)', 'Тайны'], | |
| ['Путеводители по странам. Страноведение', 'Путеводители'], | |
| ['Чтение для школьников', 'Для школьников'], | |
| ['Домашний круг', 'Домоводство'], | |
| ['Внеклассное чтение для школьников', 'Для школьников'], | |
| ]); | |
| if (genres.has(text)) { | |
| return genres.get(text); | |
| } | |
| const prevCats = new Map([ | |
| ['Исторические романы', 'Исторические романы'], | |
| ['Русская литература для детей', 'Для детей'], | |
| ]); | |
| const prevCat = categories.slice(-2, -1)[0]; | |
| const firstCat = categories[1]; | |
| switch(text) { | |
| case 'Художественная литература': | |
| case 'Исторические романы. Приключения': | |
| if (prevCats.has(prevCat)) { | |
| return prevCats.get(prevCat); | |
| } | |
| break; | |
| case 'Познавательная и справочная литература': | |
| if (firstCat === 'Детям и родителям') { | |
| return 'Справочник детям'; | |
| } | |
| break; | |
| } | |
| switch (text) { | |
| case 'Исторические романы. Приключения': | |
| return 'Исторические романы'; | |
| break; | |
| } | |
| return text; | |
| } | |
| function prepare_values() { | |
| const result = { | |
| book_name: trim(unsafeWindow.dataLayer[0].prodName), // $('.bItemName').text() | |
| genre: parse_genre(unsafeWindow.dataLayer[0].category, unsafeWindow.dataLayer[0].categoryName), | |
| author_first_name: '', | |
| author_last_name: '', | |
| year: '' | |
| }; | |
| let author = false; | |
| let year = false; | |
| $('.eItemProperties_line').each(function() { | |
| const $this = $(this); | |
| const name = trim($this.find('.eItemProperties_name').text()); | |
| const value = trim($this.find('.eItemProperties_text').text()); | |
| switch (name) { | |
| case 'Автор': | |
| //case 'Авторы': | |
| Object.assign(result, parse_author(value)); | |
| author = true; | |
| break; | |
| case 'Год выпуска': | |
| result['year'] = value; | |
| year = true; | |
| break; | |
| } | |
| if (author && year) { | |
| return false; | |
| } | |
| }); | |
| return result; | |
| } | |
| function copy_for_table(values) { | |
| const colls = [ | |
| values.author_first_name, | |
| values.author_last_name, | |
| values.book_name, | |
| values.year, | |
| '', | |
| '', | |
| values.genre, | |
| ]; | |
| const text = colls.join('\t'); | |
| console.log(text); | |
| GM_setClipboard(text); | |
| } | |
| $(function() { | |
| const values = prepare_values(); console.log(values); | |
| const btnId = 'my_copy_for_table'; | |
| const button = `<button id='${btnId}'>Скопировать для таблицы</button>`; | |
| $('.eItemProperties_text:first').append(`<span id='${btnId}></span>`); | |
| let step = 1; | |
| const clearId = setInterval(function() { | |
| console.log('interval', step, document.getElementById(btnId) === null); | |
| if (step === 1 && document.getElementById(btnId) === null) { | |
| step = 2; | |
| return; | |
| } | |
| if (step >= 2 && document.getElementById(btnId) === null) { | |
| step++; | |
| if (step >= 3) { | |
| clearInterval(clearId); | |
| $('.eItemProperties_text:first').append(button); | |
| $(`#${btnId}`).click(function() { | |
| copy_for_table(values) | |
| $(this).text('Скопировано!'); | |
| }); | |
| } | |
| } | |
| }, 500); | |
| }); |
Author
What exactly does this userscript do?
@Korb I was collecting a catalog of paper books in Google Docs. And I took data from Ozon (Russian analogue of Amazon): author's name, genre, year, etc.
The script received this data and copied it to the clipboard. But since the script is old, it probably doesn't work now.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What exactly does this userscript do?