Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var player;
- var port;
- var UNSTARTED = -1;
- var ENDED = 0;
- var PLAYING = 1;
- var PAUSED = 2;
- var BUFFERING = 3;
- var VIDCUED = 5;
- function ToggleMusic() {
- //Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).
- var playerState = player.getPlayerState();
- var toggledOn = true;
- if (playerState == PAUSED || playerState == UNSTARTED || playerState == ENDED) {
- player.playVideo();
- }
- else if (playerState == VIDCUED) {
- var playlist = localStorage.getItem('playlist');
- playlist = JSON.parse(playlist);
- player.loadVideoById(playlist[0].ID);
- }
- else {
- player.pauseVideo();
- toggledOn = false;
- }
- return toggledOn;
- }
- function ShuffleMusic() {
- var playlist = JSON.parse(localStorage.getItem('playlist'));
- playlist = ShufflePlaylist(playlist);
- localStorage.setItem('playlist', JSON.stringify(playlist));
- //The best way to do this is to load the next two songs so that we've got the next one pre-loaded.
- LoadNext(playlist, 1);
- }
- function LoadNext(playlist, countToLoad) {
- //Possible values are unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5).
- var playerState = player.getPlayerState();
- var wasPlayingSong = playerState == 1 || playerState == 3;
- if (playerState != UNSTARTED) {
- player.stopVideo();
- player.clearVideo();
- }
- for (count = 0; count < countToLoad; count++) {
- player.loadVideoById(playlist[count].ID);
- }
- if (!wasPlayingSong) {
- player.pauseVideo();
- }
- }
- function NextSong() {
- var playlist = JSON.parse(localStorage.getItem('playlist'));
- var nextSong = playlist.shift();
- playlist.push(nextSong); //Move it to the end.
- LoadNext(playlist, 1);
- localStorage.setItem('playlist', JSON.stringify(playlist));
- }
- function onPlayerStateChange() {
- var playerState = player.getPlayerState();
- if (playerState == BUFFERING) {
- port.postMessage({ playerState: "Buffering" });
- }
- else if (playerState == PLAYING) {
- port.postMessage({ playerState: "Playing" });
- }
- else if (playerState == PAUSED) {
- port.postMessage({ playerState: "Paused" });
- }
- if (playerState == ENDED) {
- NextSong();
- }
- }
- function Initialize(playlist) {
- port = chrome.extension.connect({ name: "statusPoller" });
- if (!player) {
- YT_ready(function(){
- var frameID = getFrameID("MusicHolder");
- if (frameID) {
- player = new YT.Player(frameID, {
- events: {
- "onReady": function() {
- player.cueVideoById(playlist[0].ID, 0);
- },
- "onStateChange": onPlayerStateChange
- }
- });
- }
- });
- } else {
- // Only reload if the player is not playing. Otherwise, the music
- // stops when re-opening the popup.
- if (player.getPlayerState && player.getPlayerState() != PLAYING) {
- player.cueVideoById(playlist[0].ID, 0);
- }
- }
- }
- function GetIdFromUrl(url) {
- var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
- var match = url.match(regExp);
- if (match && match[7].length == 11) {
- return match[7];
- } else {
- alert("Failed to read YouTube URL. Did you enter a valid link?");
- }
- }
- //shuffles list in-place
- function ShufflePlaylist(list) {
- var i, j, t;
- for (i = 1; i < list.length; i++) {
- j = Math.floor(Math.random() * (1 + i)); // choose j in [0..i]
- if (j != i) {
- t = list[i]; // swap list[i] and list[j]
- list[i] = list[j];
- list[j] = t;
- }
- }
- return list;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement