Guest User

Untitled

a guest
Nov 2nd, 2016
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.13 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Notatkowator 2000
  3. // @namespace http://www.wykop.pl/ludzie/piokom123/
  4. // @description Szybki podgląd notek
  5. // @author piokom123
  6. // @version 1.2
  7. // @grant none
  8. // @include http://www.wykop.pl/*
  9. // @run-at document-end
  10. // ==/UserScript==
  11. // Ukłony dla @parasolki za nadanie dodatkowi tej boskiej nazwy ;)
  12.  
  13. function main() {
  14. var loggedInNick = $('.logged-user .ellipsis span b').html();
  15. var cacheCookieName = 'userNotesCache';
  16. var cacheCookieExpirationName = 'userNotesCacheExpiration';
  17. var cacheCookieMinutes = 15;
  18. var notes = {};
  19.  
  20. /**
  21. * Loads notes from cache or fetches them from website if cache doesn't exist or is invalid
  22. */
  23. function loadNotes() {
  24. var cachedNotes = getData(cacheCookieName);
  25. if (typeof cachedNotes === 'undefined') {
  26. fetchNotes();
  27. } else {
  28. notes = JSON.parse(cachedNotes);
  29. showNotes(notes);
  30. }
  31. }
  32.  
  33. /**
  34. * Asynchronously fetches notes from website, saves it and shows
  35. */
  36. function fetchNotes() {
  37. setTimeout(function() {
  38. $.ajax('http://www.wykop.pl/moj/notatki-o-uzytkownikach/')
  39. .done(function(data) {
  40. parseNotes(data);
  41.  
  42. saveNotes();
  43.  
  44. showNotes();
  45. });
  46. }, 100);
  47. }
  48.  
  49. /**
  50. * Parses user notes list HTML and returns as object
  51. */
  52. function parseNotes(data) {
  53. notes = {};
  54.  
  55. $('#notesList li p', data).each(function(index, item) {
  56. notes[$('a b', item).html()] = parseNote(item.innerHTML);
  57. });
  58. }
  59.  
  60. /**
  61. * Parses HTML of one note
  62. */
  63. function parseNote(content) {
  64. var parsedNote = '';
  65. var match = content.split('</b></a>');
  66. match = match[1].trim();
  67.  
  68. if (match.indexOf('|') === -1) {
  69. parsedNote = match;
  70. } else {
  71. match = match.split('|');
  72. parsedNote = match[0];
  73. }
  74.  
  75. parsedNote = activateLinks(parsedNote);
  76.  
  77. return parsedNote;
  78. }
  79.  
  80. function activateLinks(content) {
  81. if (content.indexOf('http://') !== -1 || content.indexOf('https://') !== -1) {
  82. content = content.replace(/(https?:\/\/([^\s]+))/g, '<a href="$1" target="_blank">$1</a>');
  83. }
  84.  
  85. return content;
  86. }
  87.  
  88. function saveNotes() {
  89. notesString = JSON.stringify(notes);
  90.  
  91. saveData(cacheCookieName, notesString);
  92. }
  93.  
  94. /**
  95. * Shows notes on website
  96. */
  97. function showNotes() {
  98. $('.author .showProfileSummary b').each(function(index, item) {
  99. var nick = item.innerHTML.trim();
  100.  
  101. if (typeof notes[nick] !== 'undefined') {
  102. $(item.parentNode.parentNode).append('<span class="note2000 notesFor' + nick + '" style="padding-right: 55px; white-space: normal !important;">| ' + notes[nick] + '</span>');
  103. }
  104. });
  105. }
  106.  
  107. /**
  108. * Gets data from local storage. Can return 'undefined' if data doesn't exist or is invalid.
  109. */
  110. function getData(name) {
  111. if (typeof localStorage !== 'undefined') {
  112. var expirationCookie = getCookie(cacheCookieExpirationName);
  113. if (typeof expirationCookie === 'undefined') {
  114. return;
  115. }
  116.  
  117. var data = localStorage.getItem(name);
  118. if (typeof data !== 'undefined' && data !== null && data !== '') {
  119. return data;
  120. }
  121. }
  122. }
  123.  
  124. /**
  125. * Saves data to local storage and sets expiration cookie
  126. */
  127. function saveData(name, value) {
  128. if (typeof localStorage !== 'undefined') {
  129. saveCookie(cacheCookieExpirationName, 'valid', cacheCookieMinutes);
  130.  
  131. var data = localStorage.setItem(name, value);
  132. }
  133. }
  134.  
  135. function getCookie(name) {
  136. var i, x, y, ARRcookies = document.cookie.split(";");
  137. for (i = 0; i < ARRcookies.length; i++) {
  138. x = ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  139. y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
  140. x = x.replace(/^\s+|\s+$/g, "");
  141. if (x === name) {
  142. return unescape(y);
  143. }
  144. }
  145. }
  146.  
  147. function saveCookie(name, value, minutes) {
  148. var exdate = new Date();
  149. exdate.setMinutes(exdate.getMinutes() + minutes);
  150.  
  151. value = escape(value) + "; domain=.wykop.pl; path=/" + ((minutes===null) ? "" : "; expires=" + exdate.toUTCString());
  152. document.cookie = name + "=" + value;
  153. }
  154.  
  155. function addBlackListsStats() {
  156. jQuery(jQuery('.blackListForm')[0]).before(
  157. '<div style="width: 100%; padding: 25px; text-align: center">'
  158. + 'Osoby na #czarnolisto: <b>' + jQuery('div[data-type="users"] .usercard').length + '</b>'
  159. + ', w tym: <span class="color-2">bordo (<b>' + jQuery('div[data-type="users"] .color-2').length + '</b>)</span>'
  160. + ', <span class="color-1">pomarańczki (<b>' + jQuery('div[data-type="users"] .color-1').length + '</b>)</span>'
  161. + ', <span class="color-0">zielonki (<b>' + jQuery('div[data-type="users"] .color-0').length + '</b>)</span>'
  162. + ', <span class="color-1001">zbanowani (<b>' + jQuery('div[data-type="users"] .color-1001').length + '</b>)</span>'
  163. + ', <span class="color-1002">ragequity (<b>' + jQuery('div[data-type="users"] .color-1002').length + '</b>)</span>'
  164. + '<br />'
  165. + 'Tagi na #czarnolisto: <b>' + jQuery('div[data-type="hashtags"] .tagcard').length + '</b>'
  166. + '<br />'
  167. + 'Domeny na #czarnolisto: <b>' + jQuery('div[data-type="domains"] .tag').length + '</b>'
  168. + '</div>');
  169. }
  170.  
  171. function addNotesStats() {
  172. jQuery('#notesList').before(
  173. '<div style="width: 100%; padding: 25px; text-align: center">'
  174. + 'Wszystkie notatki: <b>' + jQuery('#notesList li').length + '</b>'
  175. + ', w tym o: <span class="color-2">bordo (<b>' + jQuery('#notesList .color-2').length + '</b>)</span>'
  176. + ', <span class="color-1">pomarańczkach (<b>' + jQuery('#notesList .color-1').length + '</b>)</span>'
  177. + ', <span class="color-0">zielonkach (<b>' + jQuery('#notesList .color-0').length + '</b>)</span>'
  178. + ', <span class="color-1001">zbanowanych (<b>' + jQuery('#notesList .color-1001').length + '</b>)</span>'
  179. + ', <span class="color-1002">ragequitach (<b>' + jQuery('#notesList .color-1002').length + '</b>)</span>'
  180. + '</div>');
  181. }
  182.  
  183. $(document).ready(function() {
  184. if (typeof loggedInNick === 'undefined') {
  185. return;
  186. }
  187.  
  188. loadNotes();
  189.  
  190. $(document).on('DOMNodeInserted', function(e) {
  191. if ($('.author .showProfileSummary b', $(e.target)).length > 0) {
  192. var nodes = $('.author .showProfileSummary b', $(e.target));
  193.  
  194. for (var i = 0; i < nodes.length; i++) {
  195. node = $(nodes[i]);
  196. var nick = node.html().trim();
  197.  
  198. if (typeof notes[nick] !== 'undefined') {
  199. if ($('.note2000', node.parent().parent()).length != 0) {
  200. continue;
  201. }
  202.  
  203. $(node.parent().parent()).append('<span class="note2000 notesFor' + nick + '" style="padding-right: 55px; white-space: normal !important;">| ' + notes[nick] + '</span>');
  204. }
  205. }
  206. }
  207. });
  208.  
  209. if (document.location.href.indexOf('ustawienia/czarne-listy') !== -1) {
  210. addBlackListsStats();
  211. }
  212.  
  213. if (document.location.href.indexOf('moj/notatki-o-uzytkownikach') !== -1) {
  214. addNotesStats();
  215. }
  216. });
  217. }
  218.  
  219. if (typeof $ == 'undefined') {
  220. if (typeof unsafeWindow !== 'undefined' && unsafeWindow.jQuery) {
  221. // Firefox
  222. var $ = unsafeWindow.jQuery;
  223. main();
  224. } else {
  225. // Chrome
  226. addJQuery(main);
  227. }
  228. } else {
  229. // Opera >.>
  230. main();
  231. }
  232.  
  233. function addJQuery(callback) {
  234. var script = document.createElement("script");
  235. script.textContent = "(" + callback.toString() + ")();";
  236. document.body.appendChild(script);
  237. }
Add Comment
Please, Sign In to add comment