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.

Fra 11.07.2016. Se den seneste versjonen.

  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.053
  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.  
  18. var bookMarkButtons = _$("body a[href^='https://gathering.tweakers.net/forum/insert_bookmark/']");
  19.  
  20. function getCurrentStatus() {
  21. var myReactBookmarks = jQuery('#mybookmarks .topic a:first-child[title]'),
  22. messageIds = [],
  23. messageId = '';
  24.  
  25. myReactBookmarks.each(function(i, el) {
  26. messageId = el.href.split('#');
  27. messageIds.push(messageId[1]);
  28. });
  29.  
  30. localStorage.setItem('sndr_bookmarks', messageIds.join(';'));
  31.  
  32. }
  33.  
  34. function bindBookmarkButtons() {
  35.  
  36. var $button;
  37. var messageId;
  38. var bookmarked = localStorage.getItem('sndr_bookmarks');
  39.  
  40. bookMarkButtons.each(function(i, object) {
  41.  
  42. $button = _$(object);
  43. messageId = $button.parents('.message').prev('a').attr('name');
  44. // Tag buttons
  45. $button.attr('data-message-id', messageId);
  46.  
  47. // Add event handler
  48. $button.on('click', function(ev) {
  49. ev.preventDefault();
  50.  
  51. var target = _$(ev.currentTarget);
  52.  
  53. // Get required values
  54. var author = target.parents('.message').find('.username a')[0].innerText,
  55. topicName = _$('h1')[0].innerText,
  56. name = author + ' in ' + '"' + topicName + '"',
  57. folder = '11',
  58. reactId = _$('[name="data[reactid]"]').val(),
  59. action = 'insert_bookmark',
  60. messageId = target.attr('data-message-id'),
  61. topicId = _$('[data-topicid]').attr('data-topicid'),
  62. httpReferrer = window.location.href,
  63. button = target;
  64.  
  65. // Make call
  66. bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer);
  67.  
  68. });
  69.  
  70. // Check if any of the bookmark buttons match the values in local storage
  71. if(bookmarked.indexOf(messageId) !== -1) {
  72. markButton($button);
  73.  
  74. }
  75.  
  76. });
  77.  
  78. }
  79.  
  80. function markButton(button) {
  81.  
  82. // Change color on button
  83. button.prepend('<img src="http://tweakimg.net/g/icons/checkmark-green.png" class="js_tm_bookmarked_state" /> ');
  84.  
  85. if(!button.hasClass('js_tm_bookmark_button')) {
  86. button.addClass('js_tm_bookmark_button');
  87. }
  88.  
  89. button.css({
  90. 'color': 'rgb(119, 164, 0)',
  91. 'font-weight': 'bold'
  92. });
  93.  
  94. }
  95.  
  96. function unmarkButton() {
  97.  
  98. _$('.js_tm_bookmarked_state').remove();
  99. _$('.js_tm_bookmark_button').removeAttr('style').empty().text('Bookmark');
  100.  
  101. }
  102.  
  103. function bookMarkAsync(button, name, folder, reactId, action, messageId, topicId, httpReferrer) {
  104.  
  105. _$.ajax({
  106. url: 'http://gathering.tweakers.net/forum',
  107. type: 'POST',
  108. data: {
  109. 'data[name]' : name,
  110. 'data[folder]' : folder,
  111. 'data[reactid]' : reactId,
  112. 'action' : action,
  113. 'data[messageid]' : messageId,
  114. 'data[topicid]' : topicId,
  115. 'data[http_referrer]' : httpReferrer
  116. }
  117. })
  118. .success(function() {
  119. unmarkButton();
  120. markButton(button);
  121. });
  122.  
  123. }
  124.  
  125. return {
  126. bindBookmarkButtons: bindBookmarkButtons,
  127. getCurrentStatus: getCurrentStatus
  128. };
  129.  
  130. })(jQuery);
  131.  
  132. if(window.location.pathname === '/forum/myreact') {
  133. bookmarkManager.getCurrentStatus();
  134. } else {
  135. bookmarkManager.bindBookmarkButtons();
  136. }
  137.  
  138.  
  139. });