Guest User

Kbin enhancement suite

a guest
Jun 15th, 2023
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. // ==UserScript==
  2. // @name kbin.social image hover
  3. // @version 0.1
  4. // @description Expand images by clicking, added collapse comment button
  5. // @author [email protected]
  6. // @match https://kbin.social/*
  7. // @grant none
  8. // ==/UserScript==
  9. //Feel free to alter this code however you want, credit me if you want but don't worry about it.
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. let figures = document.querySelectorAll('figure a img');
  15. let isDragging = false;
  16. let initialPosition;
  17.  
  18. figures.forEach(img => {
  19. img.addEventListener('click', function(e) {
  20. e.preventDefault(); // Prevent the <a> tag from navigating
  21.  
  22. // Find the footer for the article containing this image
  23. let footer = e.target.closest('article').querySelector('footer');
  24.  
  25. let overlay = footer.querySelector('#imgOverlay');
  26. if (!overlay) {
  27. let src = e.target.getAttribute('src');
  28. overlay = document.createElement('img');
  29. overlay.src = src;
  30. overlay.style.width = '300px'; // Set initial width
  31. overlay.id = 'imgOverlay'; // ID to easily remove it later
  32. overlay.style.userSelect = 'none'; // Prevent the image from being highlighted
  33. overlay.ondragstart = function() { return false; }; // Prevent the image from being dragged
  34. overlay.addEventListener('mousedown', startDrag);
  35. overlay.addEventListener('mousemove', drag);
  36. overlay.addEventListener('mouseup', endDrag);
  37. overlay.addEventListener('mouseleave', endDrag);
  38. footer.appendChild(overlay);
  39. } else {
  40. // If overlay is already present, remove it
  41. overlay.remove();
  42. }
  43. });
  44. });
  45.  
  46. function startDrag(e) {
  47. isDragging = true;
  48. initialPosition = e.clientX; // Get the initial x-position of the mouse
  49. }
  50.  
  51. function drag(e) {
  52. if (isDragging) {
  53. let widthChange = e.clientX - initialPosition; // Calculate the change in mouse position
  54. e.target.style.width = (e.target.offsetWidth + widthChange) + 'px'; // Change the width based on the mouse movement
  55. initialPosition = e.clientX; // Update the initial position for the next mousemove event
  56. }
  57. }
  58.  
  59. function endDrag(e) {
  60. isDragging = false;
  61. }
  62. })();
  63.  
  64. //comment collapse
  65.  
  66. (function() {
  67. 'use strict';
  68.  
  69. let blockquotes = document.querySelectorAll('blockquote');
  70. let levelRegex = /comment-level--(\d+)/;
  71.  
  72. blockquotes.forEach(blockquote => {
  73. // Get the first figure element inside the blockquote
  74. let figure = blockquote.querySelector('figure');
  75.  
  76. if (!figure) {
  77. return;
  78. }
  79.  
  80. // Add a collapse button
  81. let button = document.createElement('div');
  82. button.style.backgroundColor = 'rgb(28, 28, 28)';
  83. button.style.width = '35px';
  84. button.style.height = '35px';
  85. button.style.position = 'absolute';
  86. button.style.top = '50px'; // Position it below the figure
  87. button.style.left = '3px';
  88. button.style.cursor = 'pointer';
  89. button.style.zIndex = '100';
  90. button.style.display = 'flex';
  91. button.style.justifyContent = 'center';
  92. button.style.alignItems = 'center';
  93. button.style.fontSize = '20px';
  94. button.style.color = 'white';
  95. button.textContent = '-'; // "-" symbol
  96. button.addEventListener('mouseenter', () => button.style.backgroundColor = 'rgb(48, 48, 48)');
  97. button.addEventListener('mouseleave', () => button.style.backgroundColor = 'rgb(28, 28, 28)');
  98. button.addEventListener('click', e => {
  99. e.stopPropagation(); // Stop event from propagating to blockquote click handler
  100. blockquote.style.height = '60px';
  101. blockquote.style.overflow = 'hidden';
  102. hideChildComments(blockquote);
  103. });
  104.  
  105. // Add the button to the beginning of the figure
  106. figure.style.position = 'relative'; // To position the button relative to the figure
  107. figure.insertBefore(button, figure.firstChild);
  108.  
  109. // Add a click handler to the blockquote to expand it
  110. blockquote.addEventListener('click', () => {
  111. blockquote.style.height = 'auto';
  112. showChildComments(blockquote);
  113. });
  114. });
  115.  
  116. function hideChildComments(blockquote) {
  117. let level = getLevel(blockquote);
  118. let next = blockquote.nextElementSibling;
  119. while (next && getLevel(next) > level) {
  120. next.style.height = '0';
  121. next.style.marginTop = '-2px';
  122. next.style.padding = '0';
  123. next.style.visibility = 'hidden';
  124. next = next.nextElementSibling;
  125. }
  126. }
  127.  
  128. function showChildComments(blockquote) {
  129. let level = getLevel(blockquote);
  130. let next = blockquote.nextElementSibling;
  131. while (next && getLevel(next) > level) {
  132. next.style.height = 'auto';
  133. next.style.marginTop = '0';
  134. next.style.visibility = 'visible';
  135. next.style.padding = '16px 8px 16px 8px';
  136. next = next.nextElementSibling;
  137. }
  138. }
  139.  
  140. function getLevel(blockquote) {
  141. let level = levelRegex.exec(blockquote.className);
  142. return level ? parseInt(level[1]) : 0;
  143. }
  144. })();
Advertisement
Add Comment
Please, Sign In to add comment