scRYMble

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

当前为 2014-05-29 提交的版本,查看 最新版本

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