seijikun / OPFinder

// ==UserScript==
// @name        OPFinder
// @namespace   seijikun.9gag
// @description Adds a button in the bottom right of gag-pages that searches for the gag's OP. When found, the OP's profile page will be opened.
// @include     http://9gag.com/gag/*
// @include     https://9gag.com/gag/*
// @version     1.1
// @license     MIT
// @grant       none
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==


/* HELPERS */
function findOP(){
	var commentView = $('section.post-comment div.comment-embed > div[data-reactid*="comment-view-http9gagcom"]');

	var commentExpandButtons = null;
	do {
		//search for op
		var opTag = commentView.find('span.label.role-op');
		if(opTag.length > 0) { //found op!
			var commentHeader = opTag.parent().parent();
			var authorElement = commentHeader.find('a.username');
			
			var authorProfileUrl = authorElement.attr('href');
			window.location.href = authorProfileUrl; //navigate to profile page
			
			return;
		}
		
		//raise click events on all commentExpandButtons
		commentExpandButtons = commentView.find('a.collapsed-comment > span').click();
	} while(commentExpandButtons.length > 0);

	//found no OP :(, disable button
  $(this).addClass('notFound');
}






/* INJECT UI */

//style
$('head').append('<style>\
  div.findOpButton:before {\
		content: "🔎"\
	}\
	div.findOpButton.notFound:before {\
		content: "🚫"\
	}\
	div.findOpButton.notFound {\
		border-color: rgba(0,0,0,0.7);\
		background: rgba(0,0,0,0.2);\
		color: black;\
	}\
	div.findOpButton {\
		position: fixed;\
		right: 140px;\
		bottom: 27px;\
		width: 50px;\
		color: white;\
		border: 1px solid;\
		padding: 3px;\
		border-color: rgba(0,0,0,0.2);\
		background: rgba(0,0,0,.9);\
		font-size: 20px;\
		cursor: pointer;\
		text-align: center;\
	}\
	div.findOpButton:hover {\
		border-color: rgba(0,0,0,0.7);\
		background: rgba(0,0,0,0.2);\
		color: black;\
	}\
	div.findOpButton .opTag {\
		text-transform: uppercase;\
		font-weight: 700;\
		color: #00b22d;\
		font-size: 11px;\
	}\
</style>');


//markup
var findOpButtonElement = $.parseHTML('<div class="findOpButton">\
	<div class="opTag">OP</div>\
</div>');
$('body').append(findOpButtonElement);
$(findOpButtonElement).on('click', findOP);