4chan Gallery

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

As of 2014-12-02. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          4chan Gallery
// @version       2.0.2
// @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 (a = 0; a < themes.length; a++) {
			if (themes[a] == cur) {
				return a;
			}
		}
	}

	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();
	};

	document.getElementById("togglePostFormLink").insertAdjacentHTML("beforebegin", "<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();
		};
		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] + ")";
		};

	}
	else {
		document.getElementById("togglePostFormLink").insertAdjacentHTML("afterend", "<br>");
	}

	// 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) {
			find();
			bg.style.display = "block";
			img.style.display = "block";
			ii.style.display = "block";
			i2.style.display = "block";
			larw.style.display = "block";
			rarw.style.display = "block";
			loader();
		} else {
			endalert();
		}
	}

	function find(l) {
		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 + " ]";
		aii();
		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,
		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) {
				a++;
				find();
			} else {
				endalert();
			}
		};
	document.body.appendChild(img);

	function loader() {
			var timg = new Image();
			timg.src = find()[h];
			timg.onload = function () {
				if (h++ < find().length) {
					aii();
					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 () {
			a = 0;
			find();
		};
	i2.onclick = function () {
			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) {
				a--;
				find();
			}
		};
	rarw.onclick = function () {
			if (a < --find().length) {
				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") {
					a--;
					find();
				}
				break;
			case 39:
				if (a < --find().length && bg.style.display != "none") {
					a++;
					find();
				} else {
					endalert();
				}
				break;
			}
		});
});