-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcastbox.surr.user.js
125 lines (109 loc) · 3.65 KB
/
castbox.surr.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
// ==UserScript==
// @name castbox_surogate
// @description Allows to see Castbox podcasts episodes.
// @author KOLANICH
// @version 0.1
// @license Unlicense
// @grant none
// @run-at document-idle
// @include https://castbox.fm/channel/*
// @include https://castbox.fm/episode/*
// ==/UserScript==
"use strict";
const descriptorRx = /^\s*window\.__INITIAL_STATE__\s*=\s*"([^]+)"\s*;/im, imageSizeRx = /(\d+)x(\d+)bb.jpe?g/;
function getDescriptor(){
let s = [...document.getElementsByTagName("script")].filter(el => -1 < el.textContent.indexOf("window.__INITIAL_STATE__"))[0].textContent;
return JSON.parse(decodeURIComponent(descriptorRx.exec(s)[1]))
}
function indexDescriptorForEpisodes(descriptor){
let descriptorIndex = new Map();
for(let ep of descriptor["ch"]["eps"]){
descriptorIndex.set(ep["eid"], ep);
}
return descriptorIndex;
}
var descriptor = getDescriptor();
function replaceImageWithPic(img, ep){
let p = createPicture(ep);
img.parentElement.replaceChild(p, img);
p.appendChild(img);
}
function augmentExistingTableFromDescriptor(descriptor){
let descriptorIndex = indexDescriptorForEpisodes(descriptor);
let epRows = document.getElementsByClassName("episodeRow");
for (let r of epRows) {
for (let l of r.querySelectorAll("a")) {
let p = l.href.split("-");
let iD = null;
if(p.length){
iD = p[p.length - 1];
if(iD.indexOf("id") == 0){
iD = parseInt(iD.substring(2));
let ep = descriptorIndex.get(iD);
let controls = r.getElementsByClassName("ep-item-ctrls")[0];
let playBtn = r.getElementsByClassName("play")[0];
playBtn.href = ep["url"];
let imgContainer = r.getElementsByClassName("coverImgContainer")[0];
let img = imgContainer.getElementsByTagName("IMG")[0]
replaceImageWithPic(img, ep);
}
}
}
}
}
function augmentExistingEpisodeFromDescriptor(descriptor, descriptorIndex){
let ep = tpi["playItem"];
let playBtnIcon = document.getElementsByClassName("player_play_btn")[0];
let a = document.createElement("A");
a.href = ep["url"];
playBtnIcon.parentElement.replaceChild(a, playBtnIcon);
a.appendChild(playBtnIcon);
replaceImageWithPic(document.querySelector("img.cover"), ep);
}
function createPicture(ep){
let p = document.createElement("picture");
for(let vn of variants){
let v = document.createElement("source"), u = ep[vn], s = u.split();
s = s[s.length - 1];
let m = imageSizeRx.exec(s), w = m[1], h = m[2];
v.srcset = u;
v.media = "(min-width: " + w + "px) and (min-height: " + h + "px)";
p.appendChild(v)
}
return p;
}
const variants = ["small_cover_url", "cover_url", "big_cover_url"];
function generateListFromDescriptor(descriptor){
let l = document.createElement("OL");
for(let ep of descriptor["ch"]["eps"]){
let a = document.createElement("A");
a.href = ep["url"];
let dt = document.createElement("time");
dt.textContent = dt.datetime = ep["release_date"];
let t = document.createElement("span");
t.textContent = ep["title"];
let p = createPicture();
let img = new Image;
img.src = ep["cover_url"];
p.appendChild(img);
let au = document.createElement("span");
au.textContent = ep["author"];
let li = document.createElement("li");
l.appendChild(li);
li.appendChild(a);
a.appendChild(dt);
a.appendChild(document.createTextNode(" "));
a.appendChild(t);
a.appendChild(document.createTextNode(" by "));
a.appendChild(au);
a.appendChild(p);
}
return l;
}
//document.body.appendChild(generateListFromDescriptor(getDescriptor())); // no longer needed, they generate HTML themselves now.
let tpi = descriptor["trackPlayItem"];
if(tpi){
augmentExistingEpisodeFromDescriptor(descriptor);
} else {
augmentExistingTableFromDescriptor(descriptor);
}