RigInt Plus

By Seth_9 Uploaded Jun 28, 2009
jetpack.future.import("slideBar");
jetpack.future.import("storage.simple");
jetpack.future.import("clipboard");

var db = jetpack.storage.simple;
//var doc = jetpack.tabs.focused.contentDocument;
var alert = function(val) {
	jetpack.notifications.show({ body: val });
};

// Default settings
var userPrefs = {
	linkColor: "#0000ff", 
	linkColorOn: true, 
	userBlocklist: [], 
	searchOn: true, 
	resizeImg: true, 
	noSig: false, 
	embedYT: true, 
	blocklistYT: ["Frm8xv5wuvs", "KJ2yRTRlMFU", "OgSVXjNLFgo", "d1GDQYafAV8", "E4Dh01xth0M", "E14b_9wStUU", "ntpTuOpnt70"], 
	embedGoogle: true, 
	quoteMaxHeightOn: true, 
	quoteMaxHeight: "400px", 
	copyOnSubmit: true, 
	postWidthOn: true, 
	postWidth: "80%", 
	avatarsOn: true, 
	avatars: {
	"monster" :           "http://img15.imageshack.us/img15/9895/iconatorf034df892d40c00.gif", 
	"RomanyX" :           "http://img262.imageshack.us/img262/6890/rxavri.jpg", 
	"IanEye" :            "http://img200.imageshack.us/img200/860/7802ava.jpg", 
	"barracuda" :         "https://flashcasino.ladbrokes.com/instant-play-en/Help/Graphics/Reel_Strike_Barracuda.jpg", 
	"Jeff" :              "http://stuffgodhates.files.wordpress.com/2008/06/god_avatar3.jpg", 
	"brainpanhandler" :   "http://www.mychinamoto.com/forums/image.php?u=84&dateline=1210962726",  
	"Seamus OBlimey" :    "http://forums.govteen.com/images/avatars/avatar32780_10.gif", 
	"marmot" :            "http://img190.imageshack.us/img190/6591/portnoy0.png", 
	"orz" :               "http://beetle.motorious.org/wiki/images/9/9a/RedTriangleWithExclamationMark.gif", 
	"Joe Hillshoist" :    "http://img374.imageshack.us/img374/8632/20070405063428australiafm4.jpg", 
	"chlamor" :           "http://www.revleft.com/vb/customavatars/avatar13903_1.gif", 
	"American Dream" :    "http://www.jeboavatars.com/images/avatars/231709173066flag.jpg", 
	"8bitagent" :         "http://img188.imageshack.us/img188/1306/dalegribblejohnnyhardwi.jpg", 
	"slimmouse" :         "http://img297.imageshack.us/img297/48/imageutx.jpg", 
	"Penguin" :           "http://img193.imageshack.us/img193/8574/linux.png", 
	"Nordic" :            "http://img190.imageshack.us/img190/3711/norgenorway128x128.png"
	}
};

if ( db.userPrefs === undefined ) // If there are no stored prefs
{
	db.userPrefs = userPrefs; // Use default values
}
else // Otherwise use the stored prefs
{
	userPrefs = db.userPrefs;
}

// When a tab loads
jetpack.tabs.onReady( function(doc) 
{
	var $doc = $(doc);
	
	// If we're at RI
	if ( this.contentWindow.location.href.indexOf('rigorousintuition.ca') != -1 )
	{	
		// If we're reading a thread
		if ( this.contentWindow.location.href.indexOf('viewtopic.php') != -1 )
		{
			var posts = $doc.find("span.postbody");
			// Iterate over posts
			posts.each(function() 
			{
				var $post = $(this);
				//
				// CAP POST WIDTH
				//
				if ( userPrefs.postWidthOn )
				{
					
					$post.css(
					{ 
						'width' : userPrefs.postWidth, 
						'display' : 'block' 
					});
				}
				// Iterate over each link in the post
				$post.find('a').each(function() 
				{
					var $a = $(this);
					// 
					// EMBED YOUTUBE 
					//
					if ( userPrefs.embedYT ) 
					{
						var href = $a.attr("href");
						if( href.indexOf("youtube.com/watch?v=") > -1 )
						{
							// Grab the link text, unless it's a naked link
							var text = ( $a.text().indexOf("youtube.com/watch?v=") != -1 ) ? '' : $a.text();

							// Get the video ID
							var start = href.indexOf("youtube.com/watch?v=") + 20;
							var end = href.length;
							var tmp = href.substring(start, end); tmp = tmp.split("&");
							var vid_ID = tmp[0];

							// Replace the <a> with embed code
							var youtube_HTML = '';
							youtube_HTML += '<div style="width:425px;height:344px;">';
							youtube_HTML += '<object width="425" height="344">';
							youtube_HTML += '<param name="movie" value="http://www.youtube.com/v/';
							youtube_HTML += vid_ID + '&fs=1"></param>';
							youtube_HTML += '<param name="allowFullScreen" value="true"></param>';
							youtube_HTML += '<embed src="http://www.youtube.com/v/';
							youtube_HTML += vid_ID + '&fs=1" ';
							youtube_HTML += 'type="application/x-shockwave-flash" ';
							youtube_HTML += 'allowfullscreen="true" ';
							youtube_HTML += 'width="425" height="344">';
							youtube_HTML += '</embed>';
							youtube_HTML += '</object>';
							youtube_HTML += '</div>';
							
							// Don't embed vids in the blocklist
							if ( $.inArray(vid_ID, userPrefs.blocklistYT) < 0 )
							{
								$a.replaceWith(text + youtube_HTML);
							}
						}
					} 
					// 
					// EMBED GOOGLE VIDS
					//
					if ( userPrefs.embedGoogle )
					{
						if( (href.indexOf("video.google.com/videoplay?docid=") != -1) || (href.indexOf("video.google.ca/videoplay?docid=") != -1) )
						{
							// Grab the link text, unless it's a naked link
							var text = ( $a.text().indexOf("video.google.com/") != -1 ) ? '' : $a.text();
							
							// Get the video ID
							var start = href.indexOf("docid=") + 6;
							var end = ( href.indexOf('&') != -1 ) ? href.indexOf('&') : href.length;
							var tmp = href.substring(start, end); tmp = tmp.split("&");
							var vid_ID = tmp[0];
							
							// Replace the <a> with embed code
							var google_HTML = '';
							google_HTML += '<div style="display:block;">';
							google_HTML += '<embed id="VideoPlayback" src="http://video.google.com/googleplayer.swf?docid=';
							google_HTML += vid_ID;
							google_HTML += '&hl=en&fs=true" style="width:400px;height:326px" allowFullScreen="true" ';
							google_HTML += 'allowScriptAccess="always" type="application/x-shockwave-flash"></embed>'
							google_HTML += '</div>';
							
							$a.replaceWith(text + google_HTML);
						}
					}
					// 
					// LINK COLOR 
					//
					if ( userPrefs.linkColorOn )
					{
						$a.css({ "color" : userPrefs.linkColor });
					} 
				});
			});
			$doc.find("span.name b").each(function() 
			{
				var $b = $(this);
				var username = $b.text();
				//
				// AVATARS 
				//
				if ( userPrefs.avatarsOn )
				{
					if ( userPrefs.avatars[username] )
					{
						$b.after("<br /><br /><img src='"+userPrefs.avatars[username]+"' />");
					}
				}
				//
				// IGNORE USERS
				//
				if ( $.inArray( username, userPrefs.userBlocklist ) > -1 )
				{
					$b
					.parent("span")
					.parent("td")
					.parent("tr")
					.html("<td>&nbsp;</td><td><p>Post removed: " + username + "</p></td>")
					.css({ "min-height": "40px", "background-color": "#CCCCCC" })
					.nextAll()
					.slice(0,2)
					.css({ "display": "none" }); 
				}
			});
			//
			// QUOTE OVERFLOW
			//
			if ( userPrefs.quoteMaxHeightOn )
			{
				var wrap = '<div style="width:90%;max-height:'+userPrefs.quoteMaxHeight+';overflow-y:auto"></div>';
				$doc.find("td.quote")
					.parent("tr")
					.parent("tbody")
					.parent("table")
					.each(function() 
					{
						var $table = $(this);
						if( $table.parents("td.quote").length == 0 )
						{
							$table.wrap(wrap);
						}
					});
			}
			//
			// PREVENT IMG STRETCHING SCREEN
			//
			if ( userPrefs.resizeImg )
			{
				var max_width = 0.8*$(jetpack.tabs.contentWindow).width();
				posts.find("img").css({ "max-width": max_width });
			}
		}
		// If we're posting a reply
		else if ( this.contentWindow.location.href.indexOf('board/posting.php' != -1 ) )
		{
			// 
			// BACK UP POSTS TO CLIPBOARD
			//
			if ( userPrefs.copyOnSubmit )
			{
				$doc.find('form[action="posting.php"]').submit(function()
				{ 
					jetpack.clipboard.set( $doc.find('textarea[name="message"]').val() );
				});
			}
		}
		//
		// SEARCH BOX
		//
		if ( userPrefs.searchOn )
		{
			var search = '';
			search += '<form action="http://www.google.com/cse" id="cse-search-box" style="display:inline">';
			search += '<div style="display:inline">';
			search += '<input type="hidden" name="cx" value="partner-pub-4887758681658152:83be92-sea4" />';
			search += '<input type="hidden" name="ie" value="ISO-8859-1" />';
			search += '<input type="text" name="q" size="14" />';
			search += '<input type="submit" name="sa" value="Search" />';
			search += '</div>';
			search += '</form>';
			search += '<script type="text/javascript" ';
			search += 'src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>';
	   
			$doc.find("a.mainmenu[href='search.php']").replaceWith(search);	
		}
	}
});

// Add our content to the slide bar.
jetpack.slideBar.append(
{
	onReady: function (slide) {
		//
		// DISPLAY USER PREFS IN FORM FIELDS
		//
		// checkboxes
		$('#options input:checkbox', slide.contentDocument).each(function() 
		{
			$chkbx = $(this);
			$chkbx.attr('checked', userPrefs[$chkbx.attr('id')]);
		});
		// textboxes
		$('#options input:text', slide.contentDocument).each(function() 
		{
			$txtbx = $(this);
			$txtbx.val( userPrefs[$txtbx.attr('id')] );
		});
		// textareas
		$('#options textarea', slide.contentDocument).each(function() 
		{
			var $txtarea = $(this);
			var strVal = '';
			$.each( userPrefs[$txtarea.attr('id')], function(key, val) 
			{
				strVal += ( typeof key === "number" ) ? val+'\n' : key+'\n'+val+'\n';
			});
			$txtarea.val( strVal );
		});
		//
		// UTILITY FUNCTIONS
		//		
		var getTxtareaVals = function(txta, type)
		{
			var text = $('#'+txta, slide.contentDocument).val();
			text = text.replace(/\n\r?/g, '<br />');
			var array = text.split('<br />');
			if ( type == "obj" )
			{
				var tmpObj = {};
				for ( i=0; i<array.length-1; i++)
				{
					if ( i%2 == 0 ) tmpObj[array[i]] = array[i+1];
				}
				return tmpObj;
			}
			else if ( type == "arr" )
			{
				return array;
			}
		}
		var savePrefs = function() 
		{
			// Save checkbox values
			$('#options input:checkbox', slide.contentDocument).each(function() 
			{
				$chkbx = $(this);
				userPrefs[$chkbx.attr('id')] = ( $chkbx.attr('checked') === true ) ? true : false;
			});
			// Save textbox values
			$('#options input:text', slide.contentDocument).each(function() 
			{
				$txtbx = $(this);
				userPrefs[$txtbx.attr('id')] = $txtbx.val();
			});
			// Save avatars
			userPrefs.avatars = getTxtareaVals('avatars', 'obj');
			// Save YouTube ignore list
			userPrefs.blocklistYT = getTxtareaVals('blocklistYT', 'arr');
			// Save user ignore list
			userPrefs.userBlocklist = getTxtareaVals('userBlocklist', 'arr');
			
			// Save prefs
			db.userPrefs = userPrefs;
			// Close slidebar
			slide.close();
			// Reload page
			jetpack.tabs.focused.contentWindow.location = jetpack.tabs.focused.url;
		}
		//
		// EVENT HANDLERS
		//
		// save
		$('#save', slide.contentDocument).click(savePrefs);
	},
	width: 295, 
	persist: true, 
	// This automagically becomes our slide bar content.
	html: <>
	<style><![CDATA[
	  body { width: 275px; font-family: sans-serif; background-image:url('http://rigorousintuition.ca/board/templates/webmasterbucket/images/background.jpg')}
	  h1 { 
		font-size: 1em; 
		text-align: center; 
		color: #ffffff;
		background-image: url('http://www.rigorousintuition.ca/board/templates/webmasterbucket/images/cellpic3.gif');
		background-repeat: repeat-x;
		padding: 4px;
		margin-bottom: 0px;
	  }
	  .indent { margin-left: 15px}
	  div#options { padding: 1em; /*-moz-border-radius: 10px;*/ background: #FFF5EE; }
	  div#options a {color: blue}
	  div#buttons {
		text-align: center; 
		width: 100%;
		margin-top: 10px;
	  }
	  input[type='button'] {
		background-color : #FFF5EE;
	  }
	  textarea { width: 100%; }
	]]></style>
	<body>
		<h1>RigInt Plus Options</h1>
		<div id="options">
			<input type="checkbox" id="resizeImg" /> Resize large img<br />
			<input type="checkbox" id="linkColorOn" /> Link color: <input type="text" id="linkColor" size="7" /><br />
			<!--input type="checkbox" id="noSig" /> Block signatures<br /-->
			<input type="checkbox" id="quoteMaxHeightOn" /> Scroll tall quotes:<br />
				<span class="indent">Max height: </span>
				<input type="text" id="quoteMaxHeight" size="3" /><br />
			<input type="checkbox" id="postWidthOn" /> Cap post width:<br />
				<span class="indent">width: </span>
				<input type="text" id="postWidth" size="3" /><br />
			<input type="checkbox" id="searchOn" /> Search box<br />
			<input type="checkbox" id="embedGoogle" /> Embed Google video<br />
			<input type="checkbox" id="embedYT" /> Embed YouTube video<br />
			<input type="checkbox" id="copyOnSubmit" /> Backup posts to clipboard<br />
			Avatars<br />
			<textarea id="avatars" rows="7"> </textarea>
			YouTube ignore<br />
			<textarea id="blocklistYT" rows="6"> </textarea>
			Ignore users<br />
			<textarea id="userBlocklist" rows="3"> </textarea>
		</div>
		<div id="buttons">
			<input type="button" id="save" value="Save Options" /> 
		</div>
	</body>
	</>
});