4chan Gallery

Adds board title, catalog, gallery, and style to the top bar.

Fra og med 19.12.2014. Se den nyeste version.

// ==UserScript==
// @name          4chan Gallery
// @version       2.0.9
// @description   Adds board title, catalog, gallery, and style to the top bar.
// @include       http://boards.4chan.org/*
// @include       https://boards.4chan.org/*
// @namespace     https://greasyfork.org/users/3159
// @run-at document-end
// ==/UserScript==      
window.addEventListener('load', function () {

	var bname = document.getElementsByClassName("boardTitle")[0].innerHTML;
	if (bname.indexOf("/ - ") > -1) {
		bname = bname.split("/ - ")[1];
	}
	var path = document.location.pathname.split("/");

	function xc(q, r) {
		a = document.getElementsByClassName("boardList");
		a[0].insertAdjacentHTML(q, r);
		a[1].insertAdjacentHTML(q, r);
		b = document.getElementsByClassName("customBoardList");
		if (b[0]) {
			b[0].insertAdjacentHTML(q, r);
			b[1].insertAdjacentHTML(q, r);
		}
	}
	xc("afterbegin", '[<a href="//boards.4chan.org/' + path[1] + '">' + bname + "</a>] ");
	xc("beforeend", path[2] == "catalog" ? "" : '[<a href="//boards.4chan.org/' + path[1] + '/catalog">Catalog</a>] ');

	function xi(q) {
		var b = document.getElementById(q);
		return b;
	}
	var nr = '<span style="float:right">[<a class=low href=javascript:void(0)>Style</a>]&nbsp<span>';
	xi("navtopright").insertAdjacentHTML("afterend", nr);
	xi("navbotright").insertAdjacentHTML("afterend", nr);

	var themes = ['Yotsuba New', 'Yotsuba B New', 'Futaba New', 'Burichan New', 'Tomorrow', 'Photon'];

	function get() {
		cur = document.cookie.indexOf('ws_style') > -1 ? document.cookie.split('ws_style=')[1].split(';')[0] : 'Yotsuba New';
		for (i = 0; i < themes.length; i++) {
			if (themes[i] == cur) {
				return i;
			}
		}
	}

	function set(a) {
		document.cookie = 'ws_style=' + themes[a];
		if (path[2] == "catalog") {
			location.reload();
		} else {
			location.href = "javascript:setActiveStyleSheet('" + themes[a] + "')";
		}
	}

	function cstyle() {
		if (get() > (themes.length - 2)) {
			set(0);
		}
		else {
			set(get() + 1);
		}
	}
	var low = document.getElementsByClassName("low");
	low[0].onclick = function () {
		cstyle();
	};
	low[1].onclick = function () {
		cstyle();
	};

	var center = document.getElementsByClassName("center");
	for (i = 0; i < center.length; i++) center[i].style.display = "none";

	var form = document.getElementsByTagName('form')[0];
	form.insertAdjacentHTML("beforebegin", "<br>");
	form.insertAdjacentHTML("afterend", "<br>");

	if (path[2] == "thread") {
		//document.getElementById("postForm").style.display = "none";
		//document.getElementById("togglePostFormLink").style.display = "none";

		xc("beforeend", "[<a class=gl href=javascript:void(0)>Gallery</a>] ");
		var s = document.getElementsByClassName("gl");
		s[0].onclick = function () {
			trigger();
		};
		s[1].onclick = function () {
			trigger();
		};
		s[2].onclick = function () {
			trigger();
		};
		s[3].onclick = function () {
			trigger();
		};
		var newp = "<div id='newp' class='post reply' style='transform:rotate(-90deg);-webkit-transform:rotate(-90deg);-moz-transform:rotate(-90deg);-ms-transform:rotate(-90deg);transform-origin:top;-webkit-transform-origin:top;-moz-transform-origin:top;-ms-transform-origin:top;right:-7px;position:fixed;cursor:pointer;border:solid 1px'>&nbsp&nbspReply&nbsp&nbsp</div>";

		document.getElementById("delform").insertAdjacentHTML("afterend", newp);
		var newpid = document.getElementById("newp");
		var foot = document.getElementById('boardNavDesktopFoot');

		function npLocation() {
			if (foot.offsetTop > window.innerHeight) {
				newpid.style.top = "";
				newpid.style.bottom = "50px";
			}
			else {
				newpid.style.top = (foot.offsetTop - 50) + "px";
			}
		}
		npLocation();
		setInterval(function () {
			npLocation();
		}, 5000);
		newpid.onmouseover = function () {
			location.href = "javascript:QR.show(" + path[3] + ")";
		};

	}

	// code bellow is the gallery, works on most image boards, simply add a way to trigger
	// to test, copy paste code bellow into console then run trigger()

	function trigger() {
		if (find().length) {
			bg.style.display = "block";
			img.style.display = "block";
			ii.style.display = "block";
			i2.style.display = "block";
			larw.style.display = "block";
			rarw.style.display = "block";
			first ? null : ii.innerText = "[ Loading... ]";
			find();
			loader();
		} else {
			endalert();
		}
	}

	function find() {
		var aa = document.getElementsByTagName("a"),
			imgLinks = [];
		for (i = 0; i < aa.length; i++) {
				var link = aa[i].getAttribute("href");
				if (link) {
					ext = link.split(".").pop();
					if ((ext == "jpg" || ext == "png" || ext == "gif") && link.indexOf("google") < 0 && link.indexOf("iqdb") < 0 && link.indexOf("saucenao") < 0) {
						imgLinks.push(link);
					}
				}
			}
		var sorted = imgLinks.sort(),
			results = [];
		for (i = 0; i < sorted.length; i++) {
				if (sorted[i + 1] != sorted[i]) {
					results.push(sorted[i]);
				}
			}
		var names = document.getElementsByClassName("fileText"),
			title = [];
		for (i = 0; i < names.length; i++) {
				ext = names[i].children[0].getAttribute("href").split(".").pop();
				if (ext == "jpg" || ext == "png" || ext == "gif") {
					n = names[i].children[0].getAttribute("title");
					if (!n) {
						n = names[i].children[0].innerText;
					}
					title.push(n);
				}
			}
		img.src = results[a];
		atitle = title[a] ? title[a] : img.src;
		i2.innerText = "[ " + (a + 1) + "/" + results.length + " ]";
		return results;
	}

	function aii() {
		ii.innerText = "[ " + img.naturalWidth + "x" + img.naturalHeight + " ][ " + atitle + " ]";
	}

	function endalert() {
		if (bg.style.display == "block") {
			b = document.createElement("div");
			c = b.style;
			b.id = "theend";
			for (var d = "0123456789ABCDEF".split(""), a = "#", e = 0; 6 > e; e++) {
				a += d[Math.round(15 * Math.random())];
			}
			c.backgroundColor = a;
			c.cssText += "line-height:50px;width:200px;height:50px;position:fixed;margin:auto;left:0;right:0;top:0;bottom:0;font-size:42px;text-align:center;color:#fff;z-index:10001";
			b.innerText = "The End";
			document.body.appendChild(b);
			setTimeout(function () {
				z = document.getElementById("theend");
				z.parentNode.removeChild(z);
			}, 420);
		}
	}
	var a = 0,
		h = 0,
		first = false,
		atitle, img = new Image(),
		bg = document.createElement("div"),
		larw = document.createElement("div"),
		rarw = document.createElement("div"),
		ii = document.createElement("p"),
		i2 = document.createElement("p");
	bg.style.cssText = "opacity:0.9;position:fixed;top:0;left:0;z-index:9999;background-color:black;width:100%;height:100%;display:none";
	bg.onclick = function () {
			bg.style.display = "none";
			img.style.display = "none";
			ii.style.display = "none";
			i2.style.display = "none";
			larw.style.display = "none";
			rarw.style.display = "none";
		};
	document.body.appendChild(bg);
	img.style.cssText = "position:fixed;z-index:10000;max-height:calc(100% - 40px);max-width:calc(100% - 60px);margin:auto;top:0;bottom:0;left:0;right:0;display:none";
	img.onclick = function () {
			if (a < --find().length) {
				ii.innerText = "[ Loading... ]";
				a++;
				find();
			} else {
				endalert();
			}
		};
	img.onload = function () {
			first = true;
			aii();
		};
	document.body.appendChild(img);

	function loader() {
			var timg = new Image();
			timg.src = find()[h];
			timg.onload = function () {
				if (h++ < find().length) {
					loader();
				}
			};
		}
	var iv = "position:fixed;bottom:0;z-index:10000;color:#bababa;margin:0;display:none;";
	ii.style.cssText = iv + "left:0";
	i2.style.cssText = iv + "right:0";
	ii.onclick = function () {
			ii.innerText = "[ Loading... ]";
			a = 0;
			find();
		};
	i2.onclick = function () {
			ii.innerText = "[ Loading... ]";
			a = --find().length;
			find();
		};
	document.body.appendChild(ii);
	document.body.appendChild(i2);
	var arw = "position:fixed;z-index:10000;top:calc(50% - 15px);border-top: 15px solid transparent;border-bottom: 15px solid transparent;display:none;border-";
	larw.style.cssText = arw + "right: 20px solid #bababa;left:5px";
	rarw.style.cssText = arw + "left: 20px solid #bababa;right:5px";
	larw.onclick = function () {
			if (a > 0) {
				ii.innerText = "[ Loading... ]";
				a--;
				find();
			}
		};
	rarw.onclick = function () {
			if (a < --find().length) {
				ii.innerText = "[ Loading... ]";
				a++;
				find();
			} else {
				endalert();
			}
		};
	document.body.appendChild(larw);
	document.body.appendChild(rarw);
	document.addEventListener("keydown", function (e) {
			switch (e.which) {
			case 37:
				if (a > 0 && bg.style.display != "none") {
					ii.innerText = "[ Loading... ]";
					a--;
					find();
				}
				break;
			case 39:
				if (a < --find().length && bg.style.display != "none") {
					ii.innerText = "[ Loading... ]";
					a++;
					find();
				} else {
					endalert();
				}
				break;
			}
		});
});