gathering.tweakers.net - Add Topic Bookmark

Makes the bookmark button for a post on the GoT Forum (gathering.tweakers.net) work asynchronously. Click Bookmark and wait for it to turn Green.

Versione datata 31/08/2015. Vedi la nuova versione l'ultima versione.

  1. // ==UserScript==
  2. // @name gathering.tweakers.net - Add Topic Bookmark
  3. // @homepageURL https://github.com/sand3r/tampermonkey-scripts/tree/master/gathering_of_tweakers
  4. // @namespace gathering.tweakers.net
  5. // @version 0.052
  6. // @description Makes the bookmark button for a post on the GoT Forum (gathering.tweakers.net) work asynchronously. Click Bookmark and wait for it to turn Green.
  7. // @match *://gathering.tweakers.net/forum/myreact
  8. // @match *://gathering.tweakers.net/forum/list_message*
  9. // @include http://gathering.tweakers.net
  10. // @require http://code.jquery.com/jquery-latest.js
  11. // @author Sander Thalen
  12. // ==/UserScript==
  13.  
  14. jQuery(document).ready(function() {
  15.  
  16. var bookmarkManager = (function(_$) {
  17. var bookMarkButtons = _$("a[href^='http://gathering.tweakers.net/forum/insert_bookmark/']");
  18.  
  19. function getCurrentStatus() {
  20. var myReactBookmarks = jQuery('#mybookmarks .topic a:first-child[title]'),
  21. messageIds = [],
  22. messageId = '';
  23.  
  24. myReactBookmarks.each(function(i, el) {
  25. messageId = el.href.split('#');
  26. messageIds.push(messageId[1]);
  27. });
  28.  
  29. localStorage.setItem('sndr_bookmarks', messageIds.join(';'));
  30.  
  31. }
  32.  
  33. function bindBookmarkButtons() {
  34.  
  35. var $button;
  36. var messageId;
  37. var bookmarked = localStorage.getItem('sndr_bookmarks');
  38.  
  39. bookMarkButtons.each(function(i, object) {
  40. $button = _$(object);
  41. messageId = $button.parents('.message').prev('a').attr('name');
  42. // Tag buttons
  43. $button.attr('data-message-id', messageId);
  44.  
  45. // Add event handler
  46. $button.on('click', function(ev) {
  47. ev.preventDefault();
  48. var target = _$(ev.currentTarget);
  49.  
  50. // Get required values
  51. var author = target.parents('.message').find('.username a')[0].innerText,
  52. topicName = _$('h1')[0].innerText,
  53. name = author + ' in ' + '"' + topicName + '"',
  54. folder = '11',
  55. reactId = _$('[name="data[reactid]"]').val(),
  56. action = 'insert_bookmark',
  57. messageId = target.attr('data-message-id'),
  58. topicId = _$('[data-topicid]').attr('data-topicid'),
  59. httpReferrer = window.location.href,
  60. button = target;
  61.  
  62. // Make call
  63. bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer);
  64. });
  65.  
  66. // Check if any of the bookmark buttons match the values in local storage
  67. if(bookmarked.indexOf(messageId) !== -1) {
  68. markButton($button);
  69. }
  70.  
  71. });
  72.  
  73. }
  74.  
  75. function markButton(button) {
  76.  
  77. // Change color on button
  78. button.prepend('<img src="http://tweakimg.net/g/icons/checkmark-green.png" class="js_tm_bookmarked_state" /> ');
  79.  
  80. if(!button.hasClass('js_tm_bookmark_button')) {
  81. button.addClass('js_tm_bookmark_button');
  82. }
  83.  
  84. button.css({
  85. 'color': 'rgb(119, 164, 0)',
  86. 'font-weight': 'bold'
  87. });
  88.  
  89. }
  90.  
  91. function unmarkButton() {
  92.  
  93. _$('.js_tm_bookmarked_state').remove();
  94. _$('.js_tm_bookmark_button').removeAttr('style').empty().text('Bookmark');
  95.  
  96. }
  97.  
  98. function bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer) {
  99. _$.ajax({
  100. url: 'http://gathering.tweakers.net/forum',
  101. type: 'POST',
  102. data: {
  103. 'data[name]' : name,
  104. 'data[folder]' : folder,
  105. 'data[reactid]' : reactId,
  106. 'action' : action,
  107. 'data[messageid]' : messageId,
  108. 'data[topicid]' : topicId,
  109. 'data[http_referrer]' : httpReferrer
  110. }
  111. })
  112. .success(function() {
  113. unmarkButton();
  114. markButton(button);
  115. });
  116. }
  117.  
  118. return {
  119. bindBookmarkButtons: bindBookmarkButtons,
  120. getCurrentStatus: getCurrentStatus
  121. };
  122.  
  123. })(jQuery);
  124.  
  125. if(window.location.pathname === '/forum/myreact') {
  126. bookmarkManager.getCurrentStatus();
  127. } else {
  128. bookmarkManager.bindBookmarkButtons();
  129. }
  130.  
  131.  
  132. });