This repository has been archived by the owner on Sep 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIERtpPlay.user.js
201 lines (168 loc) · 6.45 KB
/
IERtpPlay.user.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// ==UserScript==
// @name ImportExport4RTPPlay
// @namespace ImportExport4RTPPlay
// @match https://www.rtp.pt/play/
// @grant none
// @version 1.0
// @description Um userscript para exportar e importar a lista de favoritos/progresso de programas na RTP Play
// @license MIT, https://github.com/bossatossa/ImportExport4RTPPlay/blob/main/LICENSE
// @updateURL https://github.com/bossatossa/ImportExport4RTPPlay/raw/main/IERtpPlay.meta.js
// @downloadURL https://github.com/bossatossa/ImportExport4RTPPlay/raw/main/IERtpPlay.user.js
// ==/UserScript==
let favJSON_load;
let progJSON_load;
const file_fav_options = {
suggestedName: "RTPPlay_favorites_.json",
types: [
{
description: "Ficheiro JSON",
accept: { "application/json": [".json"] },
},
],
};
const file_prog_options = {
suggestedName: "RTPPlay_telemetry_.json",
types: [
{
description: "Ficheiro JSON",
accept: { "application/json": [".json"] },
},
],
};
async function saveFav() {
var fav_blob = new Blob([localStorage.RTPPlay_favorites_.toString()], {
type: "text/plain;charset=utf-8",
});
const newHandle = await window.showSaveFilePicker(file_fav_options);
const writableStream = await newHandle.createWritable();
await writableStream.write(fav_blob);
await writableStream.close();
}
async function loadFav() {
[favJSON_load] = await window.showOpenFilePicker(file_fav_options);
const favJSON_file = await favJSON_load.getFile();
const JSON_text = await favJSON_file.text();
localStorage.setItem("RTPPlay_favorites_", JSON_text);
alert("Lista de favoritos importado com sucesso!");
location.reload();
}
async function saveProg() {
var prog_blob = new Blob([localStorage.RTPPlay_telemetry_.toString()], {
type: "text/plain;charset=utf-8",
});
const newHandle = await window.showSaveFilePicker(file_prog_options);
const writableStream = await newHandle.createWritable();
await writableStream.write(prog_blob);
await writableStream.close();
}
async function loadProg() {
[progJSON_load] = await window.showOpenFilePicker(file_prog_options);
const progJSON_file = await progJSON_load.getFile();
const JSON_text = await progJSON_file.text();
localStorage.setItem("RTPPlay_telemetry_", JSON_text);
alert("Progresso de programas importado com sucesso!");
location.reload();
}
function setupFavButtons() {
const exportfavElement = document.createElement("span");
exportfavElement.id = "export-fav-button";
exportfavElement.classList.add("ver-todos");
exportfavElement.classList.add("text-uppercase");
exportfavElement.style =
"left: 26%; float: right; cursor: pointer; margin-right: 40%;";
exportfavElement.textContent = "Exportar";
const exportIcon = document.createElement("i");
exportIcon.classList.add("fal");
exportIcon.classList.add("fa-file-export");
const importfavElement = document.createElement("span");
importfavElement.id = "import-fav-button";
importfavElement.classList.add("ver-todos");
importfavElement.classList.add("text-uppercase");
importfavElement.style =
"left: 47%; float: right; cursor: pointer; margin-right: 40%;";
importfavElement.textContent = "Importar";
const importIcon = document.createElement("i");
importIcon.classList.add("fal");
importIcon.classList.add("fa-file-import");
document.querySelector("#favorites-shelf > h2").appendChild(exportfavElement);
document.querySelector("#favorites-shelf > h2").appendChild(importfavElement);
document.querySelector("#export-fav-button").appendChild(exportIcon);
document.querySelector("#import-fav-button").appendChild(importIcon);
document
.querySelector("#export-fav-button")
.addEventListener("click", async () => {
saveFav();
});
document
.querySelector("#import-fav-button")
.addEventListener("click", async () => {
loadFav();
});
}
function setupProgButtons() {
const exportprogElement = document.createElement("span");
exportprogElement.id = "export-prog-button";
exportprogElement.classList.add("ver-todos");
exportprogElement.classList.add("text-uppercase");
exportprogElement.style =
"left: 26%; float: right; cursor: pointer; margin-right: 40%;";
exportprogElement.textContent = "Exportar";
const exportprogIcon = document.createElement("i");
exportprogIcon.classList.add("fal");
exportprogIcon.classList.add("fa-file-export");
const importprogElement = document.createElement("span");
importprogElement.id = "import-prog-button";
importprogElement.classList.add("ver-todos");
importprogElement.classList.add("text-uppercase");
importprogElement.style =
"left: 47%; float: right; cursor: pointer; margin-right: 40%;";
importprogElement.textContent = "Importar";
const importprogIcon = document.createElement("i");
importprogIcon.classList.add("fal");
importprogIcon.classList.add("fa-file-import");
document.querySelector("#watching-shelf > h2").appendChild(exportprogElement);
document.querySelector("#watching-shelf > h2").appendChild(importprogElement);
document.querySelector("#export-prog-button").appendChild(exportprogIcon);
document.querySelector("#import-prog-button").appendChild(importprogIcon);
document
.querySelector("#export-prog-button")
.addEventListener("click", async () => {
saveProg();
});
document
.querySelector("#import-prog-button")
.addEventListener("click", async () => {
loadProg();
});
}
let chkTimer = setInterval(checkFavorites, 1000);
function checkFavorites() {
if (document.getElementById("favorites-shelf").style.display != "none") {
clearInterval(chkTimer);
setupFavButtons();
}
if (document.getElementById("watching-shelf").style.display != "none") {
clearInterval(chkTimer);
setupProgButtons();
}
if (
!localStorage.RTPPlay_favorites_ ||
localStorage.RTPPlay_favorites_ == "{}"
) {
clearInterval(chkTimer);
document.getElementById("favorites-shelf").style.display = "block";
setupFavButtons();
document.querySelector("#export-fav-button").style.display = "none";
document.querySelector("#import-fav-button").style.display = "block";
}
if (
!localStorage.RTPPlay_telemetry_ ||
localStorage.RTPPlay_telemetry_ == "{}"
) {
clearInterval(chkTimer);
document.getElementById("watching-shelf").style.display = "block";
setupProgButtons();
document.querySelector("#export-prog-button").style.display = "none";
document.querySelector("#import-prog-button").style.display = "block";
}
}