Skip to content

Commit f87bc35

Browse files
committed
YouTube: Audio Only | Code Fix
1 parent b976ecf commit f87bc35

File tree

1 file changed

+40
-35
lines changed

1 file changed

+40
-35
lines changed

484611-youtube-audio-only.user.js

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// @name YouTube: Audio Only
33
// @description No Video Streaming
44
// @namespace UserScript
5-
// @version 1.6.7
5+
// @version 1.6.8
66
// @author CY Fung
77
// @match https://www.youtube.com/*
88
// @match https://www.youtube.com/embed/*
@@ -876,7 +876,7 @@
876876
if (player_.__audio544__) return;
877877
player_.__audio544__ = 1;
878878
// console.log(1233, player_)
879-
let ytdPlayerElement = null;
879+
const mediaCollection = document.getElementsByClassName('html5-main-video');
880880

881881
const stopAndReloadFn = async () => {
882882
let isLive = false;
@@ -905,7 +905,7 @@
905905

906906
const refreshAllStaleEntitiesForNonReadyAudio = async () => {
907907
try {
908-
if (audio.readyState == 0) await player_.refreshAllStaleEntities();
908+
if (audio.readyState == 0 && audio.isConnected === true) await player_.refreshAllStaleEntities();
909909
} catch (e) {
910910
}
911911
};
@@ -916,9 +916,9 @@
916916
};
917917
const seekToLiveHeadForLiveStream = async () => {
918918
try {
919-
await player_.seekToLiveHead();
920-
if ((await player_.isAtLiveHead()) === true) {
921-
await player_.seekToStreamTime();
919+
audio.isConnected === true && await player_.seekToLiveHead();
920+
if (audio.isConnected === true && (await player_.isAtLiveHead()) === true) {
921+
audio.isConnected === true && await player_.seekToStreamTime();
922922
return true;
923923
}
924924
} catch (e) {
@@ -930,38 +930,38 @@
930930
stopAndReload = false;
931931
await stopAndReloadFn();
932932
}
933-
if (audio.paused === true) {
933+
if (audio.isConnected === true && audio.paused === true) {
934934
await player_.clearVideo(); // avoid error in live streaming
935935
await player_.clearQueue(); // avoid error in live streaming
936936
await delayPn(300);
937937
for (let i = 0; i < 3; i++) {
938-
if (audio.readyState === 0) {
938+
if (audio.readyState === 0 && audio.isConnected === true) {
939939
if (await seekToLiveHeadForLiveStream()) await delayPn(60);
940940
}
941941
}
942942
if (k === -1) {
943943
await refreshAllStaleEntitiesForNonReadyAudio();
944944
} else if (k === 3) {
945-
while (audio.readyState === 0) {
945+
while (audio.readyState === 0 && audio.isConnected === true) {
946946
await refreshAllStaleEntitiesForNonReadyAudio();
947947
await triggerPlaying();
948948
await delayPn(300);
949949
}
950950
}
951-
} else if (audio.paused === false) {
952-
if (!player_.isAtLiveHead()) {
951+
} else if (audio.isConnected === true && audio.paused === false) {
952+
if (!player_.isAtLiveHead() && audio.isConnected === true) {
953953
if (await seekToLiveHeadForLiveStream()) await delayPn(60);
954954
}
955-
while (audio.readyState === 0) {
955+
while (audio.readyState === 0 && audio.isConnected === true) {
956956
await refreshAllStaleEntitiesForNonReadyAudio();
957957
await triggerPlaying();
958958
await delayPn(300);
959959
}
960-
if (!player_.isAtLiveHead()) {
960+
if (!player_.isAtLiveHead() && audio.isConnected === true) {
961961
if (await seekToLiveHeadForLiveStream()) await delayPn(60);
962962
}
963963
await refreshAllStaleEntitiesForNonReadyAudio();
964-
if (audio.readyState > 0 && audio.paused === true) {
964+
if (audio.readyState > 0 && audio.paused === true && audio.isConnected === true) {
965965
await triggerPlaying();
966966
}
967967
}
@@ -997,12 +997,18 @@
997997
};
998998
let mid = 0;
999999
const getAudioElement = () => {
1000-
if (!ytdPlayerElement) {
1001-
ytdPlayerElement = [...document.querySelectorAll('ytd-player')].filter(e => !e.closest('[hidden]'))[0]
1000+
if (mediaCollection.length === 0) return null;
1001+
if (mediaCollection.length > 1) {
1002+
const audios = [...mediaCollection].filter(e => e && !e.closest('[hidden]') && e.closest('ytd-player'));
1003+
if (audios.length === 1) {
1004+
return audios[0];
1005+
}
1006+
} else if (mediaCollection.length === 1) {
1007+
const e = mediaCollection[0];
1008+
if (e && !e.closest('[hidden]') && e.closest('ytd-player')) return e;
10021009
}
1003-
const audio = ytdPlayerElement ? HTMLElement.prototype.querySelector.call(ytdPlayerElement, '.video-stream.html5-main-video') : null;
1004-
return audio;
1005-
}
1010+
return null;
1011+
};
10061012
const _onPlayerStateChange = (k_) => {
10071013
const k = k_;
10081014
const ps = player_.getPlayerState();
@@ -1016,18 +1022,20 @@
10161022
});
10171023
}
10181024
}
1019-
if (typeof k === 'number' && k === ps) {
1020-
const audio = getAudioElement();
1021-
if (audio) {
1022-
if (mid > 1e9) mid = 9;
1023-
const t = ++mid;
1024-
prrPipeline(async () => {
1025-
if (t !== mid) return;
1026-
await prr.then();
1027-
if (t !== mid) return;
1025+
if (typeof k === 'number' && k === ps && ps !== 5) {
1026+
1027+
if (mid > 1e9) mid = 9;
1028+
const t = ++mid;
1029+
prrPipeline(async () => {
1030+
if (t !== mid) return;
1031+
await prr.then();
1032+
if (t !== mid) return;
1033+
const audio = getAudioElement();
1034+
1035+
if (audio && player_.getPlayerState() === ps) {
10281036
await asyncStateChange(audio, k);
1029-
});
1030-
}
1037+
}
1038+
});
10311039
}
10321040
};
10331041

@@ -1265,13 +1273,10 @@
12651273
(async () => {
12661274

12671275
let player__;
1268-
let elm;
12691276

12701277
const getAudioElement = () => {
1271-
if (!elm) {
1272-
elm = player__;
1273-
}
1274-
const audio = elm ? HTMLElement.prototype.querySelector.call(elm, '.video-stream.html5-main-video') : null;
1278+
const elm = player__ || player0;
1279+
const audio = elm && elm.isConnected === true ? HTMLElement.prototype.querySelector.call(elm, '.video-stream.html5-main-video') : null;
12751280
return audio;
12761281
}
12771282

0 commit comments

Comments
 (0)