Skip to content

Commit

Permalink
Added feature: addToCollection removeFromCollection fetchByName | Sta…
Browse files Browse the repository at this point in the history
…rtup is now much faster, using callback instead of timeout. Also fixed a bug where the broadcast would fail to take over properly | New awesome logo by Mullins
  • Loading branch information
UlysseM committed Apr 5, 2014
1 parent be367bb commit eeeb6ed
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 80 deletions.
57 changes: 39 additions & 18 deletions chrome/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,51 @@
});
}

var tabState = {};

var injectCode = function(tabId)
{
console.log(tabState[tabId]);
var regexRes = tabState[tabId];
delete tabState[tabId];

// prepare the injection of localStorage inside GUParams & content_script.js
var injection = 'document.body.appendChild(document.createElement(\'script\')).innerHTML='
+ JSON.stringify(
'var GUParams = JSON.parse(' + JSON.stringify(JSON.stringify(localStorage)) + ');'
+ 'GUParams.userReq = ' + JSON.stringify(regexRes[1] != undefined ? '' : (regexRes[3] != undefined ? regexRes[3] : localStorage.forceLoginUsername)) + ';'
+ 'GUParams.passReq = ' + JSON.stringify(regexRes[1] != undefined ? '' : (regexRes[5] != undefined ? regexRes[5] : localStorage.forceLoginPassword)) + ';'
+ 'GUParams.version = ' + JSON.stringify(chrome.app.getDetails().version) + ';'
) + ';'
+ "document.body.appendChild(document.createElement('script')).src='"
+ chrome.extension.getURL("content_script.js") +"';";
// inject the new script after 4 seconds on the new page.
chrome.tabs.executeScript(tabId, {code: injection}, null);
}

var callbackFunction = function(tabId, changeInfo, tab) {
var newUrl = changeInfo.url;
console.log('Loading...' + newUrl);
var usernameRegex = '[a-zA-Z0-9_-]+';
var regexRes = RegExp("http://broadcast(-nologin)?(/(" + usernameRegex + ")(/(.*)$)?)?").exec(newUrl);
if (changeInfo.status == 'loading' && regexRes != null)
if (changeInfo.status == 'loading')
{
if (localStorage.closeAllTabsOnStartup == 'true')
removeAllButId(tabId);
if (regexRes != null)
{
if (localStorage.closeAllTabsOnStartup == 'true')
removeAllButId(tabId);

var updateProperties = {'url': 'http://grooveshark.com/'};
chrome.tabs.update(tabId, updateProperties, function() {
// prepare the injection of localStorage inside GUParams & content_script.js
var injection = 'document.body.appendChild(document.createElement(\'script\')).innerHTML='
+ JSON.stringify(
'var GUParams = JSON.parse(' + JSON.stringify(JSON.stringify(localStorage)) + ');'
+ 'GUParams.userReq = ' + JSON.stringify(regexRes[1] != undefined ? '' : (regexRes[3] != undefined ? regexRes[3] : localStorage.forceLoginUsername)) + ';'
+ 'GUParams.passReq = ' + JSON.stringify(regexRes[1] != undefined ? '' : (regexRes[5] != undefined ? regexRes[5] : localStorage.forceLoginPassword)) + ';'
+ 'GUParams.version = ' + JSON.stringify(chrome.app.getDetails().version) + ';'
) + ';'
+ "document.body.appendChild(document.createElement('script')).src='"
+ chrome.extension.getURL("content_script.js") +"';";
// inject the new script after 4 seconds on the new page.
setTimeout(function() {chrome.tabs.executeScript(tabId, {code: injection}, null);}, 4000);
});
var updateProperties = {'url': 'http://grooveshark.com/'};
chrome.tabs.update(tabId, updateProperties);

console.log(tabState[tabId]);
tabState[tabId] = regexRes;
// chrome.tabs.update(tabId, updateProperties, injectCode);
}
else if (tabState.hasOwnProperty(tabId) && newUrl.indexOf('grooveshark') != -1)
{
injectCode(tabId);
}
}
};

Expand Down
105 changes: 76 additions & 29 deletions chrome/content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ var GU = {
{
Grooveshark.removeCurrentSongFromQueue();
},
'addToCollection': function()
{
Grooveshark.addCurrentSongToLibrary();
GU.sendMsg('Song added to the favorite.');
},
'removeFromCollection': function()
{
var currSong = Grooveshark.getCurrentSongStatus().song
GS.Services.API.userRemoveSongsFromLibrary(GS.getLoggedInUserID(), currSong.songID, currSong.albumID, currSong.artistID).then(function(){
GU.sendMsg('Song removed from the favorite.');
});
},
'deletePlayedSong': function()
{
var previousSong;
Expand Down Expand Up @@ -201,7 +213,7 @@ var GU = {
GS.Services.SWF.removeSongs([id]);
}
},
'getRemoveSongsList': function(stringFilter)
'getMatchedSongsList': function(stringFilter)
{
var regex = RegExp(stringFilter, 'i');
var songs = GU.getPlaylistNextSongs();
Expand All @@ -216,7 +228,7 @@ var GU = {
},
'previewRemoveByName': function(message, stringFilter)
{
var listToRemove = GU.getRemoveSongsList(stringFilter);
var listToRemove = GU.getMatchedSongsList(stringFilter);
if (listToRemove.length > 10 || listToRemove.length == 0)
GU.sendMsg('' + listToRemove.length + 'Songs matched.');
else
Expand All @@ -230,14 +242,20 @@ var GU = {
},
'removeByName': function(message, stringFilter)
{
var listToRemove = GU.getRemoveSongsList(stringFilter);
var listToRemove = GU.getMatchedSongsList(stringFilter);
var idToRemove = [];
listToRemove.forEach(function (element){
idToRemove.push(element.queueSongID);
});
GS.Services.SWF.removeSongs(idToRemove);
GU.sendMsg('Removed ' + idToRemove.length + ' songs.');
},
'fetchByName': function(message, stringFilter)
{
var songToPlay = GU.getMatchedSongsList(stringFilter);
if (songToPlay.length > 0)
GS.Services.SWF.moveSongsTo([songToPlay[0].queueSongID], 1, true);
},
'shuffle': function()
{
$('.shuffle').click();
Expand All @@ -261,6 +279,13 @@ var GU = {
{
return (current.find('a.favorite').hasClass('btn-success'));
},
'strictWhiteListCheck': function(current)
{
if (GU.inListCheck(current, GUParams.whitelist))
return true;
GU.sendMsg('Only user that are explicitly in the whitelist can use this feature, sorry!');
return false;
},
'whiteListCheck': function(current)
{
if (GU.inListCheck(current, GUParams.whitelist)) // user in whitelist
Expand Down Expand Up @@ -399,7 +424,7 @@ var GU = {
'startBroadcasting': function(bc)
{
var properties = { 'Description': bc.Description, 'Name': bc.Name, 'Tag': bc.Tag };
if (!GS.isBroadcaster()) {
if (GS.getCurrentBroadcast() === false) {
GS.Services.SWF.startBroadcast(properties);
setTimeout(GU.startBroadcasting, 3000, bc);
return;
Expand All @@ -414,6 +439,10 @@ var GU = {
GS.Services.API.userGetSongIDsInLibrary().then(function (result){
allSongsId = result.SongIDs;
});
if ($('#lightbox-close').length == 1)
{
$('#lightbox-close').click();
}
lastPlay = new Date();
// Check if there are msg in the chat, and process them.
setInterval(GU.callback, 1000);
Expand All @@ -425,38 +454,56 @@ var GU = {
else
{
GS.Services.API.getUserLastBroadcast().then(function(bc) {
if ($('#lightbox-close').length == 1)
GS.Services.SWF.ready.then(function()
{
$('#lightbox-close').click();
}
GS.Services.SWF.resumeBroadcast(bc.BroadcastID);
setTimeout(GU.startBroadcasting, 3000, bc);
GS.Services.SWF.resumeBroadcast(bc.BroadcastID);
setTimeout(GU.startBroadcasting, 3000, bc);
});
});
}
}
};

actionTable = {
'help': [[GU.inBroadcast], GU.help, '- Display this help.'],
'ping': [[GU.inBroadcast], GU.ping, '- Ping the BOT.'],
'removeNext': [[GU.inBroadcast, GU.guestCheck], GU.removeNextSong, '- Remove the next song in the queue.'],
'removeLast': [[GU.inBroadcast, GU.guestCheck], GU.removeLastSong, '- Remove the last song of the queue.'],
'previewRemoveByName': [[GU.inBroadcast, GU.guestCheck], GU.previewRemoveByName, '[FILTER] - Get the list of songs that will be remove when calling \'removeByName\' with the same FILTER.'],
'removeByName': [[GU.inBroadcast, GU.guestCheck], GU.removeByName, '[FILTER] - Remove all songs that matches the filter. If the filter if empty, remove everything. Use the \'previewRemoveByName\' first.'],
'showPlaylist': [[GU.inBroadcast, GU.guestCheck], GU.showPlaylist, '[FILTER] - Get the ID of a particular playlist.'],
'playPlaylist': [[GU.inBroadcast, GU.guestCheck], GU.playPlaylist, 'PLAYLISTID - Play the playlist from the ID given by \'showPlaylist\'.'],
'skip': [[GU.inBroadcast, GU.guestCheck], GU.skip, '- Skip the current song.'],
'shuffle': [[GU.inBroadcast, GU.guestCheck], GU.shuffle, '- Shuffle the current queue.'],
'peek': [[GU.inBroadcast, GU.whiteListCheck], GU.previewSongs, '[NUMBER] - Preview the songs that are in the queue.'],
'guest': [[GU.inBroadcast, GU.whiteListCheck], GU.guest, '- Toogle your guest status.'],
'about': [[GU.inBroadcast], GU.about, '- About this software.']
'help': [[GU.inBroadcast], GU.help, '- Display this help.'],
'ping': [[GU.inBroadcast], GU.ping, '- Ping the BOT.'],
'addToCollection': [[GU.inBroadcast, GU.strictWhiteListCheck], GU.addToCollection, '- Add this song to the collection.'],
'removeFromCollection': [[GU.inBroadcast, GU.strictWhiteListCheck], GU.removeFromCollection, '- Remove this song from the collection.'],
'removeNext': [[GU.inBroadcast, GU.guestCheck], GU.removeNextSong, '- Remove the next song in the queue.'],
'removeLast': [[GU.inBroadcast, GU.guestCheck], GU.removeLastSong, '- Remove the last song of the queue.'],
'fetchByName': [[GU.inBroadcast, GU.guestCheck], GU.fetchByName, '[FILTER] - Place the first song of the queue that matches FILTER at the beginning of the queue.'],
'previewRemoveByName': [[GU.inBroadcast, GU.guestCheck], GU.previewRemoveByName, '[FILTER] - Get the list of songs that will be remove when calling \'removeByName\' with the same FILTER.'],
'removeByName': [[GU.inBroadcast, GU.guestCheck], GU.removeByName, '[FILTER] - Remove all songs that matches the filter. If the filter if empty, remove everything. Use the \'previewRemoveByName\' first.'],
'showPlaylist': [[GU.inBroadcast, GU.guestCheck], GU.showPlaylist, '[FILTER] - Get the ID of a particular playlist.'],
'playPlaylist': [[GU.inBroadcast, GU.guestCheck], GU.playPlaylist, 'PLAYLISTID - Play the playlist from the ID given by \'showPlaylist\'.'],
'skip': [[GU.inBroadcast, GU.guestCheck], GU.skip, '- Skip the current song.'],
'shuffle': [[GU.inBroadcast, GU.guestCheck], GU.shuffle, '- Shuffle the current queue.'],
'peek': [[GU.inBroadcast, GU.whiteListCheck], GU.previewSongs, '[NUMBER] - Preview the songs that are in the queue.'],
'guest': [[GU.inBroadcast, GU.whiteListCheck], GU.guest, '- Toogle your guest status.'],
'about': [[GU.inBroadcast], GU.about, '- About this software.']
};

if (GUParams.userReq != '' && GUParams.passReq != '')
(function()
{
GS.Services.API.logoutUser().then(function(){
GS.Services.API.authenticateUser(GUParams.userReq, GUParams.passReq).then(function(user) { document.body.innerHTML = ''; setTimeout(function(){window.location = "http://broadcast-nologin/";}, 200); } );
});
}
else
GU.broadcast();
var callback_start = function()
{
onbeforeunload = null;
if (GUParams.userReq != '' && GUParams.passReq != '')
{
GS.Services.API.logoutUser().then(function(){
GS.Services.API.authenticateUser(GUParams.userReq, GUParams.passReq).then(function(user) { window.location = "http://broadcast-nologin/";});
});
}
else
GU.broadcast();
}
var init_check = function ()
{
try {
GS.ready.done(callback_start);
} catch(e) {
setTimeout(init_check, 100);
}
}
init_check();
})()
Binary file modified chrome/icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chrome/icon16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chrome/icon48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Grooveshark Broadcast Bot",
"short_name": "GS Bot",
"description": "Create or takeover a broadcast with this bot, then enjoy what the bot can offer!",
"version": "1.3.0",
"version": "1.3.1",
"icons": { "16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png" },
Expand Down
Loading

0 comments on commit eeeb6ed

Please sign in to comment.