Layout fixer

...

目前為 2014-12-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name          Layout fixer
// @namespace     Layout fixer
// @description   ...
// @grant         GM_addStyle
// @author        [email protected]
// @version       1.0.1
// @icon          http://i.imgur.com/pLg6myd.png
// @run-at		  document-end

// @resource	imgWide imgWide.png
// @resource	imgNarrow imgNarrow.png
// @grant       GM_getResourceURL
// @grant       GM_getValue
// @grant       GM_setValue

/*********** INCLUSUINS ***********/
// @include	*

/*********** EXCLUSUINS ***********/
// @exclude	*google.*tbm=isch*
// @exclude	*google.*/maps/*
// @exclude	*google.com/imgres*
// @exclude	*.jpg
// @exclude	*.gif
// @exclude	*.png

// ==/UserScript==

	/* options */

	var mode 		= 2;	// 1: wide / 2: narrow
	var docWidth	= 1000;
	//var imgWide 	= "http://i.imgur.com/RAaGxdM.png"
	var imgWide 	= GM_getResourceURL("imgWide");
	//var imgNarrow	= "http://i.imgur.com/Zb0MHZF.png"
	var imgNarrow	= GM_getResourceURL("imgNarrow");

	/* reset values */
				// bug: 1st run: exceptionsList doesnt exist
	//GM_setValue ("exceptionsList", "");
	//alert( GM_getValue ("exceptionsList") );	 



	var cUrl = document.URL.match(/(\w{2,}\.\w{2,}\.\w{2,}|\w{2,}\.\w{2,3})/)[0];
	var docLeft = (window.innerWidth-docWidth-16)/2;
	var i, elms, css;
	

	var exceptionsList = GM_getValue("exceptionsList");
	if ( ( exceptionsList.indexOf(cUrl)==-1 ) && (mode==2) ) {
		setNarrow();
		window.addEventListener ("load", setHeight); // after full load
	} else {
		mode=1;
	}



/**************************************************************************/
function rgbToHex(r, g, b) {
    return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}

function getComputedStyle(el,styleProp){
	if (el.currentStyle)
		var y = el.currentStyle[styleProp];
	else if (window.getComputedStyle)
		var y = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
	return y;
}

function setHeight(){
	var body = document.body,
	    html = document.documentElement;
	maxHeight = Math.max( body.scrollHeight, body.offsetHeight, 
	                       html.clientHeight, html.scrollHeight, html.offsetHeight );
	GM_addStyle('body {height:' + maxHeight + 'px');
}


	function switchLayout(){
		var exceptionsList = GM_getValue("exceptionsList");

		if (mode==2) { // go wide
			mode = 1;
			img.src = imgWide;
			if ( exceptionsList.indexOf(cUrl)==-1 ) GM_setValue ("exceptionsList", exceptionsList + "," + cUrl);
			
			GM_addStyle('html {width: 100%}');
			GM_addStyle('html {margin: 0}');
			GM_addStyle('body {border: none; box-shadow: none; margin: 0}');
			
		} else { // go narrow
			mode = 2;
			img.src = imgNarrow;
			if ( exceptionsList.indexOf(cUrl)!=-1 ) GM_setValue ("exceptionsList", exceptionsList.replace("," + cUrl , "") );
			
			setNarrow()
		} 
	}

	function setNarrow(){


		GM_addStyle('html {position: relative; width: ' +docWidth+ 'px; margin-left: auto; margin-right: auto; background: #eee;}');
		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}');

	//	GM_addStyle('body {padding: 0');
	//	GM_addStyle('* {min-width: 1px}');
	//	GM_addStyle('* {max-width: ' + docWidth + 'px;}');
		
		if ( getComputedStyle( document.body, 'background-color')=='transparent') GM_addStyle('body {background-color: #fff');


	/*
	document.getElementsByTagName('html')[0].id='';
	document.getElementsByTagName('html')[0].className='';
	document.body.id='';
	document.body.className = '';
	*/

		// get flexible width from widest ( exclude position fixed, absolute)

		elms = document.body.getElementsByTagName("*");
		var w;
		for (i=0 ; i<elms.length; i++) {

			/* ajust for wider */
			//if (parseInt( getComputedStyle( elms[i], 'min-width') , 10) > docWidth){
			//if (parseInt( getComputedStyle( elms[i], 'width') , 10) > docWidth){
			if ( elms[i].offsetWidth > docWidth ) {

				//GM_addStyle( 'html {width: ' + elms[i].offsetWidth + 'px}');
				//docWidth = elms[i].offsetWidth;
				
				//docWidth = parseInt( getComputedStyle( elms[i], 'width') , 10 );
				elms[i].style.width = docWidth + 'px'; 
				elms[i].style.padding = '0'; 
				elms[i].style.border = 'none'; 
			}


			if ( (getComputedStyle( elms[i], 'position')=='fixed') && (getComputedStyle( elms[i], 'left')=='0px') ) {
				elms[i].style.left = docLeft+1 + 'px'; 
			}
			if ( (getComputedStyle( elms[i], 'position')=='absolute') && (getComputedStyle( elms[i], 'left')=='0px') ) {
				elms[i].style.left = '1px'; 
			}

			
			
		}


	
		setHeight();

	}

	/* switcher button */
	var img = new Image();
	img.id = 'switcher';


	img.src = (mode==2) ? imgNarrow : imgWide;

	img.onload = function() {
		document.body.appendChild(img);
		GM_addStyle('#switcher {position: fixed; bottom: 10px; right: 10px; z-index: 99999; opacity: 0.5}');
	};

	img.addEventListener("click", switchLayout);
	img.addEventListener("mouseover", function() { img.style.opacity = '1'; } );
	img.addEventListener("mouseout", function() { img.style.opacity = '0.5'; } );