BitFn's Better Deckbox Script

A better Deckbox.org experience.

  1. // ==UserScript==
  2. // @name BitFn's Better Deckbox Script
  3. // @namespace dlras.net
  4. // @version 0.1
  5. // @description A better Deckbox.org experience.
  6. // @author Daniel Rasmussen
  7. // @match *://deckbox.org/*
  8. // @grant none
  9. // @require http://code.jquery.com/jquery-latest.js
  10. // @require http://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js
  11. // ==/UserScript==
  12. $.noConflict();
  13. function spriteToClass(spriteName, className, remove) {
  14. jQuery('li.submenu_entry.deck img.'+spriteName).each(function() {
  15. var $this = jQuery(this);
  16. var $li = $this.closest('li.submenu_entry.deck');
  17. if (remove) $this.remove();
  18. $li.addClass(className);
  19. });
  20. }
  21.  
  22. function colorPrices($prices) {
  23. var getPrice = function($this) {
  24. return Number($this.text().replace(/[^0-9\.]+/g, ''));
  25. }
  26. var max = 0;
  27. var sum = 0;
  28. var count = 0;
  29. $prices.each(function() {
  30. var price = getPrice(jQuery(this));
  31. if (price === 0) return;
  32. count++;
  33. sum += price;
  34. if (price > max) max = price;
  35. });
  36. if (count === 0) return;
  37. var avg = sum / count;
  38. $prices.each(function() {
  39. var $this = jQuery(this);
  40. var price = getPrice($this);
  41. if (price === 0) return;
  42. $this.attr('data-title',$this.text());
  43. if (price < 1) {
  44. //$this.text(Math.round(price*100)+'¢');
  45. $this.text('¢');
  46. $this.addClass('muted');
  47. } else {
  48. $this.text('$'+Math.round(price));
  49. }
  50. });
  51. }
  52. function addGlobalStyle(css) {
  53. var head, style;
  54. head = document.getElementsByTagName('head')[0];
  55. if (!head) return;
  56. style = document.createElement('style');
  57. style.type = 'text/css';
  58. style.innerHTML = css;
  59. head.appendChild(style);
  60. }
  61. (function($) {
  62. addGlobalStyle('.muted { color:#ccc; }');
  63. $(document).on('click', 'a[target=_blank][href*="/mtg/"]', function(e) {
  64. var url = 'http://magiccards.info/query?q=!'+encodeURIComponent($(this).text().toLowerCase());
  65. window.open(url);
  66. e.preventDefault();
  67. return false;
  68. });
  69. spriteToClass('s_lightbulb', 'idea', true);
  70. spriteToClass('s_brick', 'built');
  71. spriteToClass('s_delete', 'private');
  72. spriteToClass('s_key', 'protected');
  73. $('.submenu_entry.deck [data-title], .submenu_entry [id^=folder_name][data-title]').html(function() {
  74. var $this = $(this);
  75. return $this.html().replace($this.text(), $this.data('title'));
  76. });
  77. $('td.card_count').each(function() {
  78. var $this = $(this);
  79. if ($this.text().trim() === '0') $this.addClass('muted');
  80. });
  81. colorPrices($('td.price:not(.price_min):not(.price_avg)'));
  82. colorPrices($('td.price_min'));
  83. colorPrices($('td.price_avg'));
  84. $('[id^=time_]').each(function() {
  85. var $this = $(this);
  86. var text = $this.text();
  87. $this.attr('data-title',text);
  88. $this.text(moment(text, 'DD-MMM-YYYY HH:mm').fromNow());
  89. });
  90. $('table.set_cards').each(function() {
  91. var $this = $(this);
  92. var $pTh, $tTh;
  93. $('th a', $this).each(function() {
  94. var $this = $(this);
  95. var text = $this.text().trim();
  96. if (text === 'P') $pTh = $this.parent();
  97. if (text === 'T') $tTh = $this.parent();
  98. });
  99. if (!$pTh || !$tTh) return;
  100. var pIndex = $('th', $this).index($pTh);
  101. var tIndex = $('th', $this).index($tTh);
  102. if (pIndex+1!=tIndex) return;
  103. $('tr[id]', $this).each(function() {
  104. var $this = $(this);
  105. var $pTd = $('td:eq('+pIndex+')', $this);
  106. var $tTd = $('td:eq('+tIndex+')', $this);
  107. var p = $pTd.text().trim();
  108. var t = $tTd.text().trim();
  109. if (p && t) $pTd.text(p+'\xA0/\xA0'+t);
  110. $pTd.attr('colspan','2');
  111. $tTd.remove();
  112. });
  113. });
  114. })(jQuery);