-
Notifications
You must be signed in to change notification settings - Fork 0
/
google-podcast-save.js
84 lines (81 loc) · 3.32 KB
/
google-podcast-save.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
javascript:(function() {
function findNearestParentWithAttribute(node, attr) {
while (node) {
if (node.getAttribute(attr) !== null) {
return node;
}
node = node.parentElement;
}
return null;
}
function updateOrAddEpisode(episodes, newEpisode) {
const index = episodes.findIndex(ep => ep.episodeUrl === newEpisode.episodeUrl);
if (index >= 0) {
episodes[index] = newEpisode;
} else {
episodes.push(newEpisode);
}
}
function downloadJSON(data, filename) {
const blob = new Blob([data], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
let storedEpisodes = JSON.parse(localStorage.getItem("episodes")) || [];
const feedTitle = document.querySelector("div[data-title]") ? document.querySelector("div[data-title]").getAttribute("data-title") : "Unknown";
const spans = document.querySelectorAll("div[aria-role=\"button\"] span");
spans.forEach(span => {
const text = span.innerText.toLowerCase();
let status, minLeft;
if (text.includes("completed")) {
status = "completed";
minLeft = 0;
} else if (text.includes("min left")) {
status = "partial";
const hourMatch = text.match(/(\d+)\s*hr/);
const minuteMatch = text.match(/(\d+)\s*min/);
const hours = hourMatch ? parseInt(hourMatch[1], 10) : 0;
const minutes = minuteMatch ? parseInt(minuteMatch[1], 10) : 0;
minLeft = hours * 60 + minutes;
} else {
status = "unplayed";
}
const parentWithJsData = findNearestParentWithAttribute(span, "jsdata");
if (parentWithJsData) {
const jsDataValue = parentWithJsData.getAttribute("jsdata");
const urlParts = jsDataValue.split(";");
if (urlParts.length === 3) {
const url = urlParts[1];
let feedUrl = "", episodeGuid = "";
const siblingDiv = parentWithJsData.nextElementSibling;
if (siblingDiv && siblingDiv.firstElementChild) {
feedUrl = siblingDiv.firstElementChild.getAttribute("data-feed");
episodeGuid = siblingDiv.firstElementChild.getAttribute("data-guid");
}
let episode = {
feedTitle: feedTitle,
feedUrl: feedUrl,
episodeUrl: url,
episodeGuid: episodeGuid,
episodeStatus: status,
};
if (status !== "unplayed") {
episode.minLeft = minLeft;
}
console.log(episode);
updateOrAddEpisode(storedEpisodes, episode);
}
}
});
localStorage.setItem("episodes", JSON.stringify(storedEpisodes));
window.casts = storedEpisodes;
if (confirm("Do you want to download the episodes data as JSON file?")) {
downloadJSON(JSON.stringify(storedEpisodes, null, 4), "episodes.json");
}
})()