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

QingJ © 2025

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