NeoGAF : Image Scaling Lite

Fixes the issue with image links (prevents them from opening a new page when clicked) and makes quoted images a bit larger.

// ==UserScript==
// @id             neogaf-image-scaler-ad3894bc-e561-4ddb-9c7a-c32ddbf93215@hr
// @name           NeoGAF : Image Scaling Lite
// @version        6.0.1
// @namespace      hateradio)))
// @author         hateradio
// @description    Fixes the issue with image links (prevents them from opening a new page when clicked) and makes quoted images a bit larger.
// @icon           data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA2ZpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDpCQjgyQUQ5M0VFNENFMjExOUIzN0RGOTZFQzU2Qjc1MSIgeG1wTU06RG9jdW1lbnRJRD0ieG1wLmRpZDoyNkQ4NzkyMzRENUYxMUUy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yfPDj/hyxOvALyoqmpbl/8LMABwaJb+K1ZUcwAAAABJRU5ErkJggg==

// @include        http*://*neogaf.com/forum/showthread.php*
// @include        http*://*neogaf.com/forum/showpost.php*

// @match          *://*.neogaf.com/forum/showthread.php*
// @match          *://*.neogaf.com/forum/showpost.php*

// @homepage       https://greasyfork.org/scripts/1025-neogaf-image-scaling-lite

// @updated        16 Jul 2014
// @since          28 Feb 2010
// @grant          GM_Log

// @run-at document-end
// ==/UserScript==

(function () {
	'use strict';

	(function () {
		function windowRun(cb) {
			var script = document.createElement('script');
			script.type = 'application/javascript';
			script.textContent = '(' + cb + ')();';
			document.body.appendChild(script);
			document.body.removeChild(script);
		}

		windowRun(function () {
			var load = window.onload || function () {};
			window.onload = null;

			// console.log('loading old window.onload');
			load();
			// window.YAHOO.util.Event.onDOMReady(load);
		});
	}());

	var pix = {
		width: 720, // px
		height: 560,
		loop: null,
		init: function () {
			this.fn.screen();
			this.fn.css('img[data-gmscale] { cursor: pointer; box-shadow:0px 0px 3px rgba(0, 0, 0, 0.52) } img[data-gmscale].full-image { box-shadow: 0 0 2px rgba(0, 0, 0, 0.3); margin-right: 16px } img[data-gmscale]:hover { box-shadow:0px 0px 4px rgba(0, 0, 0, 0.3) } .post img:not([class*="full-image"]):not(.inlineimg) { max-width:' + this.width + 'px !important; max-height: ' + this.height + 'px !important } .gm_scale_link { position: relative; display:table; display:compact; padding-bottom: 3px; } .quote .gm_scale_link {display:inline-block} .gm_scale_link:after { content:"External Link"; border: 1px solid #EAEAEA; bottom: 13px; left: 10px; position: absolute; background:white; padding: 1px 5px; color: #333; z-index:2; opacity: .9 } a.gm_scale_link:hover:after {opacity:.6}');

			this.fn.shrink();
			this.loop = window.setInterval(this.fn.shrink, 1500);

			// window.addEventListener('load', function () {
				// // pix.fn.procA();
				// // console.log("Clearing", pix.loop);
				// window.clearInterval(pix.loop);
				// // console.log(pix.loop);
			// }, false);
		},
		evt: {
			capture: function (e) {
				console.log(e);
				if (e.target.tagName !== 'A') {
					e.preventDefault();
				}
			}
		},
		fn: {
			invalid: function (img) {
				return img && img.nodeType && !img.hasAttribute('data-gmscale')
					&& (img.naturalWidth > pix.width || img.naturalHeight > pix.height);
			},
			screen: function () {
				pix.width = Math.round(document.querySelector('.post').getBoundingClientRect().width * 0.9);
				pix.height = Math.round(window.innerHeight * 0.85);
			},
			select: function (selector, cb) {
				var s = document.querySelectorAll(selector), i = s.length;
				while (i--) {
					if (cb(s[i], i, s) === false) {
						break;
					}
				}
			},
			parent: function (el, search) {
				if (el.tagName.toLowerCase() === search.toLowerCase()) {
					return el;
				}
				if (el.parentElement) {
					return this.parent(el.parentElement, search);
				}
				return false;
			},
			shrink: function () {
				// console.log('shrink');
				// console.log(document.querySelectorAll('.post img[data-gms]').length, document.querySelectorAll('.post img').length);
				// if (document.querySelectorAll('.post img[data-gms]').length === document.querySelectorAll('.post img').length) {
					// // console.log('clear shrink');
					// window.clearInterval(pix.loop);
				// }
				pix.fn.select('.post a[href] img', pix.fn.procA);
				pix.fn.select('.post img', pix.fn.procImg);
			},
			procA: function (img) {
				if (pix.fn.invalid(img)) {
					var a = pix.fn.parent(img.parentElement, 'a'); // a = img.parentElement;
					if (a && !a.hasAttribute('data-gmscale')) { // a.tagName === 'A'
						a.setAttribute('data-gmscale', true);
						a.className += ' gm_scale_link';
						a.title = 'Click to open';
						a.addEventListener('click', pix.evt.capture, true);
						img.title = '[Middle click to open new page.] ';
					}
					pix.fn.procImg(img, true);
				}
			},
			procImg: function (img, force) {
				img.setAttribute('data-gms', 0);
				if (force === true || pix.fn.invalid(img)) {
					img.title = 'Image: ' + (img.naturalWidth) + 'x' + (img.naturalHeight) + ' ' + img.title;
					img.setAttribute('data-gmscale', true);
				}
			},
			css: function (css) {
				var e = document.createElement('style');
				e.textContent = css;
				document.body.appendChild(e);
			}
		}
	};

	pix.init();

}());