-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanichart-season-preview.user.js
114 lines (105 loc) · 3.21 KB
/
anichart-season-preview.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
// ==UserScript==
// @name AniChart Season Preview
// @namespace https://github.com/noccu
// @match https://anichart.net/*
// @grant GM_addStyle
// @version 1.1
// @author noccu
// @description Auto-expands descriptions + slightly larger font, x2 speed trailers.
// ==/UserScript==
// Config
const PLAYBACK_RATE = 2
// Improve display
GM_addStyle(`
div.chart-view {
margin: 0 !important;
}
div.card-list {
grid-column-gap: 1em !important;
grid-row-gap: 1em !important;
grid-template-rows: unset !important;
grid-template-columns: repeat(auto-fill, minmax(31em, 1fr)) !important;
margin: 1em;
width: unset !important;
}
.media-card {
height: auto !important;
}
.description-wrap {
font-size: 1.25rem !important;
}
.reveal-external-links.body {
padding-bottom: 10px !important
}`)
// Logic
const OBSERVER = new MutationObserver(cope);
const noop = function () { }
var CARDS
function expand() {
for (let c of CARDS) {
if (c.expanded || !c.__vue__.media) continue
try {
let desc = c.getElementsByClassName("description-wrap")[0]
desc.innerHTML = c.__vue__.media.description
c.expanded = true
} catch {}
// Hack in YT's js api
try {
c.__vue__.media.trailer.id += "?enablejsapi=1&autoplay=1&autohide=1"
} catch {}
}
}
function cope(records) {
if (records.length == 1) return // Ignore noise
for (let r of records) {
// Ignore own mods
if (r.target.className == "description-wrap") return
// Trailers
if (r.target.id != "app") { break }
for (let n of r.addedNodes) {
if (n.nodeType != Node.ELEMENT_NODE) continue
if (n.classList.contains("trailer-wrap")) {
domExist("iframe.video", 100, 5).then(v => {
// v.id = "yt-trailer"
v.onload = () => {
// let listen = JSON.stringify({event: 'listening', id: v.id})
// v.contentWindow.postMessage(listen, "*")
let speed = JSON.stringify({ event: 'command', func: 'setPlaybackRate', args: [PLAYBACK_RATE, true] })
v.contentWindow.postMessage(speed, "*")
}
})
return
}
}
}
// Newly loaded cards
expand()
}
// Util
function pause(ms) {
return new Promise(r => setTimeout(r, ms));
}
// interval in ms, timeout in s
async function domExist(selector, interval, timeout) {
console.log("checking... ", selector);
timeout = timeout * (1000 / interval);
let run = 0;
while (true && run < timeout) {
run++;
let domObj = document.querySelector(selector);
if (domObj) {
return domObj;
}
await pause(interval);
}
throw new Error("Dom search failed");
}
domExist(".chart-view div", 500, 15)
.then(e => {
CARDS = e.getElementsByClassName("media-card")
expand()
OBSERVER.observe(document.getElementById("app"), { childList: true, subtree: true });
});
// window.addEventListener('message', function (msgEvt) {
// console.log(msgEvt);
// });