Imagenorator

turns images into links in specified people's posts

  1. // ==UserScript==
  2. // @name Imagenorator
  3. // @namespace pendevin
  4. // @description turns images into links in specified people's posts
  5. // @include http://boards.endoftheinter.net/showmessages.php?*
  6. // @include http://archives.endoftheinter.net/showmessages.php?*
  7. // @include https://boards.endoftheinter.net/showmessages.php?*
  8. // @include https://archives.endoftheinter.net/showmessages.php?*
  9. // @require http://code.jquery.com/jquery-2.1.4.min.js
  10. // @version 3.1
  11. // ==/UserScript==
  12.  
  13.  
  14. //add userIDs of people whose image settings you want changed
  15. //e.g. LlamaGuy :) and Sabretooth would be const IMAGENORATED=$([1,11256]);
  16. //users whose images should become links
  17. const IMAGENORATED_LINKS = $([]);
  18. //users whose images should become thumbnails
  19. const IMAGENORATED_THUMBS = $([25770, 13466]);
  20. //users whose images should become inline fullsized (quoted images not included)
  21. const IMAGENORATED_FULL = $([]);
  22.  
  23. //priority is fullsize > thumbs > links if you happen to have a dude on multiple lists
  24.  
  25.  
  26. //ll breaks without noconflict jquery
  27. this.$ = this.jQuery = jQuery.noConflict(true);
  28.  
  29. //livelinks compatiblity *JQUERY
  30. //calls the function on each message-container in a document, including ones added by livelinks
  31. //place is an optional specialized location
  32. function livelinks(func, extraParams, place) {
  33. if (extraParams == undefined) {
  34. extraParams = null;
  35. }
  36. if (place == undefined) {
  37. place = '.message-container';
  38. }
  39. //run the function on the message-containers currently on the page
  40. $('#u0_1 ' + place).each(function(i, container) {
  41. func(container, extraParams);
  42. });
  43. //make mutationobserver to run junk on matches
  44. var observer = new MutationObserver(function(mutations) {
  45. mutations.forEach(function(mutation) {
  46. $(mutation.addedNodes).find(place).each(function(i, container) {
  47. func(container, extraParams);
  48. });
  49. });
  50. });
  51. //run mutationobserver
  52. observer.observe(document.querySelector('#u0_1'), {
  53. childList: true
  54. });
  55. }
  56.  
  57. //adds a style to a document and returns the style object *JQUERY
  58. //css is a string, id is an optional string that determines the object's id
  59. function addStyle(css, id) {
  60. //create a style
  61. var style = $('<style type="text/css">');
  62. //add the css data to it
  63. style.html(css);
  64. if (id) {
  65. //remove any style that has our id
  66. $('#' + id).remove();
  67. //give our style the id after removing the other stuff. idk if it matters, but i'm too lazy to find out
  68. style.attr('id', id);
  69. }
  70. //add the style into the head
  71. $('head').append(style);
  72. //we're outta here
  73. return style;
  74. }
  75.  
  76. //tags message-tops with userids
  77. //should call it with livelinks imo *JQUERY
  78. function userids(container) {
  79. $(container).find('.message-top').each(function(i, top) {
  80. top = $(top);
  81. //get userid attribute from the profile link
  82. top.attr('userID', top.children('a[href*="user="]').attr('href').split('user=')[1]);
  83. });
  84. }
  85.  
  86. //puts the message body of any quoted-message in its own message div like normal messages for easier styling(hiding)
  87. //livelinks ready *JQUERY
  88. function rearrangeQuotes(container) {
  89. //this is a for loop or something
  90. $(container).find('.quoted-message').each(function(i, quote) {
  91. quote = $(quote);
  92. //create message div for quote
  93. var quoteBody = $('<div class="message">');
  94. //add everything but the message-top to the message div
  95. quote.contents().each(function(i2, node) {
  96. node = $(node);
  97. //make sure we don't do shit with an already parsed quote
  98. if (!node.hasClass('message-top') && !node.hasClass('message')) {
  99. quoteBody.append(node);
  100. }
  101. });
  102. //add the new message div to the quoted-message if it's got anything in it
  103. if (quoteBody.contents()[0]) {
  104. quote.append(quoteBody);
  105. }
  106. });
  107. }
  108.  
  109. //adds hidden class to posts of ignorated users
  110. //needs stuff and jquery
  111. function ignorate(container) {
  112. rearrangeQuotes(container);
  113. userids(container);
  114. //get the messages and such
  115. $(container).find('.message').each(function(i, message) {
  116. message = $(message);
  117.  
  118. //get the images then check against filtered ids and execute the thingy
  119. function doTheThing(list, func, idCheck) {
  120. //make sure we're only getting the imgs elements from this message and not subordinate ones
  121. //shit in spoilers oh god this is so long i wish i could select descendents of the selection context with find()
  122. message.children('.spoiler_closed,.spoiler_opened').children('.spoiler_on_open').children('.imgs').add(message.children('.imgs')).find('a').each(function(i, img) {
  123. img = $(img);
  124. $(list).each(function(j, id) {
  125. if (idCheck == id) {
  126. //do the thing in the function and stuff
  127. func(img);
  128. //this guy breaks the loop
  129. return;
  130. }
  131. });
  132. });
  133. }
  134.  
  135. //FUCKING SPOILERS, IDIOT
  136. var idCheck = '1';
  137. //normal message
  138. if (message.parent()[0].nodeName == 'TR') {
  139. idCheck = message.parent().parent().parent().prev().attr('userID');
  140. doTheThing(IMAGENORATED_LINKS, toLinks, idCheck);
  141. doTheThing(IMAGENORATED_THUMBS, toThumbs, idCheck);
  142. doTheThing(IMAGENORATED_FULL, toFull, idCheck);
  143. }
  144. //normal quote
  145. //remember that we've used the rearrange quotes function
  146. else if (message.parent().hasClass('quoted-message') && message.parent().attr('msgid')) {
  147. idCheck = message.prev().attr('userID');
  148. doTheThing(IMAGENORATED_LINKS, toLinks, idCheck);
  149. //if we're on links, we should make quoted stuff into thumbnails
  150. if (linkCheck) {
  151. doTheThing(IMAGENORATED_THUMBS, toThumbs, idCheck);
  152. doTheThing(IMAGENORATED_FULL, toThumbs, idCheck);
  153. }
  154. }
  155. //anonymous quote in a normal message
  156. else if (message.parent().parent().hasClass('message') && message.parent().parent().attr('msgid')) {
  157. idCheck = message.parent().parent().parent().parent().parent().prev().attr('userID');
  158. doTheThing(IMAGENORATED_LINKS, toLinks, idCheck);
  159. //if we're on links, we should make quoted stuff into thumbnails
  160. if (linkCheck) {
  161. doTheThing(IMAGENORATED_THUMBS, toThumbs, idCheck);
  162. doTheThing(IMAGENORATED_FULL, toThumbs, idCheck);
  163. }
  164. }
  165. //anonymous quote in a quote. you can't nest farther than this
  166. else {
  167. idCheck = message.parent().parent().prev().attr('userID');
  168. doTheThing(IMAGENORATED_LINKS, toLinks, idCheck);
  169. //if we're on links, we should make quoted stuff into thumbnails
  170. if (linkCheck) {
  171. doTheThing(IMAGENORATED_THUMBS, toThumbs, idCheck);
  172. doTheThing(IMAGENORATED_FULL, toThumbs, idCheck);
  173. }
  174. }
  175. });
  176. }
  177.  
  178. //take a message element and turn its images into links
  179. function toLinks(img) {
  180. //make sure you don't gots links turned on
  181. if (img.children().length) {
  182. //hide old image thing
  183. img.children('.img-placeholder').addClass('hidden');
  184. //get image name
  185. var name = decodeURIComponent(img.attr('href').substring(img.attr('href').lastIndexOf('/') + 1));
  186. //stick name in link content plus a <br>
  187. var newImg = img.append(name + '<br>');
  188. }
  189. }
  190.  
  191. //take a message element and turn its images into thumbnails
  192. function toThumbs(img) {
  193. //check if links aren't turned on only because you can't reliably distinguish thumbs from fullsize
  194. //actually i guess you can't check for one but not the other <pre>ugh</pre>
  195. //hide old image thing
  196. img.children('.img-placeholder').addClass('hidden');
  197. //get image source and convert it to thumbnail
  198. var src = img.attr('imgsrc').replace(/endoftheinter\.net/, 'dealtwith.it').replace(/\/i\/n\//, '/i/t/').replace(/\.\w\w\w\w?$/, '.jpg');
  199. //add the thumb image maybe it'll work
  200. img.append('<span class="img-loaded"><img src="' + src + '"></span>');
  201. }
  202.  
  203. //take a message element and turn its images into fullsize inline images
  204. function toFull(img) {
  205. //not checking for inline fullsize because i don't think there's really any way to do it guaranteed
  206. //hide old image thing
  207. img.children('.img-placeholder').addClass('hidden');
  208. //get image source and convert it to thumbnail
  209. var src = img.attr('imgsrc').replace(/endoftheinter\.net/, 'dealtwith.it');
  210. //add the image
  211. img.append('<span class="img-loaded"><img src="' + src + '"></span>');
  212. }
  213.  
  214. //check if we're in link mode i guess
  215. var linkCheck = true;
  216. $('a.img').first().each(function(i, img) {
  217. linkCheck = ($(img).contents()[0].nodeType == 3)
  218. });
  219. //tag posts with userids
  220. livelinks(ignorate);
  221. addStyle('.hidden{display:none}', 'hidden');