steamgifts.com improved game filter

Makes hiding giveaways for specific games much quicker and easier

当前为 2016-04-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name steamgifts.com improved game filter
  3. // @description Makes hiding giveaways for specific games much quicker and easier
  4. // @namespace Barefoot Monkey
  5. // @include https://www.steamgifts.com/
  6. // @include https://www.steamgifts.com/giveaways/search?*
  7. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  8. // @version 2
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant unsafeWindow
  12. // @noframes
  13. // ==/UserScript==
  14.  
  15. var version = 2
  16. var OLD_VERSION_KEY = 'previous version'
  17.  
  18. $('<style>')
  19. .text(
  20. '.BarefootMonkey-popup-bg {'
  21. +' background: #3C424D;'
  22. +' position: fixed;'
  23. +' top: 0;'
  24. +' bottom: 0;'
  25. +' left: 0;'
  26. +' right: 0;'
  27. +' opacity: 0.85;'
  28. +' z-index: 9988;'
  29. +' cursor: pointer;'
  30. +'}'
  31.  
  32. +'.BarefootMonkey-popup ul {'
  33. +' list-style: inside;'
  34. +'}'
  35. +'.BarefootMonkey-popup p {'
  36. +' margin: 0.7em 0;'
  37. +'}'
  38.  
  39. +'.BarefootMonkey-popup h1 i {'
  40. +' font-size: 0.5em;'
  41. +' font-weight: bold;'
  42. +'}'
  43.  
  44. +'.BarefootMonkey-popup a {'
  45. +' color: #4B72D4;'
  46. +' font-weight: bold;'
  47. +'}'
  48. +'.BarefootMonkey-popup em {'
  49. +' font-style: italic;'
  50. +'}'
  51. +'.BarefootMonkey-popup h1 {'
  52. +' font-size: 1.5em;'
  53. +' margin: 0px 0px 0.5em;'
  54. +' border-bottom: 1px solid #6B7A8C;'
  55. +' padding-bottom: 0.5em;'
  56. +'}'
  57. +'.BarefootMonkey-popup key, .BarefootMonkey-popup b {'
  58. +' font-weight: bold;'
  59. +'}'
  60. +'.BarefootMonkey-popup {'
  61. +' background: #F0F2F5 none repeat scroll 0% 0%;'
  62. +' z-index: 9999;'
  63. +' position: fixed;'
  64. +' left: calc(50vw - 18em);'
  65. +' top: 20vh;'
  66. +' border-radius: 4px;'
  67. +' color: #6B7A8C;'
  68. +' font: 300 17px "Open Sans",sans-serif;'
  69. +' padding: 1em;'
  70. +' width: 36em;'
  71. +' max-height: calc(80vh - 3em);'
  72. +' overflow: auto;'
  73. +'}'
  74. )
  75. .appendTo(document.head)
  76.  
  77. function cmpVersion(a, b) {
  78. var i, cmp, len, re = /(\.0)+[^\.]*$/;
  79. a = (a + '').replace(re, '').split('.');
  80. b = (b + '').replace(re, '').split('.');
  81. len = Math.min(a.length, b.length);
  82. for( i = 0; i < len; i++ ) {
  83. cmp = parseInt(a[i], 10) - parseInt(b[i], 10);
  84. if( cmp !== 0 ) {
  85. return cmp;
  86. }
  87. }
  88. return a.length - b.length;
  89. }
  90.  
  91. function close_popup() {
  92. GM_setValue(OLD_VERSION_KEY, version)
  93. $('.BarefootMonkey-popup-bg, .BarefootMonkey-popup').fadeOut(500, function() { $(this).remove() })
  94. }
  95. function open_popup() {
  96. var bg = $('<div class="BarefootMonkey-popup-bg">')
  97. .hide()
  98. .appendTo(document.body)
  99. .click(close_popup)
  100.  
  101. var popup = $('<aside class="BarefootMonkey-popup">')
  102. .hide()
  103. .html('<h1>improved game filter <i>steamgifts.com userscript, version '+version+'</i></h1>'
  104. +"<p>This script replaces steamgifts.com's default \"Hide Game\" interface with one that's a bit more practical.</p>"
  105. +"<p>Click on the <i class=\"giveaway__icon giveaway__hide fa fa-eye-slash\"></i> icon next to the name of a game you want to hide and the game will be hidden without asking you for confirmation.</p>"
  106. +"<p>Skipping the confirmation makes hiding games quicker, but increases the chance that you accidentally hide the wrong game. To solve that problem, <b>steamgifts.com improved game filter</b> doesn't actually remove games from the page as soon as you hide them - it merely adds a visual indication that they're hidden. That way you can see what games you've just hidden and undo any accidents by clicking on the <i class=\"giveaway__icon giveaway__hide fa fa-eye-slash\"></i> icons again to undo hiding them.</p>"
  107. +"<p>If you have any comments or questions about the <b>improved game filter</b> userscript then feel free to <a href=\"http://www.steamgifts.com/discussion/c1xhr/userscript-improved-game-filter\">join this discussion</a>.</p>"
  108. +"<h1>recent changes</h1>"
  109. +"<ul>"
  110. +"<li>Compatible with 2016-04-13 site update: <a href=\"https://www.steamgifts.com/discussion/ppoZ7/https\">HTTPS</a></li>"
  111. +"<li>Compatible with 2016-04-11 site update: <a href=\"http://www.steamgifts.com/discussion/QQvgS/hide-games-faster\">Hide Games Faster</a></li>"
  112. +"</ul>"
  113. )
  114. .appendTo(document.body)
  115.  
  116. $('.BarefootMonkey-popup-bg, .BarefootMonkey-popup').fadeIn(500)
  117. }
  118.  
  119. var old_version = GM_getValue(OLD_VERSION_KEY)
  120. if (old_version == null || cmpVersion(old_version, version) < 0) {
  121. open_popup()
  122. }
  123.  
  124. console.debug("start")
  125. $('<style>')
  126. .text(
  127. '@keyframes rotate-right-60px {'
  128. +' from {'
  129. +' background-position: 0px 0;'
  130. +' }'
  131. +' to {'
  132. +' background-position: 300px 0;'
  133. +' }'
  134. +'}'
  135.  
  136. +'@keyframes rotate-left-60px {'
  137. +' from {'
  138. +' background-position: 0px 0;'
  139. +' }'
  140. +' to {'
  141. +' background-position: -300px 0;'
  142. +' }'
  143. +'}'
  144.  
  145. +'.giveaway__row-outer-wrap {'
  146. +' transition: opacity 1s linear;'
  147. +'}'
  148.  
  149. +'.BarefootMonkey-hidden {'
  150. +' opacity: 0.6;'
  151. +' background: repeating-linear-gradient(45deg, transparent 0px, transparent 10px, #ccc, 10px, #ccc 12px, transparent 12px), repeating-linear-gradient(-45deg, transparent 0px, transparent 10px, #ccc, 10px, #ccc 12px, transparent 12px);'
  152. +'}'
  153.  
  154. +'.BarefootMonkey-hiding, .BarefootMonkey-unhiding {'
  155. +' opacity: 0.6;'
  156. +' animation-duration: 12s;'
  157. +' animation-iteration-count: infinite;'
  158. +' animation-timing-function: linear;'
  159. +'}'
  160.  
  161. +'.BarefootMonkey-hiding {'
  162. +' animation-name: rotate-right-60px;'
  163. +' background: repeating-linear-gradient(45deg, transparent 0px, transparent 10px, #ccc, 10px, #ccc 12px);'
  164. +'}'
  165.  
  166. +'.BarefootMonkey-unhiding {'
  167. +' animation-name: rotate-left-60px;'
  168. +' background: repeating-linear-gradient(-45deg, transparent 0px, transparent 10px, #ccc, 10px, #ccc 12px);'
  169. +'}'
  170. )
  171. .appendTo(document.head)
  172.  
  173. function update_filter(id, token, action, url, prior_class, progress_class, complete_class) {
  174.  
  175. console.debug('update_filter', id, token, action, url, prior_class, progress_class, complete_class)
  176.  
  177. $('.giveaway__row-outer-wrap[data-game-id='+id+']')
  178. .addClass(progress_class)
  179. .removeClass('BarefootMonkey-error')
  180. .removeClass(prior_class)
  181.  
  182. $.ajax({
  183. url: url,
  184. method: 'POST',
  185. context: {id: id, progress_class: progress_class, complete_class: complete_class},
  186. data: {
  187. 'xsrf_token':token,
  188. 'game_id':id,
  189. 'do': action
  190. },
  191. 'error': function() {
  192. console.debug("error")
  193. $('.giveaway__icon.giveaway__hide[data-popup="popup--hide-games"][data-game-id='+this.id+']')
  194. .closest('.giveaway__row-outer-wrap')
  195. .removeClass(this.progress_class)
  196. .addClass('BarefootMonkey-error')
  197. },
  198. 'success': function() {
  199. $('.giveaway__row-outer-wrap[data-game-id='+this.id+']')
  200. .removeClass(this.progress_class)
  201. .addClass(this.complete_class)
  202. }
  203. })
  204. }
  205.  
  206. function callback(event) {
  207.  
  208. var closest = $(this).closest('.giveaway__row-outer-wrap[data-game-id]')
  209. if (closest.length > 0) {
  210.  
  211. // get id and token
  212. var id = closest.data('game-id')
  213. var token = $('.popup--hide-games form input[name="xsrf_token"]').val()
  214.  
  215. // hide or unhide the game
  216. if (id && token) {
  217. console.debug(id, token, closest)
  218. if (closest.hasClass('BarefootMonkey-hidden')) {
  219. console.debug("hidden, unhiding")
  220. update_filter(id, token, 'remove_filter', '/ajax.php', 'BarefootMonkey-hidden', 'BarefootMonkey-unhiding', null)
  221. } else if (!closest.hasClass('BarefootMonkey-unhiding') && !closest.hasClass('BarefootMonkey-hiding')) {
  222. console.debug("unhidden, hiding")
  223. update_filter(id, token, 'hide_giveaways_by_game_id', '/ajax.php', null, 'BarefootMonkey-hiding', 'BarefootMonkey-hidden')
  224. }
  225. }
  226. }
  227.  
  228. event.stopPropagation()
  229. }
  230.  
  231. setTimeout(function() {
  232.  
  233. // remove existing hide button click handler
  234. unsafeWindow.$('.giveaway__icon.giveaway__hide').off('click')
  235.  
  236. // handle clicking on hide button
  237. $('.giveaway__icon.giveaway__hide')
  238. .click(callback)
  239.  
  240. // observe DOM tree mutations to improve compatibility with endless scrolling userscripts
  241. var container = document.querySelector('.page__outer-wrap>.page__inner-wrap>.widget-container>.sidebar+div')
  242. var observer = new MutationObserver(function(mutations) {
  243. mutations.forEach(function(mutation) {
  244. var added = mutation.addedNodes
  245. for (var i = 0; i < mutation.addedNodes.length; i += 1) {
  246. var node = $(mutation.addedNodes[i])
  247.  
  248. node.find('.giveaway__icon.giveaway__hide')
  249. .off()
  250. .click(callback)
  251. }
  252. });
  253. });
  254. observer.observe(container, {childList:true});
  255.  
  256. }, 10)
  257. //*/
  258. console.debug("end")