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.

  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.621
  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.  
  15. class BookmarkManager {
  16.  
  17. constructor() {
  18. this.bindButtons();
  19. }
  20.  
  21. bindButtons() {
  22. jQuery('body').on('click', 'a[href*="insert_bookmark"]', (ev) => this.handleButtonClick(ev));
  23. }
  24.  
  25. handleButtonClick(ev) {
  26. const $trigger = jQuery(ev.currentTarget);
  27. ev.preventDefault();
  28. this.execBookmark($trigger);
  29. }
  30.  
  31. execBookmark($trigger) {
  32. const author = $trigger.parents('.message').find('.username a')[0].innerText;
  33. const topicName = jQuery('h1')[0].innerText;
  34. const folder = '11';
  35. const reactId = jQuery('[name="data[reactid]"]').val();
  36. const action = 'insert_bookmark';
  37. const messageId = $trigger.parents('div[data-message-id]').attr('data-message-id');
  38. const topicId = jQuery('[data-topicid]').attr('data-topicid');
  39. const httpReferrer = window.location.href;
  40.  
  41. const bookmarkName = this.getBookmarkName(author, topicName);
  42.  
  43. // Make call
  44. this.bookMarkAsync($trigger, bookmarkName, folder, reactId, action, messageId, topicId, httpReferrer);
  45. }
  46.  
  47. bookMarkAsync($button, bookmarkName, folder, reactId, action, messageId, topicId, httpReferrer) {
  48. jQuery.ajax({
  49. url: 'https://gathering.tweakers.net/forum',
  50. type: 'POST',
  51. data: {
  52. 'data[name]': bookmarkName,
  53. 'data[folder]': folder,
  54. 'data[reactid]': reactId,
  55. action,
  56. 'data[messageid]': messageId,
  57. 'data[topicid]': topicId,
  58. 'data[http_referrer]': httpReferrer,
  59. },
  60. })
  61. .success(() => {
  62. this.unmarkButtons();
  63. this.markButton($button);
  64. });
  65. }
  66.  
  67. getBookmarkName(author, topicName) {
  68. const fullBookmarkName = `${author} in ${topicName}`;
  69. const maxLength = 60;
  70. let bookmarkName = fullBookmarkName;
  71. let maxTopicLength;
  72.  
  73. if (fullBookmarkName.length > maxLength) {
  74. maxTopicLength = maxLength - `${author} in `.length;
  75. bookmarkName = `${author} in ${topicName.substr(0, maxTopicLength - 4).trim()}...`;
  76. }
  77. return bookmarkName;
  78. }
  79.  
  80. markButton($button) {
  81. // Change color on $button
  82. $button.prepend('<img src="https://tweakimg.net/g/icons/checkmark-green.png" class="js_tm_bookmarked_state" /> ');
  83.  
  84. if (!$button.hasClass('js_tm_bookmark_button')) {
  85. $button.addClass('js_tm_bookmark_button');
  86. }
  87.  
  88. $button.css({
  89. color: 'rgb(119, 164, 0)',
  90. 'font-weight': 'bold',
  91. });
  92. }
  93.  
  94. unmarkButtons() {
  95. jQuery('.js_tm_bookmarked_state').remove();
  96. jQuery('.js_tm_bookmark_button').removeAttr('style').empty().text('Bookmark');
  97. }
  98.  
  99. }
  100.  
  101. class GatheringOfTweakersExtended {
  102.  
  103. constructor() {
  104. new BookmarkManager();
  105. this.updateForumHref();
  106. }
  107.  
  108. updateForumHref() {
  109. jQuery('#navMenu .active a').attr('href', '//gathering.tweakers.net/forum/myreact').text('MyReact');
  110. }
  111.  
  112. }
  113.  
  114.  
  115. new GatheringOfTweakersExtended();