-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.user.js
83 lines (73 loc) · 3.45 KB
/
script.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
// ==UserScript==
// @name Twitch Refresh Homepage
// @name:de Twitch Startseite aktualisieren
// @version 1.0.0
// @description Updates the Thumbnail, Viewer Count, Title, Game and Live Status of the Recommended Channels on the Twitch Homepage
// @description:de Aktualisiert das Vorschaubild, Zuschauerzahl, Titel, Spiel und Live-Status der empfohlenen Kanäle auf der Twitch Startseite
// @icon https://static.twitchcdn.net/assets/favicon-32-e29e246c157142c94346.png
// @author TalkLounge (https://github.com/TalkLounge)
// @namespace https://github.com/TalkLounge/twitch-refresh-homepage
// @license MIT
// @match https://www.twitch.tv/*
// @require https://cdn.jsdelivr.net/npm/axios@0.24.0/dist/axios.min.js
// ==/UserScript==
(function () {
'use strict';
async function init() {
const list = document.querySelectorAll("main article:not(:has(.tw-channel-status-text-indicator[style*='display: none']))");
for (let i = 0; i < list.length; i++) {
const query = `query {
user(login: \"${list[i].querySelector("p").innerText}\") {
id
login
displayName
description
createdAt
roles {
isPartner
}
stream {
id
title
type
viewersCount
createdAt
game {
name
}
}
}
}`.replaceAll("\n", "").replaceAll("\t", " "); // Thanks to https://github.com/mauricew/twitch-graphql-api/blob/master/USAGE.md
let data = await axios.post("https://gql.twitch.tv/gql", {
query: query,
variables: {}
}, {
headers: {
"Client-Id": "kimne78kx3ncx6brgo4mv6wki5h1ko",
"Content-Type": "application/json"
}
});
data = data.data.data.user;
if (data?.stream) { // Stream online
data = data.stream;
// Title
list[i].querySelector("h3").innerText = data.title;
list[i].querySelector("h3").title = data.title;
// Game
if (list[i].querySelector("p a")) { // Recommended <Game>-Channel Section doesn't have game paragraph
list[i].querySelector("p a").innerText = data.game.name;
list[i].querySelector("p a").href = `https://www.twitch.tv/directory/category/${data.game.name.toLowerCase().replaceAll(" ", "-")}`;
}
// Viewers Count
list[i].querySelector(".tw-media-card-stat").innerText = `${data.viewersCount.toLocaleString()} ${list[i].querySelector(".tw-media-card-stat").innerText.split(" ").slice(1).join(" ")}`;
// Thumbnail
const img = list[i].querySelector("img:nth-child(2)");
const url = new URL(img.src);
img.src = `${url.origin}${url.pathname}?t=${Date.now()}`;
} else { // Stream offline
list[i].querySelector(".tw-channel-status-text-indicator").style.display = "none";
}
}
}
window.setInterval(init, 30000);
})();