Last active
October 2, 2020 11:08
-
-
Save gapurov/b4d8401cb3fc55d783007d0570d324a9 to your computer and use it in GitHub Desktop.
BeardedSpice Youtube and Youtube Music Media Strategies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// YouTube.plist | |
// BeardedSpice | |
// | |
// Created by Jose Falcon on 12/15/13. | |
// Updated by Alin Panaitiu on 3/2/18. | |
// Updated by Vladislav Gapurov on 07/28/18 | |
// Copyright (c) 2013 Tyler Rhodes / Jose Falcon. All rights reserved. | |
// | |
BSStrategy = { | |
version: 4, | |
displayName: "Youtube", | |
accepts: { | |
method: "predicateOnTab", | |
format: "%K LIKE[c] '*youtube.com/watch*' && !%@ LIKE[c] '*music.youtube.com*'", | |
args: ["URL", "URL"] | |
}, | |
isPlaying: function () { return !document.querySelector('#movie_player video').paused; }, | |
toggle: function () { document.querySelector('#movie_player .ytp-play-button').click(); }, | |
previous: function () { document.querySelector('#movie_player').previousVideo(); }, | |
next: function () { document.querySelector('#movie_player').nextVideo(); }, | |
pause: function () { document.querySelector('#movie_player').pauseVideo(); }, | |
favorite: function () { document.querySelector('ytd-toggle-button-renderer').click(); }, | |
trackInfo: function () { | |
function pad(number) { | |
if (number < 10) { | |
return `0${number}`; | |
} else { | |
return number.toString(); | |
} | |
}; | |
function secondsToTimeString(seconds) { | |
hours = Math.floor(seconds / 3600); | |
minutes = Math.floor(seconds / 60); | |
seconds = Math.floor(seconds % 60); | |
if (hours > 0) { | |
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`; | |
} else { | |
return `${pad(minutes)}:${pad(seconds)}`; | |
} | |
}; | |
playerManager = document.querySelector('yt-player-manager'); | |
player = playerManager.player_; | |
videoData = player.getVideoData(); | |
progress = player.getProgressState(); | |
played = secondsToTimeString(progress.current); | |
duration = secondsToTimeString(progress.duration); | |
return { | |
'image': `https://i.ytimg.com/vi/${videoData.video_id}/hqdefault.jpg`, | |
'track': videoData.title, | |
'artist': videoData.author, | |
'progress': `${played} of ${duration}`, | |
'favorited': document.querySelector('ytd-toggle-button-renderer').data.isToggled | |
}; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// YouTubeMusic.plist | |
// BeardedSpice | |
// | |
// Created by Vladislav Gapurov on 07/28/18 | |
// Copyright (c) 2013 Tyler Rhodes / Jose Falcon. All rights reserved. | |
// | |
BSStrategy = { | |
version: 1, | |
displayName: "YouTube Music", | |
accepts: { | |
method: "predicateOnTab", | |
format: "%K LIKE[c] '*music.youtube.com/*'", | |
args: ["URL"], | |
}, | |
isPlaying: function() { return !document.querySelector('#movie_player video').paused; }, | |
toggle: function() { document.querySelector('.ytmusic-player-bar.play-pause-button').click(); }, | |
previous: function() { document.querySelector('.ytmusic-player-bar.previous-button').click(); }, | |
next: function() { document.querySelector('.ytmusic-player-bar.next-button').click(); }, | |
pause: function() { | |
if(!document.querySelector('#movie_player video').paused) { | |
document.querySelector('.ytmusic-player-bar.play-pause-button').click(); | |
} | |
}, | |
favorite: function() { | |
document.querySelector('ytmusic-like-button-renderer .ytmusic-like-button-renderer.like').click() | |
}, | |
trackInfo: function() { | |
timeInfo = document.querySelector('.ytmusic-player-bar.time-info').innerHTML.split('/'); | |
thumb = document.querySelector('.ytmusic-player-bar img'); | |
title = document.querySelector('.ytmusic-player-bar.title'); | |
byline = document.querySelector('.byline.ytmusic-player-bar'); | |
like = document.querySelector('ytmusic-like-button-renderer'); | |
return { | |
'image': thumb.src, | |
'track': title.text.runs[0].text, | |
'artist': Array.from(byline.children) | |
.reduce((acc, curr, i) => i === 0 ? curr.text : `${acc}, ${curr.text}`, '' ), | |
'progress': `${timeInfo[0].trim()} of ${timeInfo[1].trim()}`, | |
'favorited': like.getAttribute('like-status') === 'LIKE', | |
}; | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment