User:Phlip/Greasemonkey

From Homestar Runner Wiki

(Difference between revisions)
Jump to: navigation, search
m (fix for Not the 100th Email!!!)
(fixing zoom out button with the wiki, javascript errors while flash is loading and adding "main" to the prev/next list)
Line 156: Line 156:
var zoomOutCell=row.insertCell(-1);
var zoomOutCell=row.insertCell(-1);
var zoomOutButton=document.createElement("button");
var zoomOutButton=document.createElement("button");
-
zoomOutButton.appendChild(document.createTextNode("–"));
+
// U+2013 is en dash - this seems to be the only way to encode this in a
 +
// javascript string - "\x2013" becomes " 13", Java-like "\U2013" isn't
 +
// supported, "–" doesn't work because it isn't parsed for HTML
 +
// and typing it directly has problems with the Latin-1 charset.
 +
zoomOutButton.appendChild(document.createTextNode(String.fromCharCode(0x2013)));
zoomOutCell.appendChild(zoomOutButton);
zoomOutCell.appendChild(zoomOutButton);
var zoomNormalCell=row.insertCell(-1);
var zoomNormalCell=row.insertCell(-1);
Line 190: Line 194:
sliderWidth=parseInt(getWidth(slider)-getWidth(thumb));
sliderWidth=parseInt(getWidth(slider)-getWidth(thumb));
frame=flash.CurrentFrame();
frame=flash.CurrentFrame();
-
framecountertext.nodeValue="\xA0"+(frame+1)+"/"+totalFrames()+"\xA0";
+
tot=totalFrames();
-
if(!dragging)
+
if (tot > 0)
{
{
-
thumb.style.left=parseInt(frame/totalFrames()*sliderWidth)+"px";
+
framecountertext.nodeValue="\xA0"+(frame+1)+"/"+tot+"\xA0";
-
paused=!flash.IsPlaying();
+
if(!dragging)
-
pauseButton.style.borderStyle=paused?"inset":"";
+
{
 +
thumb.style.left=parseInt(frame/totalFrames()*sliderWidth)+"px";
 +
paused=!flash.IsPlaying();
 +
pauseButton.style.borderStyle=paused?"inset":"";
 +
}
 +
}
 +
else
 +
{
 +
framecountertext.nodeValue="\xA0Loading...\xA0";
}
}
}
}
Line 300: Line 312:
// doesn't find pages like commandos3 or xmas04
// doesn't find pages like commandos3 or xmas04
var result;
var result;
-
    if ((result = filename.match(/^(sbemail|tgs|answer|bizcasfri|puppetjam)(\d+)$/)))
+
    if ((result = filename.match(/^(sbemail|tgs|answer|bizcasfri|puppetjam|main)(\d+)$/)))
{
{
if (result[1] != "sbemail" || result[2] != "100")
if (result[1] != "sbemail" || result[2] != "100")

Revision as of 16:51, 7 March 2006

// if you want to install this script, go here

// Homestar All-In-One
// version 1.0
// 2006-03-08
// Copyright (c) Phillip Bradbury, T Rice and Jesse Ruderman
//
// A combination of 4 useful scripts for Homestar Runner cartoons:
//  Homestar-Fullon, a greasemonkey script for making H*R cartoons fullscreen
//  Seek Bar, a bookmarklet that adds a progress bar to flash cartoons
//   (with modifications)
//  Previous/Next buttons for Strong Bad Emails, TGS, Marzipan's
//   Answering Machine, Biz Cas Fri and Puppet Jams
//  An HRWiki link on all pages.
//
// Released under the GPL
//
// Homestar-Fullon written by T Rice <timgm@bcheck.net> and is
//  released under the GPL.
//  http://dana.ucc.nau.edu/~tsr22/apps/greasemonkey/
//
// Seek bar written by Jesse Ruderman <jruderman@hmc.edu> and is
//  distributed with permission ("You may modify and/or distribute
//  up to three bookmarklets from this site in any way you want.")
//  http://www.squarefree.com/bookmarklets/
//  Originally it was just the Pause button and the seek bar,
//  I modified it to add a frame counter, frame step buttons and zoom buttons.
//  I also modified it so the Pause button automatically updates when the flash
//  movie pauses itself (eg at the end of the toon).
//
// Previous/Next buttons written by Phillip Bradbury, but inspired by
//  StrongBad Emails: Prev & Next from http://userscripts.org/scripts/show/1015
//
// HRWiki link also written by Phillip Bradbury, but inspired by an attempt
//  by Tom Preuss to do the same thing - except it did the translation from
//  URL to Wiki page name in the script, this does it at the HRWiki server
//  so that when a new toon comes out, the script doesn't need to be changed.
//  http://www.hrwiki.org/index.php/User:Tom/Greasemonkey_Script
//
// Direct any comments to
//  http://www.hrwiki.org/index.php/User_talk:Phlip/Greasemonkey
//
// --------------------------------------------------------------------
//
// WARNING: This script explicitly avoids use one of Greasemonkey's security
//  features. THIS SCRIPT SHOULD NOT BE USED on any page where you do not trust
//  the page writer. Not that it would make sense to anyway, given it's rather
//  Homestar-specific.
//
// One of the security features of Greasemonkey, used to plug its holes in
//  previous versions, is Mozilla's XPCNativeWrapper, which is used to ensure
//  that you're calling the real functions of objects on the page, and not
//  weird and potentially hazardous ones written by the page designer. However
//  this also blocks functions like flashmovie.currentFrame() which are needed
//  by the seek bar. Thus to make the seek bar work, I needed to turn this off.
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Homestar-Fullon", and click Uninstall.
//
// ==UserScript==
// @name          Homestar All-In-One
// @namespace     http://www.hrwiki.org/
// @description	  Combination of many Homestar Runner scripts
// @include       http://homestarrunner.com/*
// @include       http://www.homestarrunner.com/*
// @include       http://podstar.homestarrunner.com/*
// ==/UserScript==

(function() {
	var podstar = (location.hostname == "podstar.homestarrunner.com");

	// modified from Homestar-Fullon
	function resize() {
		if(flashmovie && flashmovie.width && flashmovie.height && flashmovie.width>0 && flashmovie.height>0) {
			var dw = window.innerWidth;
			var dh = window.innerHeight - (navbar&&navbar.height?navbar.height*2:0) - 25; // 25 for the seek bar
			var ar = flashmovie.width/flashmovie.height;
			if(dw/ar <= dh)
				dh = Math.floor(dw / ar);
			else
				dw = Math.floor(dh * ar);

			/* set embed's size */
			flashmovie.width = dw;
			flashmovie.height = dh;
		}
	}

	var objs = document.getElementsByTagName("EMBED");
	if (objs && objs.length >= 1 && !podstar)
	{
		var flashmovie = objs[0];
		var navbar = objs.length >= 2 ? objs[1] : false;
		document.body.style.margin = "0px";
		window.addEventListener("resize", resize, false);
		resize();
	}

	// modified from the seek bar bookmarklet
	function addFlashControls(flash)
	{
		var controlsDiv=document.createElement("div");
		var where=flash;
		while(where.parentNode.tagName.toLowerCase()=="object")
			where=where.parentNode;
		where.parentNode.insertBefore(controlsDiv,where.nextSibling);
		var table=document.createElement("table");
		controlsDiv.appendChild(table);
		var row=table.insertRow(-1);
		var pauseButton=document.createElement("button");
		pauseButton.appendChild(document.createTextNode("Pause"));
		var buttonCell=row.insertCell(-1);
		buttonCell.appendChild(pauseButton);
		var prevCell=row.insertCell(-1);
		var prevButton=document.createElement("button");
		prevButton.appendChild(document.createTextNode("|<"));
		prevCell.appendChild(prevButton);
		var slider=row.insertCell(-1);
		slider.width="100%";
		var visibleSlider=document.createElement("div");
		visibleSlider.style.position="relative";
		visibleSlider.style.height="10px";
		visibleSlider.style.width="100%";
		visibleSlider.style.MozBorderRadius="4px";
		visibleSlider.style.background="#aaa";
		slider.appendChild(visibleSlider);
		var thumb=document.createElement("div");
		thumb.style.position="absolute";
		thumb.style.height="20px";
		thumb.style.width="10px";
		thumb.style.top="-5px";
		thumb.style.MozBorderRadius="4px";
		thumb.style.background="#666";
		visibleSlider.appendChild(thumb);
		var nextCell=row.insertCell(-1);
		var nextButton=document.createElement("button");
		nextButton.appendChild(document.createTextNode(">|"));
		nextCell.appendChild(nextButton);
		var frameCell=row.insertCell(-1);
		var framecounter=document.createElement("div");
		framecounter.style.background="#ccc";
		framecounter.style.color="#000";
		framecounter.style.fontWeight="bold";
		frameCell.appendChild(framecounter);
		framecountertext=document.createTextNode("");
		framecounter.appendChild(framecountertext);
		var zoomOutCell=row.insertCell(-1);
		var zoomOutButton=document.createElement("button");
		// U+2013 is en dash - this seems to be the only way to encode this in a
		// javascript string - "\x2013" becomes " 13", Java-like "\U2013" isn't
		// supported, "–" doesn't work because it isn't parsed for HTML
		// and typing it directly has problems with the Latin-1 charset.
		zoomOutButton.appendChild(document.createTextNode(String.fromCharCode(0x2013)));
		zoomOutCell.appendChild(zoomOutButton);
		var zoomNormalCell=row.insertCell(-1);
		var zoomNormalButton=document.createElement("button");
		zoomNormalButton.appendChild(document.createTextNode("0"));
		zoomNormalCell.appendChild(zoomNormalButton);
		var zoomInCell=row.insertCell(-1);
		var zoomInButton=document.createElement("button");
		zoomInButton.appendChild(document.createTextNode("+"));
		zoomInCell.appendChild(zoomInButton);
		var sliderWidth;
		var paused=false;
		var dragging=false;
		var lastframe=-1;
		table.width=Math.max(parseInt(flash.width)||0,400);
		addEvent(pauseButton,"click",pauseUnpause);
		addEvent(prevButton,"click",prevFrame);
		addEvent(nextButton,"click",nextFrame);
		addEvent(zoomOutButton,"click",zoomOut);
		addEvent(zoomNormalButton,"click",zoomNormal);
		addEvent(zoomInButton,"click",zoomIn);
		addEvent(slider,"mousedown",drag);
		addEvent(slider,"drag",function(){return false;});
		window.setInterval(update,30);
		function pauseUnpause(){paused=flash.IsPlaying();pauseButton.style.borderStyle=paused?"inset":"";if(paused)flash.StopPlay();else flash.Play();}
		function prevFrame(){if(flash.IsPlaying()) pauseUnpause();flash.GotoFrame(flash.CurrentFrame()-1);}
		function nextFrame(){if(flash.IsPlaying()) pauseUnpause();flash.GotoFrame(flash.CurrentFrame()+1);}
		function zoomIn(){flash.Zoom(67);}
		function zoomOut(){flash.Zoom(150);}
		function zoomNormal(){flash.Zoom(0);}
		function update()
		{
			sliderWidth=parseInt(getWidth(slider)-getWidth(thumb));
			frame=flash.CurrentFrame();
			tot=totalFrames();
			if (tot > 0)
			{
				framecountertext.nodeValue="\xA0"+(frame+1)+"/"+tot+"\xA0";
				if(!dragging)
				{
					thumb.style.left=parseInt(frame/totalFrames()*sliderWidth)+"px";
					paused=!flash.IsPlaying();
					pauseButton.style.borderStyle=paused?"inset":"";
				}
			}
			else
			{
				framecountertext.nodeValue="\xA0Loading...\xA0";
			}
		}
		function dragMousemove(e)
		{
			var pageX=e.clientX+document.body.scrollLeft;
			var pos=bounds(0,pageX-getX(slider)-5,sliderWidth);
			var frame=bounds(1,Math.ceil(totalFrames()*pos/sliderWidth),totalFrames()-2);
			thumb.style.left=pos+"px";
			flash.GotoFrame(frame);
		}
		function release(e){removeEvent(document,"mousemove",dragMousemove);removeEvent(document,"mouseup",release);if(!paused)flash.Play();dragging=false;}
		function drag(e){addEvent(document,"mousemove",dragMousemove);addEvent(document,"mouseup",release);dragging=true;dragMousemove(e);}
		function bounds(min,val,max){return Math.min(Math.max(min,val),max);}
		function totalFrames(){if(typeof flash.TotalFrames=="number")return flash.TotalFrames;else if(typeof flash.TotalFrames=="function")return flash.TotalFrames();else return 1;}
		function getWidth(elem){if(document.defaultView&&document.defaultView.getComputedStyle)return parseFloat(document.defaultView.getComputedStyle(elem,null).getPropertyValue("width"));else return parseFloat(elem.offsetWidth);}
		function getX(elem){if(!elem) return 0;return(elem.offsetLeft)+getX(elem.offsetParent);}
		function addEvent(elem,eventName,fun){if(elem.addEventListener)elem.addEventListener(eventName,fun,false);else elem.attachEvent("on"+eventName,fun);}
		function removeEvent(elem,eventName,fun){if(elem.addEventListener)elem.removeEventListener(eventName,fun,false);else elem.detachEvent("on"+eventName,fun);}
	}
	// note use of wrappedJSObject so that the seek bar can access the Flash methods - see warning above
	if (objs && objs.length >= 1 && !podstar)
		addFlashControls(flashmovie.wrappedJSObject);


	function addHRWikiLink(pagename)
	{
		link = document.createElement("a");
		link.href="http://www.hrwiki.org/index.php/" + pagename;
		link.style.position="fixed";
		link.style.right="0px";
		link.style.top="0px";
		link.style.padding="5px";
		document.body.appendChild(link);
		img=document.createElement("img");
		img.style.border="0px";
		img.src="http://www.hrwiki.org/favicon.ico";
		link.appendChild(img);
	}
	// pull the filename from the url, use it as a link to HRWiki
	// all the filenames except interview and fhqwhgads are
	//  redirects to their articles
	var filename = location.pathname;
	i = filename.lastIndexOf('/');
	if (i >= 0)
		filename = filename.substr(i + 1);
	i = filename.lastIndexOf('.');
	if (i >= 0)
		filename = filename.substr(0,i);

	// don't link to [[interview]] or [[fhqwhgads]], neither are redirects
	// the first is an email, the second is a character
	// also special-case the portal index page (which doesn't have a wiki page)
	// and detect a 404 error
	     if (document.title == "Oops! You bwoke it.")
		addHRWikiLink("404'd");
	else if (podstar)
		addHRWikiLink("Podstar_Runner");
	else if (filename == "interview")
		addHRWikiLink("The_Interview");
	else if (filename == "fhqwhgads")
		addHRWikiLink("Everybody_to_the_Limit");
	else if (filename == "" || filename == "index")
		addHRWikiLink("Main_Page");
	else
		addHRWikiLink(filename);
	
	// Prev/Next links
	function addprevnextlinks(prefix,number)
	{
		if (number > 1)
		{
			var prevnum = (number - 1).toString();
			if (prefix == "sbemail" && number == 101) prevnum = "ahundred";
			var link = document.createElement("a");
			link.href=prefix+prevnum+".html";
			link.style.position="fixed";
			link.style.left="0px";
			link.style.bottom="0px";
			link.style.padding="5px";
			link.style.background="white";
			link.style.border="1px solid black";
			link.style.textDecoration="none";
			link.appendChild(document.createTextNode('<'));
			document.body.appendChild(link);
		}

		var nextnum = (number + 1).toString();
		if (prefix == "sbemail" && number == 99) nextnum = "ahundred";
		var link = document.createElement("a");
		link.href=prefix+nextnum+".html";
		link.style.position="fixed";
		link.style.right="0px";
		link.style.bottom="0px";
		link.style.padding="5px";
		link.style.background="white";
		link.style.border="1px solid black";
		link.style.textDecoration="none";
		link.appendChild(document.createTextNode('>'));
		document.body.appendChild(link);
	}
	
	// this is coded like this instead of just looking for /(\d+)/ so that it
	// doesn't find pages like commandos3 or xmas04
	var result;
	     if ((result = filename.match(/^(sbemail|tgs|answer|bizcasfri|puppetjam|main)(\d+)$/)))
	{
		if (result[1] != "sbemail" || result[2] != "100")
			addprevnextlinks(result[1],parseInt(result[2],10));
	}
	else if (filename == "sbemailahundred")
		addprevnextlinks("sbemail", 100);
	else if (filename == "dween_tgs")
		addprevnextlinks("tgs", 6);
})();

//
Personal tools