Picture Auto-Resize on Chrome

Auto-Resize on Chrome Picture Preview Tab

当前为 2016-09-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Picture Auto-Resize on Chrome
  3. // @name:zh-cn 图片在Chrome中自动缩放
  4. // @namespace https://gf.qytechs.cn/users/2646
  5. // @version 0.5
  6. // @description Auto-Resize on Chrome Picture Preview Tab
  7. // @description:zh-cn 让大图在Chrome窗体内自动调整到适合的缩放大小,一览图片全貌(不会影响图片保存)
  8. // @author 2016+, CLE
  9. // @include *
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. if( ! document.contentType.match(/^image\//i) ) return;
  14.  
  15. var img = document.getElementsByTagName("img")[0];
  16.  
  17. var imginfo = document.createElement("div");
  18. imginfo.setAttribute("style", "position:fixed; right:10px; top:5px; z-index:10086; color:#FFF; font-size: 26px; opacity:0.5; text-shadow: 0px 0px 5px #000; padding: 1px; text-align: right;");
  19.  
  20. var imgfsize = -1; var tfs = "unknow";
  21. var xhr = new XMLHttpRequest();
  22. xhr.open('HEAD', document.location.href, true);
  23. //xhr.open('HEAD', document.location.href, false);
  24. xhr.onreadystatechange = function(){
  25. if ( xhr.readyState == 4 ) {
  26. if ( xhr.status == 200 ) {
  27. imgfsize = Number( xhr.getResponseHeader('Content-Length') );
  28. if(imgfsize > 0){
  29. if(imgfsize < 1024)
  30. tfs = imgfsize + " bytes";
  31. else if( imgfsize <=1024000 )
  32. tfs = (imgfsize/1024).toFixed(2) + " KB";
  33. else if( imgfsize >1024000 )
  34. tfs = (imgfsize/1024/1024).toFixed(2) + " MB";
  35. refinfo();
  36. }
  37. } else {
  38. imgfsize = -1;
  39. }
  40. }
  41. };
  42. xhr.send(null);
  43.  
  44.  
  45.  
  46. function refinfo(){
  47. var infohtml;
  48. if(navigator.language=="zh-CN"){
  49. infohtml = '图片宽:'+ img.width + ' / ' + img.naturalWidth + '<br/>图片高:' + img.height + ' / ' + img.naturalHeight;
  50. if(imgfsize > 0) infohtml += '<br/>文件大小:' + tfs;
  51. }else{
  52. infohtml = 'Width: '+ img.width + ' / ' + img.naturalWidth + '<br/>Height: ' + img.height + ' / ' + img.naturalHeight;
  53. if(imgfsize > 0) infohtml += '<br/>File Size: ' + tfs;
  54. }
  55. imginfo.innerHTML = infohtml;
  56. }
  57. refinfo();
  58. document.body.appendChild(imginfo);
  59.  
  60.  
  61.  
  62. function defsize(){
  63. img.height = img.naturalHeight;
  64. img.width = img.naturalWidth;
  65. refinfo();
  66. }
  67.  
  68. function autoresize() {
  69. if ( img.naturalHeight > window.innerHeight || img.naturalWidth > window.innerWidth ) {
  70. var hb = 0; var zb = 0; var rat = 0;
  71. if(img.naturalWidth > window.innerWidth) hb = img.naturalWidth / window.innerWidth;
  72. if(img.naturalHeight > window.innerHeight) zb = img.naturalHeight / window.innerHeight;
  73. if(hb !== 0 && zb !== 0){
  74. if(hb >= zb) rat = hb; else rat = zb;
  75. } else if (hb !==0) {
  76. rat = hb;
  77. } else if (zb !==0) {
  78. rat = zb;
  79. }
  80. if (rat !==0 ){
  81. img.width = img.naturalWidth / rat;
  82. img.height = img.naturalHeight / rat;
  83. }
  84. }
  85. refinfo();
  86. }
  87.  
  88. autoresize();
  89. window.onresize = autoresize;
  90.  
  91.  
  92. var defm = false;
  93. window.onkeydown = function(event){
  94. switch(event.keyCode) {
  95. case 13: //enter
  96. if(defm){
  97. window.onresize = autoresize;
  98. autoresize();
  99. }else{
  100. window.onresize = null;
  101. defsize();
  102. }
  103. defm = !defm;
  104. break;
  105. case 27: //escape
  106. window.close();
  107. break;
  108. case 8: //backspace
  109. if(imginfo.style.display=="none")
  110. imginfo.style.display = "block";
  111. else
  112. imginfo.style.display = "none";
  113. break;
  114. }
  115. };

QingJ © 2025

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