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

QingJ © 2025

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