Skip to content

Commit

Permalink
merge, preview.grooveshark.com
Browse files Browse the repository at this point in the history
  • Loading branch information
horejsek committed Oct 16, 2011
1 parent fd41eee commit e460ba9
Show file tree
Hide file tree
Showing 7 changed files with 374 additions and 396 deletions.
161 changes: 77 additions & 84 deletions javascript/background.js
Expand Up @@ -13,93 +13,86 @@ function init () {
periodicDataGetter(callbackIfGroovesharkIsNotOpen=resetIcon);
injectGrooveshark();

// Performance: store last 19 percentage status
// If null, will render the new icon
var last19;
// Performance: store last 19 percentage status
// If null, will render the new icon
var last19;

// Performance: save the last title to avoid use Chrome API
var lastTitle;
// Performance: save the last title to avoid use Chrome API
var lastTitle;

// Performance: preload the play-icon
var playImage = new Image();
playImage.src = ICONS['playing'];
// Performance: preload the play-icon
var playImage = new Image();
playImage.src = ICONS['playing'];

// Start the data collector system
setInterval(function(){
// Update the badgeIcon baseed on player percentage
// UNAVAILABLE, STOPPED or percentage (float)
userAction('getCurrentPercentage', null, function(percentage){
// If playlist is empty
if (percentage === 'UNAVAILABLE') {
last19 = null;
return resetIcon();
}

// If player is paused
if (percentage === 'STOPPED') {
last19 = null;
return setIcon(ICONS['pause']);;
}

// Else, calcule the new last19
// If is different of the old last19, render the new icon
var new_last19 = Math.round(percentage / (100 / 19));
if (new_last19 !== last19) {
last19 = new_last19;

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

context.clearRect(0, 0, 19, 19);
context.drawImage(playImage, 0, 0);

context.fillStyle = '#CCC';
context.fillRect(0, 17, 19, 19);

context.fillStyle = '#000';
context.fillRect(0, 17, new_last19, 19);

chrome.browserAction.setIcon({
imageData: context.getImageData(0, 0, 19, 19)
});
}
});

// Update badgeTitle - the song name, if avaiable
userAction('getCurrentSongData', null, function(songName, songArtist){
// If playlist is empty
if (songName === 'UNAVAILABLE') {
lastTitle = null;
return resetTitle();
}

// Else, set the new title info
var new_lastTitle = songName + ' - ' + songArtist;
if (new_lastTitle !== lastTitle) {
lastTitle = new_lastTitle;
chrome.browserAction.setTitle({title: new_lastTitle});
}
});

// Configure the active queue song id, and open the notification bar
userAction('getQueueSongId', null, function(queueSongId){
// If playlist is empty, set song queue ID to -1
if (queueSongId === 'UNAVAILABLE') {
activeQueueSongID = -1;
return;
}

// Auto-show notification system
if (queueSongId != -1
&& activeQueueSongID != -1
&& activeQueueSongID != queueSongId
&& last19 < 1) {
showNotification();
}

// Set the active queue song ID
activeQueueSongID = queueSongId;
});
setInterval(function () {
// Update the badgeIcon baseed on player percentage
// UNAVAILABLE, STOPPED or percentage (float)
userAction('getCurrentPercentage', null, function(percentage){
if (percentage === 'UNAVAILABLE') {
last19 = null;
return resetIcon();
}

if (percentage === 'STOPPED') {
last19 = null;
return setIcon(ICONS['pause']);;
}

// If is different of the old last19, render the new icon
var new_last19 = Math.round(percentage / (100 / 19));
if (new_last19 !== last19) {
last19 = new_last19;

var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');

context.clearRect(0, 0, 19, 19);
context.drawImage(playImage, 0, 0);

context.fillStyle = '#CCC';
context.fillRect(0, 17, 19, 19);

context.fillStyle = '#000';
context.fillRect(0, 17, new_last19, 19);

chrome.browserAction.setIcon({
imageData: context.getImageData(0, 0, 19, 19)
});
}
});

// Update badgeTitle - the song name, if avaiable
userAction('getCurrentSongData', null, function (songName, songArtist) {
if (songName === 'UNAVAILABLE') {
lastTitle = null;
return resetTitle();
}

var new_lastTitle = songName + ' - ' + songArtist;
if (new_lastTitle !== lastTitle) {
lastTitle = new_lastTitle;
chrome.browserAction.setTitle({title: new_lastTitle});
}
});

// Configure the active queue song id, and open the notification bar
userAction('getQueueSongId', null, function(queueSongId){
if (queueSongId === 'UNAVAILABLE') {
activeQueueSongID = -1;
return;
}

// Auto-show notification system
if (queueSongId != -1
&& activeQueueSongID != -1
&& activeQueueSongID != queueSongId
&& last19 < 1) {
showNotification();
}

activeQueueSongID = queueSongId;
});
}, 1000);
}

Expand All @@ -112,7 +105,7 @@ function setIcon (icon) {
}

function resetTitle () {
chrome.browserAction.setTitle({title: 'Grooveshark Control'});
chrome.browserAction.setTitle({title: 'Grooveshark Control'});
}

function injectGrooveshark () {
Expand Down

0 comments on commit e460ba9

Please sign in to comment.