Child pages
  • Titlebar Customizer
Skip to end of metadata
Go to start of metadata

Discussion: http://digitalgrin.com/showthread.php?t=25861

/*
--------------------------------------------------
	Titlebar Customizer
--------------------------------------------------
*/
var titleSettings = new Array();
titleSettings.separator    = ": "; // text to insert between parts of title
titleSettings.maxLength    = -1;   // limits length of title (-1 == no limit)
titleSettings.doPhotos     = true; // true == append photo captions
titleSettings.doAlbums     = true; // true == append album names
titleSettings.doGalleries  = true; // true == append gallery names
titleSettings.stripSmugmug = true; // true == remove " - powered by SmugMug" from title bar text

function ContextualizeTitle()
{
	var smugmugSuffix = " - powered by SmugMug";
	var insertAt = document.title.indexOf(smugmugSuffix);
	var newTitle = document.title;
	var newText = null;
	if(IsHomePage() == false)
	{
		// add gallery title
		if(titleSettings.doGalleries)
		{
			var galleryTitle = document.getElementById("galleryTitle");
			if(galleryTitle)
			{
				var galleryTitleText = GetTextContent(galleryTitle);
				if(galleryTitleText.length > 0)
				{
					var index = galleryTitleText.indexOf(" galleries");
					if(index > -1)
					{
						galleryTitleText = galleryTitleText.substr(0, index);
					}
					newText = titleSettings.separator + galleryTitleText;
					newTitle = InsertString(newTitle, newText, insertAt);
				}
			}
		}
		// add album title
		if(titleSettings.doAlbums)
		{
			var albumTitle = document.getElementById("albumTitle");
			if(albumTitle)
			{
				var albumTitleText = GetTextContent(albumTitle);
				if(albumTitleText.length > 0)
				{
					newText = titleSettings.separator + albumTitleText;
					newTitle = InsertString(newTitle, newText, insertAt);
				}
			}
		}
		// add photo title
		if(titleSettings.doPhotos)
		{
			var photoTitleText = GetPhotoCaption();
			if(photoTitleText.length > 0)
			{
				newText = titleSettings.separator + photoTitleText;
				newTitle = InsertString(newTitle, newText, insertAt);
			}
		}
		if(titleSettings.maxLength > -1)
		{
			newTitle = newTitle.substr(0, titleSettings.maxLength);
		}
	}
	if(titleSettings.stripSmugmug)
	{
		var regexp = /- powered by SmugMug/g;
		newTitle = newTitle.replace(regexp, "");
	}
	document.title = newTitle;
}

function GetPhotoCaption()
{
	var caption = "";
	var photoTitle = document.getElementById("caption_bottom");
	if(!photoTitle)
	{
		photoTitle = document.getElementById("caption_top");
	}
	if(photoTitle)
	{
		caption = GetTextContent(photoTitle);
	}
	return caption;
}

function GetTextContent(node)
{
	var text = "";
	if(node)
	{
		if(node.children)
		{
			text = GetTextContent(node.firstChild);
		}
		else
		{
			if(node.nodeValue)
			{
				text = node.nodeValue; // IE
			}
			else
			{
				text = node.textContent; // mozilla
			}
		}
	}
	text = Trim(text);
	return text;
}

function InsertString(text, insertText, index)
{
	var newText = "";
	if(index > -1 && index < text.length)
	{
		newText = text.substring(0, index) + insertText + text.substring(index);
	}
	else
	{
		newText = text + insertText;
	}
	return newText;
}

function Trim(text)
{
	var regexp = /^\s+|\s+$/g;
	text = text.replace(regexp, "");
	return text;
}

function IsHomePage()
{
	var isHomePage = false;
	// test for the "homepage" class name in the <BODY> tag
	var classStr = document.body.className;
	if (classStr && (classStr.indexOf("homepage") != -1))
	{
		isHomePage = true;
	}
	return isHomePage;
}

  • No labels

1 Comment

  1. Anonymous

    This code is out of date. Current code is in the discussion thread.