Image Viewer

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

目前為 2014-05-04 提交的版本,檢視 最新版本

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