scRYMble

Visit a release page on rateyourmusic.com and scrobble the songs you see!

  1. // ==UserScript==
  2. // @name scRYMble
  3. // @namespace http://bluetshirt.ca/scrymble
  4. // @description Visit a release page on rateyourmusic.com and scrobble the songs you see!
  5. // @include http://rateyourmusic.com/release/*
  6. // @include https://rateyourmusic.com/release/*
  7. // @version 0.0.1.20140717152423
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_xmlhttpRequest
  11. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js
  12.  
  13. // ==/UserScript==
  14.  
  15. //THANK YOU FOR THE NAME, LYNKALI!
  16. //THANKS TO 5thEye, AnniesBoobs, BruceWayne, and _Andrew_ for some useful tweaks and bug fixes.
  17.  
  18.  
  19. /*
  20. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  21. * Digest Algorithm, as defined in RFC 1321.
  22. * Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
  23. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  24. * Distributed under the BSD License
  25. * See http://pajhome.org.uk/crypt/md5 for more info.
  26. */
  27.  
  28. /*
  29. * Configurable variables. You may need to tweak these to be compatible with
  30. * the server-side, but the defaults work in most cases.
  31. */
  32. var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
  33. var b64pad = ""; /* base-64 pad character. "=" for strict RFC compliance */
  34. var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
  35.  
  36. /*
  37. * These are the functions you'll usually want to call
  38. * They take string arguments and return either hex or base-64 encoded strings
  39. */
  40. function hex_md5(s){ return binl2hex(core_md5(str2binl(s), s.length * chrsz));}
  41. function b64_md5(s){ return binl2b64(core_md5(str2binl(s), s.length * chrsz));}
  42. function str_md5(s){ return binl2str(core_md5(str2binl(s), s.length * chrsz));}
  43. function hex_hmac_md5(key, data) { return binl2hex(core_hmac_md5(key, data)); }
  44. function b64_hmac_md5(key, data) { return binl2b64(core_hmac_md5(key, data)); }
  45. function str_hmac_md5(key, data) { return binl2str(core_hmac_md5(key, data)); }
  46.  
  47.  
  48. /*
  49. * Perform a simple self-test to see if the VM is working
  50. */
  51. function md5_vm_test()
  52. {
  53. return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
  54. }
  55.  
  56. /*
  57. * Calculate the MD5 of an array of little-endian words, and a bit length
  58. */
  59. function core_md5(x, len)
  60. {
  61. /* append padding */
  62. x[len >> 5] |= 0x80 << ((len) % 32);
  63. x[(((len + 64) >>> 9) << 4) + 14] = len;
  64. var a = 1732584193;
  65. var b = -271733879;
  66. var c = -1732584194;
  67. var d = 271733878;
  68. for(var i = 0; i < x.length; i += 16)
  69. {
  70. var olda = a;
  71. var oldb = b;
  72. var oldc = c;
  73. var oldd = d;
  74. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  75. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  76. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  77. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  78. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  79. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  80. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  81. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  82. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  83. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  84. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  85. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  86. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  87. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  88. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  89. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  90. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  91. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  92. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  93. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  94. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  95. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  96. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  97. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  98. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  99. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  100. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  101. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  102. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  103. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  104. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  105. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  106. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  107. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  108. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  109. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  110. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  111. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  112. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  113. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  114. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  115. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  116. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  117. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  118. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  119. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  120. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  121. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  122. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  123. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  124. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  125. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  126. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  127. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  128. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  129. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  130. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  131. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  132. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  133. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  134. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  135. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  136. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  137. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  138. a = safe_add(a, olda);
  139. b = safe_add(b, oldb);
  140. c = safe_add(c, oldc);
  141. d = safe_add(d, oldd);
  142. }
  143. return Array(a, b, c, d);
  144. }
  145.  
  146. /*
  147. * These functions implement the four basic operations the algorithm uses.
  148. */
  149. function md5_cmn(q, a, b, x, s, t)
  150. {
  151. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  152. }
  153. function md5_ff(a, b, c, d, x, s, t)
  154. {
  155. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  156. }
  157. function md5_gg(a, b, c, d, x, s, t)
  158. {
  159. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  160. }
  161. function md5_hh(a, b, c, d, x, s, t)
  162. {
  163. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  164. }
  165. function md5_ii(a, b, c, d, x, s, t)
  166. {
  167. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  168. }
  169.  
  170. /*
  171. * Calculate the HMAC-MD5, of a key and some data
  172. */
  173. function core_hmac_md5(key, data)
  174. {
  175. var bkey = str2binl(key);
  176. if(bkey.length > 16) bkey = core_md5(bkey, key.length * chrsz);
  177. var ipad = Array(16), opad = Array(16);
  178. for(var i = 0; i < 16; i++)
  179. {
  180. ipad[i] = bkey[i] ^ 0x36363636;
  181. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  182. }
  183. var hash = core_md5(ipad.concat(str2binl(data)), 512 + data.length * chrsz);
  184. return core_md5(opad.concat(hash), 512 + 128);
  185. }
  186.  
  187. /*
  188. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  189. * to work around bugs in some JS interpreters.
  190. */
  191. function safe_add(x, y)
  192. {
  193. var lsw = (x & 0xFFFF) + (y & 0xFFFF);
  194. var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  195. return (msw << 16) | (lsw & 0xFFFF);
  196. }
  197.  
  198. /*
  199. * Bitwise rotate a 32-bit number to the left.
  200. */
  201. function bit_rol(num, cnt)
  202. {
  203. return (num << cnt) | (num >>> (32 - cnt));
  204. }
  205.  
  206. /*
  207. * Convert a string to an array of little-endian words
  208. * If chrsz is ASCII, characters >255 have their hi-byte silently ignored.
  209. */
  210. function str2binl(str)
  211. {
  212. var bin = Array();
  213. var mask = (1 << chrsz) - 1;
  214. for(var i = 0; i < str.length * chrsz; i += chrsz)
  215. bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  216. return bin;
  217. }
  218.  
  219. /*
  220. * Convert an array of little-endian words to a string
  221. */
  222. function binl2str(bin)
  223. {
  224. var str = "";
  225. var mask = (1 << chrsz) - 1;
  226. for(var i = 0; i < bin.length * 32; i += chrsz)
  227. str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  228. return str;
  229. }
  230.  
  231. /*
  232. * Convert an array of little-endian words to a hex string.
  233. */
  234. function binl2hex(binarray)
  235. {
  236. var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
  237. var str = "";
  238. for(var i = 0; i < binarray.length * 4; i++)
  239. {
  240. str += hex_tab.charAt((binarray[i>>2] >> ((i%4)*8+4)) & 0xF) +
  241. hex_tab.charAt((binarray[i>>2] >> ((i%4)*8 )) & 0xF);
  242. }
  243. return str;
  244. }
  245.  
  246. /*
  247. * Convert an array of little-endian words to a base-64 string
  248. */
  249. function binl2b64(binarray)
  250. {
  251. var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  252. var str = "";
  253. for(var i = 0; i < binarray.length * 4; i += 3)
  254. {
  255. var triplet = (((binarray[i >> 2] >> 8 * ( i %4)) & 0xFF) << 16)
  256. | (((binarray[i+1 >> 2] >> 8 * ((i+1)%4)) & 0xFF) << 8 )
  257. | ((binarray[i+2 >> 2] >> 8 * ((i+2)%4)) & 0xFF);
  258. for(var j = 0; j < 4; j++)
  259. {
  260. if(i * 8 + j * 6 > binarray.length * 32) str += b64pad;
  261. else str += tab.charAt((triplet >> 6*(3-j)) & 0x3F);
  262. }
  263. }
  264. return str;
  265. }
  266.  
  267. function getElementsByClassName(node, classname)
  268. {
  269. var a = [];
  270. var re = new RegExp('\\b' + classname + '\\b');
  271. var els = node.getElementsByTagName("*");
  272. for(var i=0,j=els.length; i<j; i++)
  273. if(re.test(els[i].className))a.push(els[i]);
  274. return a;
  275. }
  276.  
  277. String.prototype.trim = function() {
  278. return this.replace(/^\s+|\s+$/g,"");
  279. }
  280. String.prototype.ltrim = function() {
  281. return this.replace(/^\s+/,"");
  282. }
  283. String.prototype.rtrim = function() {
  284. return this.replace(/\s+$/,"");
  285. }
  286.  
  287. toScrobble = new Array();
  288. var currentlyScrobbling = -1;
  289. var sessID = false;
  290. var submitURL = false;
  291. var npURL = false;
  292. var currTrackDuration = false;
  293. var currTrackPlaytime = false;
  294. var numChecks = 0;
  295.  
  296.  
  297. function confirmBrowseAway(oEvent)
  298. {
  299. if (currentlyScrobbling != -1)
  300. oEvent.returnValue = "You are currently scrobbling a record. Leaving the page now will prevent future tracks from this release from scrobbling.";
  301. }
  302.  
  303. function getPageArtist()
  304. {
  305. byartist = $('span[itemprop="byArtist"]');
  306. art_cred = $(byartist).find('.credited_name:eq(0) > span[itemprop="name"]');
  307. if ($(art_cred).length > 0){
  308. return $(art_cred).text();
  309. } else {
  310. return $(byartist).text();
  311. }
  312. }
  313.  
  314. function getAlbum()
  315. {
  316. return $(".album_title:eq(0)").text();
  317. }
  318.  
  319. function isVariousArtists()
  320. {
  321. var artist = getPageArtist();
  322. return ((artist.indexOf("Various Artists") > -1) || (artist.indexOf(" / ") > -1));
  323. }
  324.  
  325. function ScrobbleRecord(trackName, artist, duration)
  326. {
  327. this.artist = artist;
  328. this.trackName = trackName;
  329. var durastr = duration.trim();
  330. var colon = duration.indexOf(":");
  331. if (colon != -1)
  332. {
  333. var minutes = duration.substring(0, colon);
  334. var seconds = duration.substring(colon+1);
  335. this.duration = (minutes * 60) + (seconds * 1);
  336. } else
  337. {
  338. this.duration = 180;
  339. }
  340. this.time = 0;
  341. }
  342.  
  343. fetch_unix_timestamp = function()
  344. {
  345. return parseInt(new Date().getTime().toString().substring(0, 10))
  346. }
  347.  
  348. function acceptSubmitResponse(responseDetails, isBatch)
  349. {
  350. if (responseDetails.status == 200)
  351. {
  352. if (responseDetails.responseText.indexOf("OK") == -1)
  353. {
  354. alert("track submit failed: " + responseDetails.status +
  355. ' ' + responseDetails.statusText + '\n\n' +
  356. 'Data:\n' + responseDetails.responseText);
  357. } else
  358. {
  359. //alert("OK!");
  360. }
  361. } else
  362. {
  363. alert("track submit failed: " + responseDetails.status +
  364. ' ' + responseDetails.statusText + '\n\n' +
  365. 'Data:\n' + responseDetails.responseText);
  366. }
  367. if (isBatch)
  368. {
  369. document.getElementById("scrymblemarquee").innerHTML = "Scrobbled OK!";
  370. } else
  371. {
  372. scrobbleNextSong();
  373. }
  374. }
  375.  
  376. function acceptSubmitResponseSingle(responseDetails)
  377. {
  378. acceptSubmitResponse(responseDetails, false);
  379. }
  380.  
  381. function acceptSubmitResponseBatch(responseDetails)
  382. {
  383. acceptSubmitResponse(responseDetails, true);
  384. }
  385.  
  386.  
  387. function acceptNPResponse(responseDetails)
  388. {
  389. if (responseDetails.status == 200)
  390. {
  391. if (responseDetails.responseText.indexOf("OK") == -1)
  392. {
  393. alert("track submit failed: " + responseDetails.status +
  394. ' ' + responseDetails.statusText + '\n\n' +
  395. 'Data:\n' + responseDetails.responseText);
  396. } else
  397. {
  398. //alert("OK!");
  399. }
  400. } else
  401. {
  402. alert("track submit failed: " + responseDetails.status +
  403. ' ' + responseDetails.statusText + '\n\n' +
  404. 'Data:\n' + responseDetails.responseText);
  405. }
  406. }
  407.  
  408. function buildListOfSongsToScrobble()
  409. {
  410. toScrobble = new Array();
  411. var canscrobble = 1;
  412. $.each($('.scrymblechk'), function(){
  413. if ($(this).is(':checked')){
  414. song = $(this).parent().parent();
  415. var songTitle = $(song).find('span[itemprop="name"]').text();
  416. var artist = getPageArtist();
  417. var length = $(song).find('.tracklist_duration').text();
  418. ////
  419. if (isVariousArtists())
  420. {
  421. var firstDash = songTitle.indexOf(" - ");
  422. if(firstDash == -1) // no dash exists! must be a single artist with " / " in the name or v/a with unscrobbleable list
  423. {
  424. artist = getPageArtist();
  425. if (artist.indexOf("Various Artists") > -1)
  426. {
  427. artist = $(".album_title:eq(0)").text()
  428. //canscrobble = 0;
  429. }
  430. }
  431. else
  432. {
  433. artist = songTitle.substring(0, firstDash);
  434. songTitle = songTitle.substring(firstDash + 3);
  435. }
  436. }
  437. else
  438. {
  439. artist = getPageArtist()
  440. title = $(song).find('span[itemprop="name"]');
  441. if ($(title).html().indexOf('<a title="[Artist') == 0 && $(title).text().indexOf(' - ') > 0){
  442. var firstDash = songTitle.indexOf(" - ");
  443. artist = songTitle.substring(0, firstDash);
  444. songTitle = songTitle.substring(firstDash + 3);
  445. }
  446. }
  447. if((songTitle.toLowerCase() == "untitled") || (songTitle.toLowerCase() == "untitled track") || (songTitle == ""))
  448. {
  449. songTitle = "[untitled]";
  450. }
  451. ////
  452. while (songTitle.indexOf(' ') > 0){songTitle = songTitle.replace(' ', ' ')}
  453. toScrobble[toScrobble.length] = new ScrobbleRecord(songTitle, artist, length);
  454. }
  455. });
  456. }
  457.  
  458. function submitTracksBatch(sessID, submitURL)
  459. {
  460. buildListOfSongsToScrobble();
  461. if(toScrobble != null)
  462. {
  463. var currTime = fetch_unix_timestamp();
  464. var hoursFudge = prompt("How many hours ago did you listen to this?");
  465. if(hoursFudge != null)
  466. {
  467. var album = getAlbum();
  468. hoursFudge = parseFloat(hoursFudge);
  469. if (!isNaN(hoursFudge))
  470. {
  471. //alert(currTime);
  472. currTime = currTime - (hoursFudge * 60 * 60);
  473. //alert(currTime);
  474. }
  475. for (var i= (toScrobble.length)-1; i>=0; i--)
  476. {
  477. currTime = (currTime * 1) - (toScrobble[i].duration * 1);
  478. toScrobble[i].time = currTime;
  479. }
  480. var outstr = "Artist: " + getPageArtist() + "\nAlbum: " + album + "\n";
  481. for (var i=0; i<toScrobble.length; i++)
  482. {
  483. outstr += toScrobble[i].trackName + "(" + toScrobble[i].duration + ")\n";
  484. }
  485. //alert(outstr);
  486. var postdata = new Array();
  487. for (var i=0; i<toScrobble.length; i++)
  488. {
  489. postdata["a[" + i + "]"] = toScrobble[i].artist;
  490. postdata["t[" + i + "]"] = toScrobble[i].trackName;
  491. postdata["b[" + i + "]"] = album;
  492. postdata["n[" + i + "]"] = (i+1);
  493. postdata["l[" + i + "]"] = toScrobble[i].duration;
  494. postdata["i[" + i + "]"] = toScrobble[i].time;
  495. postdata["o[" + i + "]"] = "P";
  496. postdata["r[" + i + "]"] = "";
  497. postdata["m[" + i + "]"] = "";
  498. }
  499. postdata["s"] = sessID;
  500. var postdataStr = "";
  501. var firstTime = true;
  502. for (currKey in postdata)
  503. {
  504. if (firstTime)
  505. {
  506. firstTime = false;
  507. } else
  508. {
  509. postdataStr = postdataStr + "&";
  510. }
  511. postdataStr = postdataStr + encodeURIComponent(currKey) + "=" + encodeURIComponent(postdata[currKey]);
  512. }
  513. //alert(submitURL);
  514. GM_xmlhttpRequest({
  515. method: 'POST',
  516. url: submitURL,
  517. data: postdataStr,
  518. headers: {
  519. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  520. 'Content-type': 'application/x-www-form-urlencoded',
  521. },
  522. onload: acceptSubmitResponseBatch
  523. });
  524. }
  525. }
  526. }
  527.  
  528. function elementsOnAndOff(state)
  529. {
  530. document.getElementById("scrobblenow").disabled = !state;
  531. document.getElementById("scrobblepassword").disabled = !state;
  532. document.getElementById("scrobbleusername").disabled = !state;
  533. document.getElementById("scrobblepassword").disabled = !state;
  534. //var eleTrackTable = document.getElementById('tracks');
  535. //var rows = eleTrackTable.tBodies[0].rows;
  536. $.each($(".scrymblechk"), function() {
  537. try{
  538. $(this).disabled = !state;
  539. } catch (e)
  540. {
  541. }
  542. });
  543. }
  544.  
  545. function elementsOff()
  546. {
  547. elementsOnAndOff(false);
  548. }
  549.  
  550. function elementsOn()
  551. {
  552. elementsOnAndOff(true);
  553. }
  554.  
  555. function startScrobble()
  556. {
  557. currentlyScrobbling = -1;
  558. currTrackDuration = 0;
  559. currTrackPlayTime = 0;
  560. elementsOff();
  561. buildListOfSongsToScrobble();
  562. scrobbleNextSong();
  563. }
  564.  
  565.  
  566. function resetScrobbler()
  567. {
  568. currentlyScrobbling = -1;
  569. currTrackDuration = 0;
  570. currTrackPlayTime = 0;
  571. document.getElementById("scrymblemarquee").innerHTML = "&nbsp;";
  572. document.getElementById("progbar").style.width = "0%";
  573. toScrobble = new Array();
  574. elementsOn();
  575. numChecks = 0;
  576. }
  577.  
  578. function scrobbleNextSong()
  579. {
  580. currentlyScrobbling++;
  581. if(currentlyScrobbling == toScrobble.length)
  582. {
  583. resetScrobbler();
  584. } else
  585. {
  586. window.setTimeout(timertick, 10);
  587. handshake();
  588. }
  589. }
  590.  
  591. function submitThisTrack()
  592. {
  593. var postdata = new Array();
  594. var i = 0;
  595. var currTime = fetch_unix_timestamp();
  596. postdata["a[" + i + "]"] = toScrobble[currentlyScrobbling].artist;
  597. postdata["t[" + i + "]"] = toScrobble[currentlyScrobbling].trackName;
  598. postdata["b[" + i + "]"] = getAlbum();
  599. postdata["n[" + i + "]"] = (currentlyScrobbling+1);
  600. postdata["l[" + i + "]"] = toScrobble[currentlyScrobbling].duration;
  601. postdata["i[" + i + "]"] = currTime-toScrobble[currentlyScrobbling].duration;
  602. postdata["o[" + i + "]"] = "P";
  603. postdata["r[" + i + "]"] = "";
  604. postdata["m[" + i + "]"] = "";
  605. postdata["s"] = sessID;
  606. var postdataStr = "";
  607. var firstTime = true;
  608. for (currKey in postdata)
  609. {
  610. if (firstTime)
  611. {
  612. firstTime = false;
  613. } else
  614. {
  615. postdataStr = postdataStr + "&";
  616. }
  617. postdataStr = postdataStr + encodeURIComponent(currKey) + "=" + encodeURIComponent(postdata[currKey]);
  618. }
  619. GM_xmlhttpRequest({
  620. method: 'POST',
  621. url: submitURL,
  622. data: postdataStr,
  623. headers: {
  624. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  625. 'Content-type': 'application/x-www-form-urlencoded',
  626. },
  627. onload: acceptSubmitResponseSingle
  628. });
  629. }
  630.  
  631.  
  632.  
  633. function npNextTrack()
  634. {
  635. var postdata = new Array();
  636. var i = 0;
  637. var currTime = fetch_unix_timestamp();
  638. postdata["a"] = toScrobble[currentlyScrobbling].artist;
  639. postdata["t"] = toScrobble[currentlyScrobbling].trackName;
  640. postdata["b"] = getAlbum();
  641. postdata["n"] = (currentlyScrobbling+1);
  642. postdata["l"] = toScrobble[currentlyScrobbling].duration;
  643. postdata["m"] = "";
  644. postdata["s"] = sessID;
  645. currTrackDuration = toScrobble[currentlyScrobbling].duration;
  646. currTrackPlayTime = 0;
  647. document.getElementById("scrymblemarquee").innerHTML = toScrobble[currentlyScrobbling].trackName;
  648. var postdataStr = "";
  649. var firstTime = true;
  650. for (currKey in postdata)
  651. {
  652. if (firstTime)
  653. {
  654. firstTime = false;
  655. } else
  656. {
  657. postdataStr = postdataStr + "&";
  658. }
  659. postdataStr = postdataStr + encodeURIComponent(currKey) + "=" + encodeURIComponent(postdata[currKey]);
  660. }
  661. GM_xmlhttpRequest({
  662. method: 'POST',
  663. url: npURL,
  664. data: postdataStr,
  665. headers: {
  666. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
  667. 'Content-type': 'application/x-www-form-urlencoded',
  668. },
  669. onload: acceptNPResponse
  670. });
  671. }
  672.  
  673. function timertick()
  674. {
  675. var again = true;
  676. if (currentlyScrobbling != -1)
  677. {
  678. var progbar = document.getElementById("progbar");
  679. if (currTrackDuration != 0)
  680. {
  681. progbar.style.width = "" + (100*currTrackPlayTime/currTrackDuration) + "%";
  682. }
  683. currTrackPlayTime++;
  684. if (currTrackPlayTime == currTrackDuration)
  685. {
  686. submitThisTrack();
  687. again = false;
  688. }
  689. }
  690. if (again)
  691. {
  692. window.setTimeout(timertick,1000);
  693. }
  694. }
  695.  
  696. function acceptHandshakeSingle(responseDetails)
  697. {
  698. acceptHandshake(responseDetails, false);
  699. }
  700.  
  701. function acceptHandshakeBatch(responseDetails)
  702. {
  703. acceptHandshake(responseDetails, true);
  704. }
  705.  
  706. function acceptHandshake(responseDetails, isBatch)
  707. {
  708. if (responseDetails.status == 200)
  709. {
  710. var lines = responseDetails.responseText.split("\n");
  711. if (lines[0].indexOf("OK") == -1)
  712. {
  713. alert("handshake failed: " + responseDetails.status +
  714. ' ' + responseDetails.statusText + '\n\n' +
  715. 'Data:\n' + responseDetails.responseText);
  716. } else
  717. {
  718. sessID = lines[1];
  719. npURL = lines[2];
  720. submitURL = lines[3];
  721. if (isBatch)
  722. {
  723. submitTracksBatch(sessID, submitURL);
  724. } else
  725. {
  726. npNextTrack();
  727. }
  728. }
  729. } else
  730. {
  731. alert("handshake failed: " + responseDetails.status +
  732. ' ' + responseDetails.statusText + '\n\n' +
  733. 'Data:\n' + responseDetails.responseText);
  734. }
  735. }
  736.  
  737. function handshake(isBatch)
  738. {
  739. var user = document.getElementById("scrobbleusername").value;
  740. var password = document.getElementById("scrobblepassword").value;
  741. GM_setValue("user", user);
  742. GM_setValue("pass", password);
  743. var timestamp = fetch_unix_timestamp();
  744. var auth = hex_md5(hex_md5(password) + timestamp);
  745. var handshakeURL = "http://post.audioscrobbler.com/?hs=true&p=1.2&c=scr&v=1.0&u=" + user + "&t=" + timestamp + "&a=" + auth;
  746. GM_xmlhttpRequest({
  747. method: 'GET',
  748. url: handshakeURL,
  749. headers: {
  750. 'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey'
  751. },
  752. onload: (isBatch) ? acceptHandshakeBatch : acceptHandshakeSingle
  753. });
  754. }
  755.  
  756. function handshakeSingle()
  757. {
  758. handshake(false);
  759. }
  760.  
  761. function handshakeBatch()
  762. {
  763. handshake(true);
  764. }
  765.  
  766. var eleTrackTable, eleButtonDiv, eleScrobbleNow, eleAllOrNone;
  767. eleTrackTable = document.getElementById('tracks');
  768.  
  769.  
  770. if (eleTrackTable) {
  771. var n = 0;
  772. chkbox = '<span style="float:left;"><input type="checkbox" class="scrymblechk" id="chktrackNUM" checked="checked"></span>';
  773. $.each($("#tracks > .track > .tracklist_line"), function(){
  774. if ($(this).find('.tracklist_num:eq(0)').text() != '\n \n '){
  775. n++;
  776. $(this).prepend(chkbox.replace('NUM',n));
  777. }
  778. });
  779. }
  780.  
  781. eleButtonDiv = document.createElement('DIV');
  782. eleButtonDiv.innerHTML = "<table border='0' cellpadding='0' cellspacing='2'><tr><td width='105' ><input type='checkbox' name='allornone' id='allornone' style='vertical-align:middle' checked='checked'>&nbsp;<label for='allornone' style='font-size:60%'>select&nbsp;all/none</label><br/><table border='2' cellpadding='0' cellspacing='0'><tr><td style='height:50px;width:103px;background:url(http://cdn.last.fm/flatness/logo_black.3.png) no-repeat;color:#fff'><marquee scrollamount='3' scrolldelay='200' behavior='alternate' style='font-size:80%;font-family:sans-serif;position:relative;top:17px' id='scrymblemarquee'>&nbsp;</marquee></td></tr><tr><td style='background-color:#000033'><div style='position:relative;background-color:#ff0000;width:0%;max-height:5px;left:0px;top:0px;' id='progbar'>&nbsp;</div></td></tr></table></td><td>user: <input type='text' size='16' id='scrobbleusername' value = '"+ GM_getValue("user", "") + "' /><br />pass: <input type='password' size='16' id='scrobblepassword' value = '" + GM_getValue("pass", "") + "'></input><br /><input type='button' id='scrobblenow' value = 'Scrobble in real-time' /> <input type='button' id='scrobblethen' value = 'Scrobble a previous play' /></td></tr></table>";
  783. //eleButtonDiv.innerHTML = "<table border='2' cellpadding='0' cellspacing='2'><tr><td><img src='http://cdn.last.fm/flatness/logo_black.3.png'></td><td>user: <input type='text' size='10' id='scrobbleusername' value = '" + GM_getValue("user", "") + "'/><br />pass: <input type='password' size='10' id='scrobblepassword' value = '" + GM_getValue("pass", "") + "'/><br /><input type='button' id='scrobblenow' value='Scrobble!' /></td></tr></table>"
  784. eleButtonDiv.style.textAlign = "right";
  785.  
  786. var buttonDivParent = document.getElementById("h_album");
  787. //buttonDivParent.appendChild(eleButtonDiv);
  788. $(eleTrackTable).after(eleButtonDiv);
  789.  
  790. eleScrobbleNow = document.getElementById("scrobblenow");
  791. eleScrobbleNow.addEventListener("click", startScrobble, true);
  792.  
  793. eleAllOrNone = document.getElementById("allornone");
  794. eleAllOrNone.addEventListener("click", allOrNoneClick, true);
  795.  
  796. document.getElementById("scrobblethen").addEventListener("click", handshakeBatch, true);
  797.  
  798. window.addEventListener("beforeunload", confirmBrowseAway, true);
  799.  
  800.  
  801.  
  802.  
  803. function allOrNoneClick()
  804. {
  805. window.setTimeout(allOrNoneAction, 10);
  806. }
  807.  
  808. function allOrNoneAction()
  809. {
  810. var allnone = $("#allornone").is(':checked');
  811. $.each($(".scrymblechk"), function(){
  812. $(this).prop('checked', allnone);
  813. });
  814. }