Image Viewer

Image viewer

目前为 2014-05-03 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Image Viewer
  3. // @namespace https://gist.github.com/bradenbest/04bd0fc2d57ec650449f
  4. // @version 1.0.0
  5. // @description Image viewer
  6. // @copyright 2014+, Braden Best
  7. // ==/UserScript==
  8.  
  9. /*
  10. * Web image viewer script by Braden Best
  11. * To use:
  12. * inject this script into any page, and RIGHT-CLICK on the image you want to view full-size
  13. * while the image is "open", you can drag it around to pan instead of scrolling
  14. * There is a close button, and a full URL link above the image
  15. * You can press Esc to close the image without the need for clicking the close button
  16. * Special thanks to Cyberdevil for providing suggestions
  17. * Escape key
  18. * Provide Direct URL to image
  19. * image is draggable
  20. * Don't let it interfere with default click events
  21. */
  22. (function(){
  23. function push(url){
  24. var img = document.createElement('img'),
  25. img_helper = document.createElement('div');
  26. // Image
  27. img.src = url;
  28. img.style.position = 'absolute';
  29. img.style.left = '0px';
  30. img.style.top = '60px';
  31. img.style.zIndex = '1000000'; // this is to push it above everything else, so the NG navbar doesn't float over it.
  32. img.className = 'img_viewer';
  33. img.draggable = 'false';
  34. img.dragging = 0;
  35. img.mousepos = [0,0];
  36. // Image helper
  37. img_helper.innerHTML = "Click here to close image<hr><a target=\"_blank\" href=\"" + url + "\">Direct URL</a>";
  38. img_helper.style.position = 'absolute';
  39. img_helper.style.left = '0px';
  40. img_helper.style.top = '0px';
  41. img_helper.style.margin = '0';
  42. img_helper.style.padding = '5px 0';
  43. img_helper.style.width = '100%';
  44. img_helper.style.height='50px';
  45. img_helper.style.background = '#fff';
  46. img_helper.style.color = '#000';
  47. img_helper.style.fontSize = '12px';
  48. img_helper.style.textAlign = 'center';
  49. img_helper.style.zIndex = '2147483647'; // The absolute maximum
  50. img_helper.className = 'img_viewer';
  51. // append to body
  52. document.body.appendChild(img);
  53. document.body.appendChild(img_helper);
  54. document.body.scrollTop = 0;
  55. // helper on-click
  56. img_helper.onclick = function(){
  57. document.body.removeChild(img);
  58. document.body.removeChild(img_helper);
  59. img_helper.onclick = null;
  60. }
  61. img.onmousedown = function(evt){
  62. this.dragging = 1;
  63. this.mousepos[0] = (evt.clientX || evt.pageX);
  64. this.mousepos[1] = (evt.clientY || evt.pageY);
  65. }
  66. img.onmouseup = function(evt){
  67. this.dragging = 0;
  68. }
  69. img.onmousemove = function(evt){ // Hoo boy, that was pretty difficult to figure out
  70. if(this.dragging){
  71. lastX = this.mousepos[0];
  72. curX = (evt.clientX || evt.pageX);
  73. lastY = this.mousepos[1];
  74. curY = (evt.clientY || evt.pageY);
  75. if(!(lastX == curX && lastY == curY)){
  76. console.log("mouse has moved")
  77. if(curX > lastX){
  78. this.style.left = (parseInt(this.style.left) + (curX - lastX)) + 'px';
  79. }
  80. if(curX < lastX){
  81. this.style.left = (parseInt(this.style.left) - (lastX - curX)) + 'px';
  82. }
  83. if(curY > lastY){
  84. this.style.top = (parseInt(this.style.top) + (curY - lastY)) + 'px';
  85. }
  86. if(curY < lastY){
  87. this.style.top = (parseInt(this.style.top) - (lastY - curY)) + 'px';
  88. }
  89. }
  90. this.mousepos[0] = curX;
  91. this.mousepos[1] = curY;
  92. }
  93. return false;
  94. }
  95. }
  96.  
  97. function clear(){
  98. var imgs = document.querySelectorAll('.img_viewer');
  99. if(imgs[0]) {
  100. for(var i = 0; i < imgs.length; i++){
  101. document.body.removeChild(imgs[i]);
  102. }
  103. } else {
  104. console.log("No images generated by this script were found");
  105. }
  106. }
  107.  
  108. var imgs = document.querySelectorAll('img[src]'), i;
  109. if(imgs[0]){
  110. for(i = 0; i < imgs.length; i++){
  111. if(imgs[i].src){
  112. imgs[i].oncontextmenu = function(){
  113. push(this.src);
  114. return false;
  115. }
  116. }
  117. }
  118. } else {
  119. console.log("Something has gone wrong!");
  120. }
  121. document.body.onkeydown = function(evt){
  122. if(evt.keyCode == 27){ // Escape key
  123. clear();
  124. }
  125. }
  126. })();