Image Viewer

inject this script into any page, and right-click on the image you want to view full-size

当前为 2017-06-11 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Image Viewer
  3. // @namespace https://gist.github.com/bradenbest/04bd0fc2d57ec650449f
  4. // @version 1.6.0
  5. // @description inject this script into any page, and right-click on the image you want to view full-size
  6. // @copyright 2014 - 2017, Braden Best
  7. // ==/UserScript==
  8.  
  9. const firefox = /Firefox/i.test(navigator.userAgent);
  10.  
  11. function mk_node({
  12. name = "span",
  13. props = [],
  14. styles = [],
  15. events = [],
  16. extra = null
  17. } = {}){
  18. let el = document.createElement(name);
  19.  
  20. props.forEach(function([name, value]){
  21. el[name] = value;
  22. });
  23.  
  24. styles.forEach(function([name, value]){
  25. el.style[name] = value;
  26. });
  27.  
  28. events.forEach(function([name, callback]){
  29. el.addEventListener(name, function(ev){
  30. callback(ev, el);
  31. });
  32. });
  33.  
  34. if(extra)
  35. extra(el);
  36.  
  37. return el;
  38. }
  39.  
  40. function mk_img(url){
  41. return mk_node({
  42. name: "img",
  43. props: [
  44. ["src", url],
  45. ["className", "img_viewer"],
  46. ["draggable", "false"],
  47. ["dragging", 0],
  48. ["mousepos", [0,0]],
  49. ["tabIndex", 0]
  50. ],
  51. styles: [
  52. ["position", "absolute"],
  53. ["left", "0px"],
  54. ["top", (80 + document.body.scrollTop) + "px"],
  55. ["zIndex", "2147483647"]
  56. ],
  57. events: [
  58. ["mousedown", function(ev, self){
  59. self.dragging = firefox
  60. ? !self.dragging
  61. : 1;
  62.  
  63. self.mousepos[0] = (ev.clientX || ev.pageX);
  64. self.mousepos[1] = (ev.clientY || ev.pageY);
  65. }],
  66. ["mouseup", function(unused, self){
  67. if(firefox)
  68. return false;
  69.  
  70. self.dragging = 0;
  71. }],
  72. ["mousemove", function(ev, self){
  73. let curX = (ev.clientX || ev.pageX);
  74. let curY = (ev.clientY || ev.pageY);
  75. let diffX = curX - self.mousepos[0];
  76. let diffY = curY - self.mousepos[1];
  77.  
  78. if(!self.dragging || (!diffX && !diffY))
  79. return false;
  80.  
  81. self.mousepos = [curX, curY];
  82. self.style.left = parseInt(self.style.left) + diffX + "px";
  83. self.style.top = parseInt(self.style.top) + diffY + "px";
  84. }],
  85. ["keydown", function(ev, self){
  86. let dispatch = ({
  87. "ArrowUp": ["height", -10],
  88. "ArrowDown": ["height", +10],
  89. "ArrowLeft": ["width", -10],
  90. "ArrowRight": ["width", +10]
  91. })[ev.key] || ["width", 0];
  92.  
  93. self[dispatch[0]] = +self[dispatch[0]] + dispatch[1];
  94. }]
  95. ]
  96. });
  97. }
  98.  
  99. function mk_img_helper({img, img_helper_link}){
  100. let el = mk_node({
  101. name: "div",
  102. props: [
  103. ["innerHTML", "Click here to close image"],
  104. ["className", "img_viewer"]
  105. ],
  106. styles: [
  107. ["position", "fixed"],
  108. ["left", "0px"],
  109. ["top", "0px"],
  110. ["margin", "0"],
  111. ["padding", "5px 0"],
  112. ["width", "100%"],
  113. ["height", "50px"],
  114. ["background", "#fff"],
  115. ["borderBottom", "1px solid #ccc"],
  116. ["color", "#000"],
  117. ["fontSize", "12px"],
  118. ["textAlign", "center"],
  119. ["zIndex", "2147483647"]
  120. ],
  121. events: [
  122. ["click", function(unused, unused2){
  123. document.body.removeChild(img);
  124. document.body.removeChild(el);
  125. document.body.removeChild(img_helper_link);
  126. }]
  127. ]
  128. });
  129.  
  130. return el;
  131. }
  132.  
  133. function mk_img_helper_link(url){
  134. return mk_node({
  135. name: "a",
  136. props: [
  137. ["innerHTML", "Direct URL"],
  138. ["href", url],
  139. ["target", "_blank"],
  140. ["className", "img_viewer"]
  141. ],
  142. styles: [
  143. ["margin", "0"],
  144. ["padding", "0"],
  145. ["background", "#fff"],
  146. ["borderBottom", "1px solid #ccc"],
  147. ["display", "block"],
  148. ["width", "100%"],
  149. ["height", "20px"],
  150. ["position", "fixed"],
  151. ["left", "0"],
  152. ["top", "50px"],
  153. ["fontSize", "12px"],
  154. ["textAlign", "center"],
  155. ["zIndex", "2147483647"]
  156. ]
  157. });
  158. }
  159.  
  160. function mk_img_group(url){
  161. let img = mk_img(url);
  162. let img_helper_link = mk_img_helper_link(url);
  163. let img_helper = mk_img_helper({img, img_helper_link});
  164.  
  165. [img, img_helper, img_helper_link].forEach(function(node){
  166. document.body.appendChild(node);
  167. });
  168.  
  169. return { img, img_helper, img_helper_link };
  170. }
  171.  
  172. function clear(){
  173. [...document.querySelectorAll(".img_viewer")]
  174. .forEach(function(viewer){
  175. document.body.removeChild(viewer);
  176. });
  177. }
  178.  
  179. function init(){
  180. [...document.querySelectorAll("img")].forEach(function(img){
  181. img.addEventListener("contextmenu", function(ev){
  182. mk_img_group(img.src);
  183. ev.preventDefault();
  184. });
  185. });
  186.  
  187. document.body.addEventListener("mouseup", function(ev){
  188. if(firefox)
  189. return false;
  190.  
  191. [...document.querySelectorAll(".img_viewer")]
  192. .forEach(function(viewer){
  193. viewer.dragging = 0;
  194. });
  195. });
  196.  
  197. document.body.addEventListener("keydown", function(ev){
  198. let dispatch = ({
  199. "Escape": clear,
  200. "Control": init
  201. })[ev.key] || (()=>null);
  202.  
  203. dispatch();
  204. });
  205. }
  206.  
  207. init();
  208. console.log("Image Viewer started up.");
  209. console.log("Try right-clicking an image");

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址