Grooveshark window.GS object problem

in Script development
Subscribe to Grooveshark window.GS object problem 2 posts, 2 voices



fenuks Scriptwright
FirefoxX11

Hi,
I have a problem, probably newbie problem. I wrote simple script to remove duplicated songs in player queue which is improved version of script from https://gist.github.com/1021044/43550fb2b439a65.... It works fine, but only when I run it from Firebug console (reduce queue from 967 songs to 560). When I run it as Greasemonkey script I got undefined object window.GS error. I tried to delay script executing, but I didn't solve problem. Here's the code:

// ==UserScript==
// @name           Grooveshark Duplicates Remover
// @namespace      grooverem
// @description    Removes duplicated songs from playing queue
// @include        http://*grooveshark.com*
// @version 0.1
// @icon http://grooveshark.com/webincludes/images/favicon64.ico
// ==/UserScript==
function rem() {
	song_hash = {};
	removal_list = [];
	var song;
	var str;
	for(var i = 0; i < window.GS.player.queue.songs.length; i++) {
		song = window.GS.player.queue.songs[i];
		str=song["SongName"].toLowerCase();
		if(song_hash[str] == undefined) {
			song_hash[str] = 1;
		} 
		else {
			removal_list.push(song["queueSongID"]);
		}
	}
	
	window.GS.player.removeSongs(removal_list);

}
rem();

I appreciate any help.

 
SBscripts Scriptwright
FirefoxWindows

In Greasemonkey, you can't access page JavaScript directly for security reasons. You can try to use unsafeWindow (with your code, this will allow any web page with grooveshark.com in the URL to run arbitrary JavaScript with the same privileges as Greasemonkey). You can also try to inject the script directly into the page; this might be better in your case.

Try this code:

// ==UserScript==
// @name           Grooveshark Duplicates Remover
// @namespace      grooverem
// @description    Removes duplicated songs from playing queue
// @include        http://*grooveshark.com*
// @version 0.1
// @icon http://grooveshark.com/webincludes/images/favicon64.ico
// ==/UserScript==
/*
    @description    This function will insert the given code as a <style> block into a page.
    @param The code to insert; supported types are: JavaScript Function, String (JavaScript), String (CSS).
    @param2 Optional: The type of code that is inserted. If omitted, "js" is assumed. Possible values are 'js' or 'css'.
    @return The HTML element that was inserted, or FALSE on failure
*/
function insert(z,t){
    var j,f,x,c,i,n,d
    d=document
    c=d.createElement
    i=d.head.appendChild
    a=d.createTextNode
    if(typeof z==='function') j=!0,f=!0;
    if((t=='js'||!t)&&!f){j=!0,f=!1}
    if(t=='css'&&!j){x=c('style');x.setAttribute('type','text/css')}
    if(j){x=c('script');x.setAttribute('type','text/javascript')}
    if(f) n=a('('+z+')()');else n=a(z)
    x.appendChild(n)

    if(x){return i(x)}else{return !1}
}


insert(function(){
	song_hash = {};
	removal_list = [];
	var song;
	var str;
	for(var i = 0; i < window.GS.player.queue.songs.length; i++) {
		song = window.GS.player.queue.songs[i];
		str=song["SongName"].toLowerCase();
		if(song_hash[str] == undefined) {
			song_hash[str] = 1;
		} 
		else {
			removal_list.push(song["queueSongID"]);
		}
	}
	
	window.GS.player.removeSongs(removal_list);

});