picMF

show MetaFilter profile pictures next to names in posts and comments

目前为 2014-10-04 提交的版本。查看 最新版本

// picMF.user.js
//
// Written by: Michael Devore
// Released to the public domain
//
// This is a Greasemonkey script.
// See http://www.greasespot.net/ for more information on Greasemonkey.
//
// ==UserScript==
// @name			picMF
// @namespace		http://www.devoresoftware.com/gm/picMF
// @description		show MetaFilter profile pictures next to names in posts and comments
// @match			https://*.metafilter.com/*
// @match			http://*.metafilter.com/*
// @grant           GM_xmlhttpRequest
// @run-at document-end
// @version 1.0
// ==/UserScript==
//

"use strict";

var theWidth = "64px";
var theHeight = "64px";
var zoomWidth = "256px";
var zoomHeight = "256px";
var serverPrefix = "//s3-us-west-2.amazonaws.com/mefi.profile/";

function onLoaded()
{
	var xpath = "//DIV/SPAN[starts-with(text(),'posted by') and (@class='smallcopy' or @class='smallcopy postbyline')]";
	var postNodes = document.evaluate(
		xpath,
		document,
		null,
		XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
		null
	);
	var total = postNodes.snapshotLength;
	for (var i = 0; i < total; i++)
	{
		// not much validation here, cuts performance overhead by avoiding extra tests against the nodes
		// tighten it down later if it conflicts with other add-ons or Metafilter bling
		var userSpan = postNodes.snapshotItem(i);
		var currentNode = userSpan.firstChild;
		var found = false;
		var userLink;
		var linkNode;
		while (currentNode && !found)
		{
			if (currentNode.nodeName === "A")
			{
				var href_value = currentNode.getAttribute('href');
				var result = href_value.match(/\/user\/(\d+)/);
				if (result && result[1])
				{
					var userNumber = result[1];
					var serverLink = serverPrefix+userNumber+".jpg";
					var img = document.createElement("img");
					img.setAttribute("src", serverLink);
					setTimeout(function(x, y)
						{
							return function() 
							{
								if (x.height > 0)
								{					
									x.setAttribute("height", theHeight);
									x.setAttribute("width", theWidth);
									y.insertBefore(document.createTextNode(" "), y.firstChild);
									y.insertBefore(x, y.firstChild);
									x.addEventListener('mouseover', picHover, false);
									x.addEventListener('mouseout', picRestore, false);
								}
							}; 
						}(img, currentNode), 1000);
					break;
				}
			}
			currentNode = currentNode.nextSibling;
		}
	}
}

function picHover(evt)
{
	var picNode = evt['target'];
	picNode.setAttribute("height", zoomHeight);
	picNode.setAttribute("width", zoomWidth);
}

function picRestore(evt)
{
	var picNode = evt['target'];
	picNode.setAttribute("height", theHeight);
	picNode.setAttribute("width", theWidth);
}

document.addEventListener('DOMContentLoaded',onLoaded,true);