-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoursera.surr.user.js
157 lines (135 loc) · 3.89 KB
/
coursera.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
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
// ==UserScript==
// @name coursera_surrogate
// @description Allows to watch Coursera without their JavaScript
// @author KOLANICH
// @version 0.1
// @license Unlicense
// @grant none
// @run-at document-idle
// @match https://www.coursera.org/lecture/*
// @match https://www.coursera.org/learn/*
// ==/UserScript==
"use strict";
[...document.getElementsByClassName("collapsed")].forEach(e => {
e.classList.toggle("collapsed");
for(let ee of e.getElementsByClassName("content")){
ee.style.height = "";
}
});
[...document.getElementsByClassName("overflow-hidden")].forEach(e => {
e.classList.toggle("overflow-hidden");
e.style.height = "";
for(let ee of e.getElementsByClassName("visibility-hidden")){
ee.classList.toggle("visibility-hidden");
}
});
function parseMetadata() {
const infoParsers = {
"apolloState": /__APOLLO_STATE__\s*=\s*(\{.+\})\s*;\s*window\.renderedClassNames/,
"renderedClassNames": /window\.renderedClassNames\s*=\s*(\[[^\]]+\]);/
};
let res = {};
for(let s of document.getElementsByTagName("SCRIPT")){
if(!s.src){
for(let [k,rx] of Object.entries(infoParsers)){
let m = rx.exec(s.textContent);
if(m){
res[k] = JSON.parse(m[1]);
}
}
}
}
return res;
}
function getTypez(md){
let typez = {};
for(let [k,v] of Object.entries(md.apolloState)){
if(v["typeName"] && v["id"]){
let tC = typez[v["typeName"]];
if(!tC){
tC = typez[v["typeName"]] = {"id":{}, "name":{}};
}
tC["id"][v["id"]] = v;
tC["name"][v["name"]] = v;
}
}
return typez;
}
let md = parseMetadata();
function getCourseEl(md){
for(let el of Object.values(md["apolloState"] ["$ROOT_QUERY.CoursesV1Resource"])){
let id = el["id"];
if(id){
return md["apolloState"][id]
}
}
}
let course = getCourseEl(md);
let typez = getTypez(md);
let classIndexPairs = [
["m-y-2", "lecture"],
];
let materialRx = /^(.+?)(\d+m)$/;
var nounsRemap = {
"readings": "supplement",
"practice": "exam",
"videos": "lecture",
};
let sil = document.getElementsByClassName("Syllabus")[0];
if(sil){
let weeks = sil.getElementsByClassName("SyllabusWeek");
for(let w of weeks){
let modules = sil.getElementsByClassName("SyllabusModule");
for(let m of modules){
for(let det of m.getElementsByClassName("SyllabusModuleDetails")){
for(let g of m.getElementsByClassName("ItemGroupView")){
let i = g.getElementsByClassName("learning-item")[0];
//console.log(i);
let noun = i.textContent.split(" ")[1];
let idxType = nounsRemap[noun];
let idx = typez[idxType];
for(let item of g.querySelectorAll(".m-y-2")){
let ma = materialRx.exec(item.textContent);
if(ma){
var name = ma[1];
let imd = idx["name"][name];
if(imd){
console.log(item)
let a = document.createElement("A");
a.href = window.location.href;
a.search = "";
a.pathname = idxType + "/" + course["slug"] + "/" + imd["slug"] + "-" + imd["id"];
let p = item.parentElement;
item.parentElement.replaceChild(a, item);
a.appendChild(item);
}
}
}
}
}
}
}
} else {
let vmd = JSON.parse(document.querySelectorAll("script[type='application/ld+json']")[0].textContent);
for(let el of Object.values(vmd["@graph"])){
if("VideoObject" == el["@type"]){
console.log(el);
let videoContainer = document.querySelector("[alt=video-placeholder]").parentElement,
titleEl = videoContainer.parentElement.parentElement.getElementsByTagName("H2")[0];
let vt = document.createElement("VIDEO");
vt.src = el["contentURL"];
vt.poster = el["thumbnailURL"];
vt.controls = !0;
vt.style.width = "100%";
let vc2 = videoContainer.parentElement;
vc2.parentElement.replaceChild(vt, vc2);
let a = document.createElement("A");
a.textContent = titleEl.textContent;
a.href = el["contentURL"];
a.download = el["name"] + ".mp4";
titleEl.textContent = "";
titleEl.appendChild(a);
break;
}
}
}