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

QingJ © 2025

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