Spelar via Spotify Spelar via YouTube
Hoppa till YouTube-video

Laddar spelare ...

Skrobbla från Spotify?

Anslut ditt Spotify-konto till ditt Last.fm-konto och skrobbla allt du lyssnar på från alla Spotify-appar på alla enheter eller plattformar.

Anslut till Spotify

Avvisa

Vill du inte se annonser? Uppgradera nu

Converting Greasemonkey scripts to work in Google Chrome

Google Chrome has some limited support for Greasemonkey scripts (by using the –enable-greasemonkey command line argument), so I have been looking at making some of my last.fm Greasemonkey scripts work in Chrome. There are a few limitations. I will go through each one, with their workarounds:

Chrome ignores script metadata including @include and @exclude
In Chrome all Greasemonkey scripts are applied to all pages. The workaround is to include checking in the script, either based on the URL or a unique tag id / class name known to be present on the correct page.

No GM_setValue GM_getValue
This means that by default there is no way of saving / loading settings and if a script uses these methods it will fail. A workaround is to use cookies. This can be added to your script to allow it to work in Chrome:

function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name,defaultvalue) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca;
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return defaultvalue;
}

if(!window.GM_setValue)
{
window.GM_setValue = function(valuename, value) { createCookie(valuename,value,365); };
}
if(!window.GM_getValue)
{
window.GM_getValue = function(valuename, defaultvalue) { return readCookie(valuename,defaultvalue); };
}

No unsafeWindow
This does not actually appear to be needed in Chrome, but will cause an error if used. Adding this should prevent errors:

if(!window.unsafeWindow)
{
window.unsafeWindow = window;
}

No cross-domain HTTP requests
The GM_xmlhttpRequest function does not work in Chrome. It is possible to use the xmlhttpRequest object, but this only works for the same domain. A workaround is to use a proxy which will convert the XML feed to JSON format. An example using a Yahoo Pipe is shown below:

// Gets XML feeds, converting them to JSON
// using the getXML Yahoo Pipe - http://pipes.yahoo.com/sanand/getxml
function getFeed(feed,callback) {
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
var tim = new Date();
newScript.src = 'http://pipes.yahoo.com/pipes/hrDp8A0v3RGr1Dm_yZ1_DQ/run?_render=json&_callback='; + callback + '&url='+escape(feed) + '&cache=' + tim.toGMTString();
document.getElementsByTagName("head").appendChild(newScript);
}

Vill du inte se annonser? Uppgradera nu

API Calls