Rule34 Direct Links

Provides direct links to Rule34 images in list view

  1. // ==UserScript==
  2. // @name Rule34 Direct Links
  3. // @namespace potato_potato
  4. // @version 0.2.4
  5. // @description Provides direct links to Rule34 images in list view
  6. // @author potato_potato
  7. // @match https://rule34.xxx/index.php?page=post&s=list*
  8. // @exclude https://rule34.xxx/index.php?page=post&s=view*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. const TEST_BANNER = false;
  14.  
  15. (() => {
  16. 'use strict';
  17. let thumbs = Array.from(document.getElementsByClassName('thumb'));
  18. let errors = [];
  19.  
  20. thumbs.forEach((thumb, index) => {
  21. try {
  22. // Just in case something changes, so we don't just flat out crash on our end.
  23. if (thumb.children.length > 0 && thumb.children[0].children.length > 0 && thumb.children[0].children[0].tagName == 'IMG') {
  24. // Guaranteed due to the above check.
  25. let img = thumb.children[0].children[0];
  26. let span = document.createElement('span');
  27. let link = img.src.replace("/thumbnails/", "/images/").replace("/thumbnail_", "/").replace(/\.jpg\?\d+$/, '');
  28. // TODO: Determine a better way to check for the `animated` tag, and potentially a way to determine if the
  29. // content is a gif or webm file.
  30. let tags = img.getAttribute('alt');
  31. if (tags.includes('video')) {
  32. span.innerHTML = `<br><a href="${link}.mp4">mp4</a>`;
  33. } else if (tags.includes('webm')) {
  34. span.innerHTML = `<br><a href="${link}.webm">webm</a>`;
  35. } else if (tags.includes('gif') || tags.includes('animated_gif')) {
  36. span.innerHTML = `<br><a href="${link}.gif">gif</a>`;
  37. } else if (tags.includes('animated')) {
  38. span.innerHTML = `<br><a href="${link}.gif">gif</a> &nbsp; <a href="${link}.webm">webm</a>`;
  39. } else {
  40. // Unfortunately, there's no way to tell what the format of a given image is. Perhaps it would be worth requesting something like
  41. // an automatic tag for the extension? `png`, `jpg`, `jpeg`, `gif`, etc as a meta tag?
  42. span.innerHTML = `<br><a href="${link}.jpeg">jpeg</a> &nbsp; <a href="${link}.jpg">jpg</a> &nbsp; <a href="${link}.png">png</a>`;
  43. }
  44. thumb.append(span);
  45. } else {
  46. errors.push({
  47. cause: 'Incorrect data structure for `thumb` instance',
  48. link: null, // We don't HAVE a link, due to bad data structure
  49. thumb_index: index,
  50. });
  51. }
  52. } catch (e) {
  53. errors.push({
  54. cause: 'Unknown cause. Page structure change?',
  55. link: null,
  56. thumb_index: index,
  57. });
  58. }
  59. index++;
  60. });
  61.  
  62. // TODO: Look into making the banner less hacky.
  63. if (errors.length > 0 || TEST_BANNER) {
  64. console.log("One or more errors have occured while attaching direct links to thumbnail elements:");
  65. console.log(errors);
  66. try {
  67. let banner_container = document.createElement('div');
  68. let banner_text = document.createElement('span');
  69. banner_text.textContent = 'Errors have occured while attaching direct links to thumbnails! Please check the console output!';
  70. banner_text.style['padding-top'] = '1em';
  71. banner_text.style['padding-bottom'] = '1em';
  72. banner_text.style.display = 'block';
  73. banner_text.style['font-size'] = 'large';
  74. let banner_source_info = document.createElement('span');
  75. banner_source_info.textContent = 'Rule34 Direct Links Userscript';
  76. banner_source_info.style['font-size'] = 'x-small';
  77. banner_source_info.style.right = '0.5em';
  78. banner_source_info.style.bottom = '0.25em';
  79. banner_source_info.style.position = 'absolute';
  80. banner_container.style.position = 'relative';
  81. banner_container.style.width = '100%';
  82. banner_container.style['text-align'] = 'center';
  83. banner_container.style.backgroundColor = '#E0A0A0';
  84. banner_container.appendChild(banner_text);
  85. banner_container.appendChild(banner_source_info);
  86. document.getElementById('content').prepend(banner_container);
  87. } catch (e) {
  88. console.log("Unable to attach error banner!");
  89. console.log(e);
  90. }
  91. }
  92. })();