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.5.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. (function initialize(init){
  9. var init = init || 0;
  10. function push(url){
  11. var img = document.createElement('img'),
  12. img_helper = document.createElement('div'),
  13. img_helper_link = document.createElement('a');
  14. // Image
  15. img.src = url;
  16. img.style.position = 'absolute';
  17. img.style.left = '0px';
  18. img.style.top = (document.body.scrollTop) + 'px';
  19. img.style.zIndex = '2147483647'; // this is to push it above everything else, so the NG navbar doesn't float over it.
  20. img.className = 'img_viewer';
  21. img.draggable = 'false';
  22. img.dragging = 0;
  23. img.mousepos = [0,0];
  24. img.tabIndex = 0;
  25. // Image helper
  26. img_helper.innerHTML = "Click here to close image";
  27. img_helper.style.position = 'fixed';
  28. img_helper.style.left = '0px';
  29. img_helper.style.top = '0px';
  30. img_helper.style.margin = '0';
  31. img_helper.style.padding = '5px 0';
  32. img_helper.style.width = '100%';
  33. img_helper.style.height='50px';
  34. img_helper.style.background = '#fff';
  35. img_helper.style.borderBottom = '1px solid #ccc';
  36. img_helper.style.color = '#000';
  37. img_helper.style.fontSize = '12px';
  38. img_helper.style.textAlign = 'center';
  39. img_helper.style.zIndex = '2147483647'; // The absolute maximum
  40. img_helper.className = 'img_viewer';
  41. // Image helper link
  42. img_helper_link.innerText = 'Direct URL';
  43. img_helper_link.href = url;
  44. img_helper_link.target = '_blank';
  45. img_helper_link.style.margin = '0';
  46. img_helper_link.style.padding = '0';
  47. img_helper_link.style.background = '#fff';
  48. img_helper_link.style.borderBottom = '1px solid #ccc';
  49. img_helper_link.style.display = 'block';
  50. img_helper_link.style.width = '100%';
  51. img_helper_link.style.height = '20px';
  52. img_helper_link.style.position = 'fixed';
  53. img_helper_link.style.left = '0';
  54. img_helper_link.style.top = '50px';
  55. img_helper_link.style.fontSize = '12px';
  56. img_helper_link.style.textAlign = 'center';
  57. img_helper_link.style.zIndex = '2147483647';
  58. img_helper_link.className = 'img_viewer';
  59. // append to body
  60. document.body.appendChild(img_helper);
  61. document.body.appendChild(img_helper_link);
  62. document.body.appendChild(img);
  63. // helper on-click
  64. img_helper.onclick = function(){
  65. document.body.removeChild(img);
  66. document.body.removeChild(img_helper);
  67. document.body.removeChild(img_helper_link);
  68. }
  69. img.onmousedown = function(evt){
  70. this.dragging = 1;
  71. this.mousepos[0] = (evt.clientX || evt.pageX);
  72. this.mousepos[1] = (evt.clientY || evt.pageY);
  73. }
  74. img.onmouseup = function(evt){
  75. this.dragging = 0;
  76. }
  77. img.onmousemove = function(evt){ // Hoo boy, that was pretty difficult to figure out
  78. if(this.dragging){
  79. lastX = this.mousepos[0];
  80. curX = (evt.clientX || evt.pageX);
  81. lastY = this.mousepos[1];
  82. curY = (evt.clientY || evt.pageY);
  83. if(!(lastX == curX && lastY == curY)){
  84. console.log("mouse has moved");
  85. if(curX > lastX){
  86. this.style.left = (parseInt(this.style.left) + (curX - lastX)) + 'px';
  87. }
  88. if(curX < lastX){
  89. this.style.left = (parseInt(this.style.left) - (lastX - curX)) + 'px';
  90. }
  91. if(curY > lastY){
  92. this.style.top = (parseInt(this.style.top) + (curY - lastY)) + 'px';
  93. }
  94. if(curY < lastY){
  95. this.style.top = (parseInt(this.style.top) - (lastY - curY)) + 'px';
  96. }
  97. }
  98. this.mousepos[0] = curX;
  99. this.mousepos[1] = curY;
  100. }
  101. return false;
  102. }
  103. img.onkeydown = function(evt){
  104. var temp_width;
  105. if(evt.keyCode == 38) { // Up
  106. temp_width = parseInt(this.width) + 10;
  107. this.width = temp_width;
  108. } else if(evt.keyCode == 40) { // Down
  109. temp_width = parseInt(this.width) - 10;
  110. this.width = temp_width;
  111. } else {
  112. console.log("Key: " + evt.keyCode);
  113. }
  114. return false;
  115. }
  116. }
  117.  
  118. function clear(){
  119. var imgs = document.querySelectorAll('.img_viewer');
  120. if(imgs[0]) {
  121. for(var i = 0; i < imgs.length; i++){
  122. document.body.removeChild(imgs[i]);
  123. }
  124. } else {
  125. console.log("No images generated by this script were found");
  126. }
  127. }
  128.  
  129. var imgs = document.querySelectorAll('img[src]'), i;
  130. if(imgs[0]){
  131. for(i = 0; i < imgs.length; i++){
  132. if(imgs[i].src){
  133. imgs[i].oncontextmenu = function(){
  134. push(this.src);
  135. return false;
  136. }
  137. }
  138. }
  139. } else {
  140. console.log("Something has gone wrong!");
  141. }
  142. document.body.onkeydown = function(evt){
  143. if(evt.keyCode == 27){ // Escape key
  144. clear();
  145. }
  146. if(evt.keyCode == 17){ // Ctrl
  147. //clear();
  148. initialize(1);
  149. }
  150. }
  151. if(!init){
  152. console.log("Image Viewer successfully started up!");
  153. console.log("Try right-clicking an image");
  154. }else{
  155. console.log("Queue updated");
  156. }
  157. })();