User:Bradv/Scripts/MetaPagePreview.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
* Meta Page Preview
*     Author: User:Bradv
*     Version: 0.9
*
*	TODO: Update this whole script to use jQuery
*
*/
m_zIndex=101;
metapreview = {
    enabled:   true
};

metapreview.init = function() {
    if (!metapreview.enabled) return;
    handler = function (evt) {
       if (evt.target.tagName=='A' && evt.altKey && evt.button===0) {
           var title = evt.target.innerHTML;
           var titlehref = evt.target.href;
           console.log(evt.target.href);
           if (titlehref.indexOf(mw.config.get('wgServer'))<0 || titlehref.indexOf('&action=')>=0 
           || titlehref.indexOf('/wiki/Special:')>=0 || titlehref.indexOf('/wiki/Wikipedia:')>=0 
           || titlehref.indexOf('?title=Special:')>=0 || titlehref.indexOf('?title=Wikipedia:')>=0) {
				console.log('aborting');
				return;
           }
           var url = evt.target.href.replace('/wiki/', '/w/index.php?title=');
           if (!title) title = 'Untitled';

           showPreviewWindow(title, titlehref, url);
           evt.returnValue=false;
       }
    };
    window.addEventListener('click', handler, true);
};

showPreviewWindow = function(title, titlehref, url) {   
	console.log('showPreviewWindow('+title+','+titlehref+','+url);

	$('body').css('overflow','hidden');

    var cover = document.createElement('div');
    cover.id='meta_cover';
    cover.style.position='fixed';
    cover.style.zIndex=++m_zIndex;
    cover.style.opacity='0.1';
    cover.style.backgroundColor='black';
    cover.style.width='100%';
    cover.style.height='100%';
    cover.style.left='0px';
    cover.style.top='0px';
 
    var wnd = document.createElement('div');
    wnd.id='meta_previewWindow';
    wnd.style.position='fixed';
    wnd.style.padding='10px';
    wnd.style.zIndex=++m_zIndex;
    wnd.style.backgroundColor='white';
    wnd.style.border='5px solid #aaaaaa';
    wnd.style.width='90%';
    wnd.style.height='90%';
    wnd.style.minWidth='20em';
    wnd.style.minHeight='10em';

    //This is where we place the preview window - TODO: Check for modern skin
    //var obj = document.getElementById('column-one'); //Monobook
    var obj = document.body;
    obj.appendChild(cover);
    obj.appendChild(wnd);

    wnd.style.left=parseInt(window.innerWidth-wnd.clientWidth)/2 + 'px';
    wnd.style.top=parseInt(window.innerHeight-wnd.clientHeight)/2 + 'px';

    closePreview = function () {
        cover.parentNode.removeChild(cover);
        wnd.parentNode.removeChild(wnd);
        $('body').css('overflow','');
        return false;
    }; 

    cover.onclick = closePreview;

    var hdr = document.createElement('div');
    hdr.style.position='relative';
    hdr.style.width='100%';
    hdr.style.height='2em';
    hdr.style.borderBottom='1px solid #aaaaaa';
    hdr.style.cursor='move';
    wnd.appendChild(hdr);

    var closeButton = document.createElement('a');
    closeButton.href='#';
    closeButton.style.position='absolute';
    closeButton.style.top='10px';
    closeButton.style.right='10px';
    closeButton.onclick = closePreview;
    //closeButton.innerHTML = '<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Nuvola_apps_error.png/18px-Nuvola_apps_error.png"/>';
    var img = document.createElement('img');
    img.src = 'https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/Nuvola_apps_error.png/18px-Nuvola_apps_error.png';
    closeButton.appendChild(img);
    wnd.appendChild(closeButton);

    var content = document.createElement('div');
    content.id='sprint_previewContent';
    content.style.position='relative';
    content.style.clear='both';
    content.style.overflow='scroll';
    content.style.height=parseInt(wnd.clientHeight - hdr.offsetHeight - parseInt(wnd.style.padding)*2)+'px';
    //content.style.lineHeight='1.5em';
    wnd.appendChild(content);

    wnd.onmousedown=function(event) {
        if (wnd.style.zIndex < m_zIndex) {
            wnd.style.zIndex=++m_zIndex;
        }
    };

    hdr.onmousedown=function(event) {
        wnd.initialX = parseInt( event.clientX - wnd.offsetLeft );
        wnd.initialY = parseInt( event.clientY - wnd.offsetTop );
        window.onmouseup=function(event) {
            window.onmousemove=null;
            window.onmouseup=null;
            wnd.style.opacity='';
            content.style.display='';
        };
        window.onmousemove=function(event) {
            if (!content.style.display) {
                wnd.style.opacity='.8';
                content.style.display='none';
            }
            wnd.style.left=event.clientX-wnd.initialX+'px';
            wnd.style.top=event.clientY-wnd.initialY+'px';
        };
    };

    var resize = document.createElement('div');
    resize.id='sprint_previewResize';
    resize.style.position='absolute';
    resize.style.bottom='0px';
    resize.style.right='0px';
    resize.style.height='20px';
    resize.style.width='20px';
    resize.style.cursor='se-resize';
    wnd.appendChild(resize);

    resize.onmousedown=function(event) {
        wnd.initialWidth = parseInt( event.clientX - wnd.offsetWidth );
        wnd.initialHeight = parseInt( event.clientY - wnd.offsetHeight );
        window.onmouseup=function(event) {
            window.onmousemove=null;
            window.onmouseup=null;
            wnd.style.opacity='';
            content.style.height=parseInt(wnd.clientHeight - hdr.offsetHeight - parseInt(wnd.style.padding)*2)+'px';
            content.style.display='';
        };
        window.onmousemove=function(event) {
            if (!content.style.display) {
                wnd.style.opacity='.8';
                content.style.display='none';
            }
            wnd.style.width=event.clientX-wnd.initialWidth-parseInt(wnd.style.padding)*2+'px';
            wnd.style.height=event.clientY-wnd.initialHeight-parseInt(wnd.style.padding)*2+'px';
        };
    };

    //hdr.innerHTML='<a href="/wiki/'+title+'" style="font-size:1.5em;">'+title+'</a>';
    var aTitle = document.createElement('a');
    aTitle.href = titlehref;
    aTitle.style.fontSize = '1.5em';
    aTitle.appendChild(document.createTextNode(title));
    hdr.appendChild(aTitle);
    
    //content.innerHTML='Loading...';
    content.className = 'mw-body-content';
    while (content.firstChild) { content.removeChild(content.firstChild) };
    content.appendChild(document.createTextNode('Loading...'));

/*
	aj.onreadystatechange = function() {
        if(aj.readyState == 4 && aj.status == 200) {
            var htm;    
            htm = aj.responseText;
            content.innerHTML = htm;
            content.id = 'bodyContent'; //TODO: Find a better way to make the page format correctly
            if (ajaxPatrolLinks) {
                ajaxPatrolLinks(closeButton.onclick);
            }
        };                      
    };
            
    aj.open("GET", url, true);
    aj.send(null);*/
    
//	$( '#mybox' ).load( 'url #bodyContent' );
	$.get(
		url, {
			action: 'render'
		},
		function( data ) {
			/* Call was successful do something with "data" */
			content.innerHTML = data;
			content.className = 'mw-body-content';
		}
	);
};

$(metapreview.init);