Guest User

content.js

a guest
Dec 28th, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.98 KB | None | 0 0
  1. console.log('SJW script LOADED');
  2. var api_base = 'https://sjwbot.herokuapp.com',
  3. sjw_text = 'https://pbs.twimg.com/media/Clh5FKpVYAAGStA.jpg',
  4. sjw_logo = 'https://pbs.twimg.com/media/Clh6oCQUsAA8wIf.png',
  5. checked_for_sjws = false;
  6.  
  7. function getTweets() {
  8. return $('li.js-stream-item.stream-item.stream-item');
  9. }
  10.  
  11. function checkForSJW(tweets) {
  12. tweets.each(function() {
  13. var tweet = $(this),
  14. handle = tweet.find('div.tweet')
  15. .data('screen-name');
  16.  
  17. $.ajax({
  18. url: api_base + '/warriors/check/' + handle,
  19. dataType: 'json',
  20. success: function(data) {
  21. if (data['sjw'] == 'true') {
  22. tweet.find('div.content')
  23. .find('img.avatar')
  24. .attr('src', sjw_text);
  25. tweet.find('.tweet-timestamp')
  26. .after('<img class="sjw-logo" style="width: 20px; height: 20px; margin-left:5px; float:right;" src=' + sjw_logo + '>')
  27. }
  28. }
  29. })
  30. })
  31. }
  32.  
  33. // only run checks if user viewing generic twitter feed (or notifications tab, TBD)
  34. setInterval(function() {
  35. if ($('div.ProfileCanopy')
  36. .length == 0 && checked_for_sjws == false) {
  37. var tweets = getTweets();
  38. checkForSJW(tweets)
  39. checked_for_sjws = true;
  40. }
  41. }, 500)
  42.  
  43. // check for sjws again, if user auto loads more tweetes in feed
  44. $(window)
  45. .scroll(function() {
  46. if ($(window)
  47. .scrollTop() + $(window)
  48. .height() == $(document)
  49. .height()) {
  50. var tweets = getTweets();
  51. console.log('got new tweets');
  52. checkForSJW(tweets);
  53. }
  54. });
  55.  
  56. if ($('div.ProfileCanopy')
  57. .length == 1) {
  58. var nominee = $('div.ProfileHeaderCard')
  59. .find('span.u-linkComplex-target')
  60. .text(),
  61. nominator = $('li.current-user')
  62. .find('a')
  63. .attr('href')
  64. .split('/')[1];
  65. avatar = $('div.ProfileAvatar')
  66. .find('img');
  67.  
  68. var refreshInterval = setInterval(function() {
  69. if ($('div#nominate-sjw')
  70. .length == 0) {
  71. checkForNomination();
  72. }
  73. }, 500)
  74.  
  75. function checkForNomination() {
  76. $.ajax({
  77. url: api_base + '/nominees/check/' + nominee + '/' + nominator,
  78. dataType: 'json',
  79. success: function(data) {
  80. if (data['nominated'] == 'true') {
  81. showThanks();
  82. clearInterval(refreshInterval);
  83. }
  84.  
  85. if (data['warrior'] == 'true') {
  86. avatar.attr('src', 'https://pbs.twimg.com/media/Clh6oCQUsAA8wIf.png')
  87. showThanks();
  88. clearInterval(refreshInterval);
  89. }
  90.  
  91. if ($('button.UserActions-editButton.edit-button')
  92. .find('.button-text')
  93. .text() == 'Edit profile') {
  94. showPromo();
  95. clearInterval(refreshInterval);
  96. } else {
  97. showNomination();
  98. clearInterval(refreshInterval);
  99. }
  100. }
  101. })
  102. }
  103.  
  104. function showPromo() {
  105. $('div.ProfileMessagingActions-actionsContainer')
  106. .append('<div id="nominate-sjw"><button class="u-sizeFull js-tooltip btn primary-btn tweet-action tweet-btn" type="button" style="background-color:#19cf86"><span class="NewTweetButton-content button-text tweeting-text u-textTruncate"><span class="NewTweetButton-text" id="sjw-cta" aria-hidden="true">Thanks for using SJW Bot!</span></span></button></div>');
  107. $("#nominate-sjw")
  108. .attr("disabled", "disabled");
  109. $("#nominate-sjw")
  110. .addClass("promo");
  111. }
  112.  
  113. function showThanks() {
  114. $('div.ProfileMessagingActions-actionsContainer')
  115. .append('<div id="nominate-sjw"><button class="u-sizeFull js-tooltip btn primary-btn tweet-action tweet-btn" type="button" style="background-color:#fab81e"><span class="NewTweetButton-content button-text tweeting-text u-textTruncate"><span class="NewTweetButton-text" id="sjw-cta" aria-hidden="true">Thanks for the nomination.</span></span></button></div>');
  116. $("#nominate-sjw")
  117. .attr("disabled", "disabled");
  118. }
  119.  
  120. function showNomination() {
  121. $('div.ProfileMessagingActions-actionsContainer')
  122. .append('<div id="nominate-sjw"><button class="u-sizeFull js-tooltip btn primary-btn tweet-action tweet-btn" type="button" style="background-color:#ff1d8e"><span class="NewTweetButton-content button-text tweeting-text u-textTruncate"><span class="NewTweetButton-text" id="sjw-cta" aria-hidden="true">Nominate to SJW</span></span></button></div>');
  123. };
  124.  
  125. $('body')
  126. .on('click', '#nominate-sjw', function(e) {
  127. e.preventDefault();
  128. if ($(this)
  129. .hasClass('promo')) {
  130. window.open('http://www.sjwbot.com', '_blank');
  131. return false;
  132. } // handle user on own profile page
  133. if ($(this)
  134. .attr('disabled') == 'disabled') {
  135. return false;
  136. } // handle already nominated
  137.  
  138. if ($('div#nominate-sjw')
  139. .length == 1) {
  140. clearInterval(refreshInterval);
  141. }
  142.  
  143. $.ajax({
  144. url: api_base + '/nominate',
  145. method: 'post',
  146. data: {
  147. nominator_handle: nominator,
  148. nominee_handle: nominee
  149. },
  150. success: function(data) {
  151. alert('@' + nominee + 'was nominated successfully!');
  152. $('span#sjw-cta')
  153. .text('Thanks!');
  154. $('div#nominate-sjw')
  155. .fadeOut(1250);
  156. }
  157. })
  158.  
  159. })
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment