Skip to content

Commit

Permalink
Player, WebSockets and other bug fixes
Browse files Browse the repository at this point in the history
1. Player tries to load three times
2. If the channel doesn't have 7tv or BTTV account the corresponding WebSocket will not open
3. Mod actions will now work if the user has them enabled
  • Loading branch information
Fiszh committed Nov 2, 2024
1 parent 0f09b03 commit 7e369f5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
3 changes: 1 addition & 2 deletions pages/channel.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<link rel="stylesheet" type="text/css" href="styles/followList.css">
<link rel="stylesheet" type="text/css" href="styles/styles.css">
<link rel="stylesheet" type="text/css" href="styles/settings.css">
<script src="https://player.twitch.tv/js/embed/v1.js"></script>

<style>
@import url('https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap');
Expand Down Expand Up @@ -54,7 +53,7 @@
</div>
</div>
<div class="embed">
<div id="twitch-embed"></div>
<div id="twitch-embed">Refresh if you don't see the player.</div>
<div class="stream-info">
<div class="stream-title">stream-title</div>
<div class="stream-viewers">0</div>
Expand Down
6 changes: 5 additions & 1 deletion src/channel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2082,6 +2082,8 @@ async function fetch7TVEmoteData(emoteSet) {
// 7TV WEBSOCKET

async function detect7TVEmoteSetChange() {
if (!SevenTVID) { return; }

SevenTVWebsocket = new WebSocket(`wss://events.7tv.io/v3@emote_set.update<object_id=${SevenTVemoteSetId}>`);

SevenTVWebsocket.onopen = async () => {
Expand Down Expand Up @@ -2267,6 +2269,8 @@ async function fetchBTTVEmoteData() {
// BTTV WEBSOCKET

async function detectBTTVEmoteSetChange() {
if (BTTVEmoteData.length < 1) { return; }

BTTVWebsocket = new WebSocket(`wss://sockets.betterttv.net/ws`);

BTTVWebsocket.onopen = async () => {
Expand Down Expand Up @@ -2766,7 +2770,7 @@ const intervalId = setInterval(scrollToBottom, 500);
chatDisplay.addEventListener('scroll', handleScroll, false);

function deleteMessages(attribute, value) {
if (settings && !settings['modAction']) { return; }
if (userSettings && !userSettings['modAction']) { return; }

if (attribute) {
const elementsToDelete = chatDisplay.querySelectorAll(`[${attribute}="${value}"]`);
Expand Down
24 changes: 22 additions & 2 deletions src/landingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ async function loadBodyElements(elements) {
link.href = 'https://fonts.googleapis.com/css2?family=Inter:[email protected]&display=swap';
document.head.appendChild(link);

const script = document.createElement('script');
script.src = "https://player.twitch.tv/js/embed/v1.js";
document.head.appendChild(script);

const twitchEmbed = document.getElementById('twitch-embed');
if (twitchEmbed) {
console.log('#twitch-embed exists!');
Expand Down Expand Up @@ -148,13 +152,17 @@ function loadFavicon(iconElement) {
document.head.appendChild(newFavicon);
}

function initializeTwitchPlayer() {
function initializeTwitchPlayer(retryCount = 3, delay = 1000) {
try {
var input = window.location.href.split('/');
var chnl = input[input.length - 1] || "uni1g";

document.title = chnl + " - YAUTC";

const twitchEmbed = document.getElementById('twitch-embed');

twitchEmbed.innerHTML = ''

new Twitch.Player("twitch-embed", {
width: "100%",
height: "100%",
Expand All @@ -166,7 +174,19 @@ function initializeTwitchPlayer() {
parent: ["fiszh.github.io"],
});
} catch (error) {
console.error(error);
console.error("Error initializing Twitch Player:", error);

const twitchEmbed = document.getElementById('twitch-embed');

twitchEmbed.innerHTML = "Refresh if you don't see the player."

if (retryCount > 0) {
setTimeout(() => {
initializeTwitchPlayer(retryCount - 1, delay);
}, delay);
} else {
console.error("Failed to initialize Twitch Player after multiple attempts.");
}
}
}

Expand Down

0 comments on commit 7e369f5

Please sign in to comment.