Layout fixer

This script allows 1920x1080 screen users to adjust pages layout

目前为 2014-12-07 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Layout fixer
  3. // @namespace Layout fixer
  4. // @version 3.02
  5. // @description This script allows 1920x1080 screen users to adjust pages layout
  6. // @author leandro.azevedo@gmail.com
  7.  
  8. // @icon http://i.imgur.com/pLg6myd.png
  9. // @resource imgWide http://s9.postimg.org/n3jzwyoi3/img_Wide.png
  10. // @resource imgNarrow http://s9.postimg.org/5c8dii93f/img_Narrow.png
  11.  
  12. // @grant GM_addStyle
  13. // @grant GM_getResourceURL
  14. // @grant GM_getValue
  15. // @grant GM_setValue
  16.  
  17. // @run-at document-end
  18.  
  19. /*********** INCLUSUINS ***********/
  20. // @include *
  21.  
  22. /*********** EXCLUSUINS ***********/
  23. // @exclude *google.*tbm=isch*
  24. // @exclude *google.*/maps/*
  25. // @exclude *google.com/imgres*
  26. // @exclude *.jpg
  27. // @exclude *.gif
  28. // @exclude *.png
  29.  
  30. // ==/UserScript==
  31.  
  32.  
  33.  
  34. /* options */
  35.  
  36. var mode = 2; // 1: wide / 2: narrow
  37. var docWidth = 1000;
  38. var imgWide = GM_getResourceURL("imgWide");
  39. var imgNarrow = GM_getResourceURL("imgNarrow");
  40. if ( GM_getValue ("exceptionsList")===undefined ) GM_setValue ("exceptionsList", "");
  41. var exceptionsList = GM_getValue("exceptionsList");
  42. /* reset exceptions */
  43. //GM_setValue ("exceptionsList", "");
  44.  
  45. /* functions */
  46. function getComputedStyle(el,styleProp){
  47. var y;
  48. if (el.currentStyle)
  49. y = el.currentStyle[styleProp];
  50. else if (window.getComputedStyle)
  51. y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
  52. return y;
  53. }
  54.  
  55. function setHeight(){
  56. var body = document.body,
  57. html = document.documentElement;
  58. maxHeight = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );
  59. GM_addStyle('body {height:' + maxHeight + 'px');
  60. }
  61.  
  62. function switchLayout(){
  63. var exceptionsList = GM_getValue("exceptionsList");
  64.  
  65. if (mode==2) { // go wide
  66. mode = 1;
  67. img.src = imgWide;
  68. if ( exceptionsList.indexOf(cUrl)==-1 ) GM_setValue ("exceptionsList", exceptionsList + "," + cUrl);
  69. GM_addStyle('html {width: 100%}');
  70. GM_addStyle('html {margin: 0}');
  71. GM_addStyle('body {border: none; box-shadow: none; margin: 0}');
  72. } else { // go narrow
  73. mode = 2;
  74. img.src = imgNarrow;
  75. if ( exceptionsList.indexOf(cUrl)!=-1 ) GM_setValue ("exceptionsList", exceptionsList.replace("," + cUrl , "") );
  76. setNarrow();
  77. }
  78. }
  79.  
  80. function setNarrow(){
  81. GM_addStyle('html {position: relative; width: ' +docWidth+ 'px; margin-left: auto; margin-right: auto; background: #eee;}');
  82. GM_addStyle('body {width: 100%; border-left: 1px solid #999; border-right: 1px solid #999; box-shadow: 0 0 5px 1px #bbb; padding: 0; margin: 0}');
  83.  
  84. // GM_addStyle('body {padding: 0');
  85. // GM_addStyle('* {min-width: 1px}');
  86. // GM_addStyle('* {max-width: ' + docWidth + 'px;}');
  87. if ( getComputedStyle( document.body, 'background-color')=='transparent') GM_addStyle('body {background-color: #fff');
  88.  
  89.  
  90. // get flexible width from widest ( exclude position fixed, absolute)
  91.  
  92. elms = document.body.getElementsByTagName("*");
  93. var w;
  94. for (i=0 ; i<elms.length; i++) {
  95.  
  96. /* ajust for wider */
  97. //if (parseInt( getComputedStyle( elms[i], 'min-width') , 10) > docWidth){
  98. //if (parseInt( getComputedStyle( elms[i], 'width') , 10) > docWidth){
  99. if ( elms[i].offsetWidth > docWidth ) {
  100.  
  101. //GM_addStyle( 'html {width: ' + elms[i].offsetWidth + 'px}');
  102. //docWidth = elms[i].offsetWidth;
  103. //docWidth = parseInt( getComputedStyle( elms[i], 'width') , 10 );
  104. elms[i].style.width = docWidth + 'px';
  105. elms[i].style.padding = '0';
  106. elms[i].style.border = 'none';
  107. }
  108.  
  109.  
  110. if ( (getComputedStyle( elms[i], 'position')=='fixed') && (getComputedStyle( elms[i], 'left')=='0px') ) {
  111. elms[i].style.left = docLeft+1 + 'px';
  112. }
  113. if ( (getComputedStyle( elms[i], 'position')=='absolute') && (getComputedStyle( elms[i], 'left')=='0px') ) {
  114. elms[i].style.left = '1px';
  115. }
  116.  
  117. }
  118.  
  119. setHeight();
  120. }
  121.  
  122. /* code */
  123.  
  124. var cUrl = document.URL.match(/(\w{2,}\.\w{2,}\.\w{2,}|\w{2,}\.\w{2,3})/)[0];
  125. var docLeft = (window.innerWidth-docWidth-16)/2;
  126. var i, elms, css;
  127. if ( ( exceptionsList.indexOf(cUrl)==-1 ) && (mode==2) ) {
  128. setNarrow();
  129. window.addEventListener ("load", setHeight); // after full load
  130. } else {
  131. mode=1;
  132. }
  133. /* switcher button */
  134. var img = new Image();
  135. img.id = 'switcher';
  136.  
  137.  
  138. img.src = (mode==2) ? imgNarrow : imgWide;
  139.  
  140. img.onload = function() {
  141. document.body.appendChild(img);
  142. GM_addStyle('#switcher {position: fixed; bottom: 10px; right: 10px; z-index: 99999; opacity: 0.5}');
  143. };
  144.  
  145. img.addEventListener("click", switchLayout);
  146. img.addEventListener("mouseover", function() { img.style.opacity = '1'; } );
  147. img.addEventListener("mouseout", function() { img.style.opacity = '0.5'; } );
  148.  
  149. /*
  150. This script allows 1920x1080 screen users to adjust pages layout:
  151. <img src="http://s9.postimg.org/j9qjnt767/screenshot_2.png">
  152. <img src="http://s9.postimg.org/5u3iycyof/screenshot_3.png">
  153. <img src="http://s9.postimg.org/guyo3dqx7/screenshot_4.png">
  154. <img src="http://s9.postimg.org/ymaahu6bv/screenshot_5.png">
  155. */