The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.4KB

  1. /* image-hover.js
  2. * This script is copied almost verbatim from https://github.com/Pashe/8chanX/blob/2-0/8chan-x.user.js
  3. * All I did was remove the sprintf dependency and integrate it into 8chan's Options as opposed to Pashe's.
  4. * I also changed initHover() to also bind on new_post.
  5. * Thanks Pashe for using WTFPL.
  6. */
  7. if (active_page === "catalog" || active_page === "thread" || active_page === "index") {
  8. $(document).on('ready', function(){
  9. if (window.Options && Options.get_tab('general')) {
  10. Options.extend_tab("general",
  11. "<fieldset><legend>Image hover</legend>"
  12. + ("<label class='image-hover' id='imageHover'><input type='checkbox' /> "+_('Image hover')+"</label>")
  13. + ("<label class='image-hover' id='catalogImageHover'><input type='checkbox' /> "+_('Image hover on catalog')+"</label>")
  14. + ("<label class='image-hover' id='imageHoverFollowCursor'><input type='checkbox' /> "+_('Image hover should follow cursor')+"</label>")
  15. + "</fieldset>");
  16. }
  17. $('.image-hover').on('change', function(){
  18. var setting = $(this).attr('id');
  19. localStorage[setting] = $(this).children('input').is(':checked');
  20. });
  21. if (!localStorage.imageHover || !localStorage.catalogImageHover || !localStorage.imageHoverFollowCursor) {
  22. localStorage.imageHover = 'false';
  23. localStorage.catalogImageHover = 'false';
  24. localStorage.imageHoverFollowCursor = 'false';
  25. }
  26. if (getSetting('imageHover')) $('#imageHover>input').prop('checked', 'checked');
  27. if (getSetting('catalogImageHover')) $('#catalogImageHover>input').prop('checked', 'checked');
  28. if (getSetting('imageHoverFollowCursor')) $('#imageHoverFollowCursor>input').prop('checked', 'checked');
  29. function getFileExtension(filename) { //Pashe, WTFPL
  30. if (filename.match(/\.([a-z0-9]+)(&loop.*)?$/i) !== null) {
  31. return filename.match(/\.([a-z0-9]+)(&loop.*)?$/i)[1];
  32. } else if (filename.match(/https?:\/\/(www\.)?youtube.com/)) {
  33. return 'Youtube';
  34. } else {
  35. return "unknown: " + filename;
  36. }
  37. }
  38. function isImage(fileExtension) { //Pashe, WTFPL
  39. return ($.inArray(fileExtension, ["jpg", "jpeg", "gif", "png"]) !== -1);
  40. }
  41. function isVideo(fileExtension) { //Pashe, WTFPL
  42. return ($.inArray(fileExtension, ["webm", "mp4"]) !== -1);
  43. }
  44. function isOnCatalog() {
  45. return window.active_page === "catalog";
  46. }
  47. function isOnThread() {
  48. return window.active_page === "thread";
  49. }
  50. function getSetting(key) {
  51. return (localStorage[key] == 'true');
  52. }
  53. function initImageHover() { //Pashe, influenced by tux, et al, WTFPL
  54. if (!getSetting("imageHover") && !getSetting("catalogImageHover")) {return;}
  55. var selectors = [];
  56. if (getSetting("imageHover")) {selectors.push("img.post-image", "canvas.post-image");}
  57. if (getSetting("catalogImageHover") && isOnCatalog()) {
  58. selectors.push(".thread-image");
  59. $(".theme-catalog div.thread").css("position", "inherit");
  60. }
  61. function bindEvents(el) {
  62. $(el).find(selectors.join(", ")).each(function () {
  63. if ($(this).parent().data("expanded")) {return;}
  64. var $this = $(this);
  65. $this.on("mousemove", imageHoverStart);
  66. $this.on("mouseout", imageHoverEnd);
  67. $this.on("click", imageHoverEnd);
  68. });
  69. }
  70. bindEvents(document.body);
  71. $(document).on('new_post', function(e, post) {
  72. bindEvents(post);
  73. });
  74. }
  75. function imageHoverStart(e) { //Pashe, anonish, WTFPL
  76. var hoverImage = $("#chx_hoverImage");
  77. if (hoverImage.length) {
  78. if (getSetting("imageHoverFollowCursor")) {
  79. var scrollTop = $(window).scrollTop();
  80. var imgY = e.pageY;
  81. var imgTop = imgY;
  82. var windowWidth = $(window).width();
  83. var imgWidth = hoverImage.width() + e.pageX;
  84. if (imgY < scrollTop + 15) {
  85. imgTop = scrollTop;
  86. } else if (imgY > scrollTop + $(window).height() - hoverImage.height() - 15) {
  87. imgTop = scrollTop + $(window).height() - hoverImage.height() - 15;
  88. }
  89. if (imgWidth > windowWidth) {
  90. hoverImage.css({
  91. 'left': (e.pageX + (windowWidth - imgWidth)),
  92. 'top' : imgTop,
  93. });
  94. } else {
  95. hoverImage.css({
  96. 'left': e.pageX,
  97. 'top' : imgTop,
  98. });
  99. }
  100. hoverImage.appendTo($("body"));
  101. }
  102. return;
  103. }
  104. var $this = $(this);
  105. var fullUrl;
  106. if ($this.parent().attr("href").match("src")) {
  107. fullUrl = $this.parent().attr("href");
  108. } else if (isOnCatalog()) {
  109. fullUrl = $this.attr("data-fullimage");
  110. if (!isImage(getFileExtension(fullUrl))) {fullUrl = $this.attr("src");}
  111. }
  112. if (isVideo(getFileExtension(fullUrl))) {return;}
  113. hoverImage = $('<img id="chx_hoverImage" src="'+fullUrl+'" />');
  114. if (getSetting("imageHoverFollowCursor")) {
  115. var size = $this.parents('.file').find('.unimportant').text().match(/\b(\d+)x(\d+)\b/),
  116. maxWidth = $(window).width(),
  117. maxHeight = $(window).height();
  118. var scale = Math.min(1, maxWidth / size[1], maxHeight / size[2]);
  119. hoverImage.css({
  120. "position" : "absolute",
  121. "z-index" : 101,
  122. "pointer-events": "none",
  123. "width" : size[1] + "px",
  124. "height" : size[2] + "px",
  125. "max-width" : (size[1] * scale) + "px",
  126. "max-height" : (size[2] * scale) + "px",
  127. 'left' : e.pageX,
  128. 'top' : imgTop,
  129. });
  130. } else {
  131. hoverImage.css({
  132. "position" : "fixed",
  133. "top" : 0,
  134. "right" : 0,
  135. "z-index" : 101,
  136. "pointer-events": "none",
  137. "max-width" : "100%",
  138. "max-height" : "100%",
  139. });
  140. }
  141. hoverImage.appendTo($("body"));
  142. if (isOnThread()) {$this.css("cursor", "none");}
  143. }
  144. function imageHoverEnd() { //Pashe, WTFPL
  145. $("#chx_hoverImage").remove();
  146. }
  147. initImageHover();
  148. });
  149. }