Metalinks

A sidebar of links scraped from the current Metafilter page's comments.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

ستحتاج إلى تثبيت إضافة مثل Stylus لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتتمكن من تثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

ستحتاج إلى تثبيت إضافة لإدارة أنماط المستخدم لتثبيت هذا النمط.

(لدي بالفعل مثبت أنماط للمستخدم، دعني أقم بتثبيته!)

// ==UserScript==
// @name           Metalinks
// @namespace      http://example.com/metalinks
// @description    A sidebar of links scraped from the current Metafilter page's comments.
// @include        http://metafilter.com/*
// @include        http://*.metafilter.com/*
// @version 0.0.1.20140524193917
// ==/UserScript==

var pathPrefix = location.pathname;
var slashIdx = pathPrefix.indexOf('/', 1);
if (slashIdx != -1) {
	pathPrefix = pathPrefix.substr(0, slashIdx);
}
var urlPrefix = location.protocol + '//' + location.host + pathPrefix + '/';

function LinkInfo(link, text, author, time, backlink) {
	this.link = link;
	this.text = text;
	this.author = author;
	this.time = time;
	this.backlink = backlink;
}

var linkSuperset = document.evaluate(
	"//div[contains(concat(' ', @class, ' '), ' comments ')]//a",
	document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

var linkInfos = new Array();

for (var i = 0; i < linkSuperset.snapshotLength; i++) {
	var link = linkSuperset.snapshotItem(i);
	var parent = link.parentNode;
	var comment = null;
	while (true) {
		if ((parent == null) || (parent == document)) {
			break;
		}

		var parentClasses = ' ' + parent.getAttribute('class') + ' ';
		if ((parentClasses.indexOf(' smallcopy ') != -1) ||
				(parentClasses.indexOf(' whitesmallcopy ') != -1)) {
			break;
		}
		if (parentClasses.indexOf(' comments ') != -1) {
			comment = parent;
			break;
		}
		parent = parent.parentNode;
	}
	
	if (comment == null) {
		continue;
	}
	
	var smallcopyLinks = document.evaluate(
		"span[contains(concat(' ', @class, ' '), ' smallcopy ')]//a",
		comment, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
	if (smallcopyLinks.snapshotLength < 2) {
		continue;
	}
	
	if (link.href.indexOf(urlPrefix) == 0) {
		continue;
	}

	var author = smallcopyLinks.snapshotItem(0).innerHTML;
	var time = smallcopyLinks.snapshotItem(1).innerHTML;
	var backlink = smallcopyLinks.snapshotItem(1).href;	

	var text = comment.textContent;
	var postedByIndex = text.lastIndexOf('posted by');
	if (postedByIndex != -1) {
		text = text.substr(0, postedByIndex) + '               \r\n' + 
			text.substr(postedByIndex);

		var flagIndex = text.indexOf('[!]', postedByIndex);
		if (flagIndex != -1) {
			text = text.substr(0, flagIndex);
		}
		var emptyFavIndex = text.indexOf('[+]', postedByIndex);
		if (emptyFavIndex != -1) {
			text = text.substr(0, emptyFavIndex);
		}
	}

	var linkInfo = new LinkInfo(link, text, author, time, backlink);

	linkInfos.push(linkInfo);
}

var tagDivs = document.evaluate(
	"//div[@class='tags']",
	document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var tagDivsParent = tagDivs.snapshotItem(0).parentNode;

var linksDiv = document.createElement('div');
linksDiv.setAttribute('class', 'tags');
linksDiv.appendChild(document.createTextNode('Links:'));
linksDiv.appendChild(document.createElement('br'));

for (var i = 0; i < linkInfos.length; ++i) {
	var linkInfo = linkInfos[i];

	var span = document.createElement('span');
	span.setAttribute('style',
		'font-size:11px; font-weight:normal;');

	var newLink = document.createElement('a');
	newLink.href = linkInfo.link.href;
	newLink.innerHTML = linkInfo.link.innerHTML;
	newLink.setAttribute('style', 
		'font-size:11px; font-weight:normal; text-decoration:underline;');
	newLink.setAttribute('title', linkInfo.text);
	span.appendChild(newLink);

	span.appendChild(document.createTextNode(' | '));

	var backlink = document.createElement('a');
	backlink.href = linkInfo.backlink;
	backlink.innerHTML = 'src';
	backlink.setAttribute('style', 
		'font-size:11px; font-weight:normal; text-decoration:underline;');
	backlink.setAttribute('title', 'posted by ' + linkInfo.author + ' at ' + 
		linkInfo.time);
	span.appendChild(backlink);

	linksDiv.appendChild(span);
	linksDiv.appendChild(document.createElement('hr'));
}

tagDivsParent.appendChild(linksDiv);