Skip to content

Commit

Permalink
- Performance: getNowPlaying user action;
Browse files Browse the repository at this point in the history
  • Loading branch information
rentalhost committed Oct 1, 2011
1 parent 561754e commit a74b89e
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 48 deletions.
32 changes: 30 additions & 2 deletions javascript/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ var GCInjector = new function () {
}

// Smile
this.isSmile = function (songId) {
this.isSmile = function () {
return $("#queue_list li.queue-item-active div.radio_options a.smile").hasClass("active");
}

Expand All @@ -106,7 +106,7 @@ var GCInjector = new function () {
}

// Frown
this.isFrown = function (songId) {
this.isFrown = function () {
return $("#queue_list li.queue-item-active div.radio_options a.frown").hasClass("active");
}

Expand Down Expand Up @@ -203,6 +203,34 @@ var GCInjector = new function () {
);
}

// Get now playing data
this.getNowPlaying = function(callback){
// If not have data about currentSong, set data as 'unavailable'
// Using -1 instead of UNAVAILABLE because that the first callback param is string (will confuse it)
if (typeof this.GS.player.currentSong === 'undefined') {
return callback(-1);
}

var currentSong = this.GS.player.currentSong;
var playbackStatus = this.GS.player.getPlaybackStatus();
var queue = this.GS.player.queue;

return callback(
currentSong.SongName,
currentSong.ArtistName,
currentSong.AlbumName,
currentSong.getImageURL('s'),
playbackStatus.position,
playbackStatus.duration,
$("#playerDetails_nowPlaying a.add").hasClass("selected"),
$("#playerDetails_nowPlaying a.favorite").hasClass("selected"),
this.isSmile(),
this.isFrown(),
queue.activeSong.index,
queue.songs.length
);
}

// Get Data
this.getData = function () {
function parseSongItem (item) {
Expand Down
89 changes: 45 additions & 44 deletions javascript/groovesharkControl.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,61 @@

var DEFAULT_ALBUM_IMAGE = 'http://static.a.gs-cdn.net/webincludes/images/default/album_250.png';

var updateProgressbar = true;
var isGroovesharkFocused = false;

/** INIT */

// Init DOM controller system
function controlInit() {
// Start the data collector system
setInterval(function(){
var collectData = function(){
// Control the player options
userAction('getPlayerOptions', null, function(playerSuffle, playerLoop, playerCrossfade){
$('#shuffle').attr('class', playerSuffle);
$('#loop').attr('class', playerLoop);
$('#crossfade').attr('class', playerCrossfade);
});
}, 1000);

// Control the now playing data
userAction('getNowPlaying', null, function(songName, artistName, albumName, albumImage,
playbackPosition, playbackDuration, inLibrary, inFavorite, isSmile, isFrown,
queueIndex, queueLength){
// Configure song data
$('.nowPlaying .song')
.text(songName)
.attr('title', songName);
$('.nowPlaying .artist')
.text(artistName)
.attr('title', artistName);
$('.nowPlaying .album')
.text(albumName)
.attr('title', albumName);

// Configure album image
$('.nowPlaying .image')
.attr('src', albumImage);

// Configure text time
$('.nowPlaying .timeElapsed').text(msToHumanTime(playbackPosition));
$('.nowPlaying .timeDuration').text(msToHumanTime(playbackDuration));

// Configure song preferences
$('.nowPlaying .library').toggleClass('disable', !inLibrary);
$('.nowPlaying .favorite').toggleClass('disable', !inFavorite);
$('.nowPlaying .smile').toggleClass('active', isSmile);
$('.nowPlaying .frown').toggleClass('active', isFrown);

// Configure queue
$('.nowPlaying .position .queuePosition').text(queueIndex + 1);
$('.nowPlaying .position .queueCountSongs').text(queueLength);

// Configure progress bar
var percentage = Math.round(100 / playbackDuration * playbackPosition);
$('.progressbar .elapsed').css('width', percentage + '%');
$('.progressbar').slider('value', percentage);
});
}

// Start the data collector system
setInterval(collectData, 1000);
collectData();
}


Expand Down Expand Up @@ -176,40 +215,6 @@ function isNotificationOpen () {

/***** SETTERS *****/

function setNowPlaying (request) {
$('.nowPlaying .song').text(request.currentSong.SongName);
$('.nowPlaying .song').attr('title', request.currentSong.SongName);
$('.nowPlaying .artist').text(request.currentSong.ArtistName);
$('.nowPlaying .artist').attr('title', request.currentSong.ArtistName);
$('.nowPlaying .album').text(request.currentSong.AlbumName);
$('.nowPlaying .album').attr('title', request.currentSong.AlbumName);

$('.nowPlaying .image').attr('src', request.currentSong.imageUrlS || DEFAULT_ALBUM_IMAGE);

$('.nowPlaying .timeElapsed').text(msToHumanTime(request.playbackStatus.position));
$('.nowPlaying .timeDuration').text(msToHumanTime(request.playbackStatus.duration));

if (request.currentSong.inLibrary) $('.nowPlaying .library').removeClass('disable');
else $('.nowPlaying .library').addClass('disable');

if (request.currentSong.isFavorite) $('.nowPlaying .favorite').removeClass('disable');
else $('.nowPlaying .favorite').addClass('disable');

if (request.currentSong.smile) $('.nowPlaying .smile').addClass('active');
else $('.nowPlaying .smile').removeClass('active');

if (request.currentSong.frown) $('.nowPlaying .frown').addClass('active');
else $('.nowPlaying .frown').removeClass('active');

$('.nowPlaying .position .queuePosition').text(request.queue.queuePosition);
$('.nowPlaying .position .queueCountSongs').text(request.queue.songs.length);

$('.progressbar .elapsed').css('width', request.playbackStatus.percent + '%');
if (updateProgressbar) {
$('.progressbar').slider('value', request.playbackStatus.percent);
}
}

function setPlaylist (request) {
var playlistItems = $('.playlist');
playlistItems.empty();
Expand Down Expand Up @@ -260,11 +265,7 @@ function setUpProgressbar () {
$('.progressbar').slider({
step: 0.1,
stop: function(event, ui) {
updateProgressbar = false;
userAction('seekTo', [$(this).slider('value')]);
setTimeout(function () {
updateProgressbar = true;
}, 500);
}
});
}
1 change: 0 additions & 1 deletion javascript/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ function setUpNotification () {

chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
setNowPlaying(request);
setRadio(request);

$('#playpause').attr('class', request.isPlaying ? 'pause' : 'play');
Expand Down
1 change: 0 additions & 1 deletion javascript/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ chrome.extension.onRequest.addListener(

showPopup();

setNowPlaying(request);
setPlaylist(request);
setRadio(request);

Expand Down

0 comments on commit a74b89e

Please sign in to comment.