forked from oklookat/kp2imdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkpexport.js
111 lines (97 loc) · 2.72 KB
/
kpexport.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// ==UserScript==
// @name Kinopoisk folder exporter
// @name:ru Кинопоиск экспорт папки
// @namespace https://github.com/oklookat/kp2imdb
// @version 0.1
// @description Export movies from Kinopoisk folder to JSON.
// @description:ru Экспорт фильмов из папки Кинопоиска в JSON.
// @author oklookat
// @match https://www.kinopoisk.ru/mykp/folders/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
// @grant none
// @license MIT
// ==/UserScript==
(function () {
"use strict";
window.addEventListener(
"load",
function () {
addButton();
},
false
);
})();
function addButton() {
const filmsList = document.getElementsByClassName("filmsListTitle");
if (filmsList.length == 0) {
console.error("KP EXPORT: filmsListTitle not found");
return;
}
const expButt = document.createElement("div");
expButt.className = "KPX_exportButton";
expButt.style.height = "100%";
expButt.style.width = "200px";
expButt.style.backgroundColor = "red";
expButt.onclick = () => {
exportMovies();
};
filmsList[0].append(expButt);
}
function exportMovies() {
const ulList = document.getElementById("itemList");
if (!ulList) {
console.error("KP EXPORT: itemList not found");
return;
}
const parsedLis = [];
ulList.childNodes.forEach((node, key) => {
const isElement = node.nodeType == node.ELEMENT_NODE;
if (!isElement) {
return;
}
/**
* @type {HTMLLIElement}
*/
parsedLis.push(parseLi(node));
});
const parsed = JSON.stringify(parsedLis);
download(parsed, "kp_" + new Date().getTime() + ".json", "text/json");
}
function parseLi(liElement) {
const obj = {
id: "",
name: "",
alt_name: "",
date_added: "",
};
/**
* @type {HTMLLIElement}
*/
const li = liElement;
obj.id = li.getAttribute("data-id");
const info = li.getElementsByClassName("info")[0];
obj.name = info.getElementsByClassName("name")[0].textContent;
obj.alt_name = info.getElementsByTagName("span")[0].textContent;
obj.date_added = li.getElementsByTagName("span")[0].textContent;
return obj;
}
// Function to download data to a file
function download(data, filename, type) {
var file = new Blob([data], { type: type });
if (window.navigator.msSaveOrOpenBlob)
// IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else {
// Others
var a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function () {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}