Skip to content

Instantly share code, notes, and snippets.

@robinsk
Created November 7, 2009 19:22
Show Gist options
  • Save robinsk/228847 to your computer and use it in GitHub Desktop.
Save robinsk/228847 to your computer and use it in GitHub Desktop.
/**
* @fileOverview
* This file is a GreaseMonkey user script containing various Twitter hacks.
*
* Changelog
* ---------
* Version 1.0 (2009-11-07):
* - adds "Following you" to users in following/followers pages
*
* @author <a href="http://twitter.com/robinsk">Robin Skoglund</a>
* @version 1.0
*/
// ==UserScript==
// @name Twitterdrit
// @namespace http://dev.robinsk.net/twitter
// @description Various Twitter hacks.
// @include http://twitter.com/*
// ==/UserScript==
/**
* Runs an XPath query
*
* @param {String} query XPath query to evaluate
* @param {Number} resultType [optional] One of the XPathResult constants.
* @param {Node} contextNode [optional] Context node for evaluation.
* @return The XPathResult of the evaluated query, or simple value if the
resultType is one of NUMBER_TYPE, STRING_TYPE, or BOOLEAN_TYPE.
*/
function xpath(query, resultType, contextNode) {
if (!resultType) resultType = XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE;
if (!contextNode) contextNode = document;
var result = document.evaluate(query, contextNode, null, resultType, null);
switch (resultType) {
case XPathResult.NUMBER_TYPE:
return result.numberValue;
case XPathResult.STRING_TYPE:
return result.stringValue;
case XPathResult.BOOLEAN_TYPE:
return result.booleanValue;
default:
return result;
}
}
/**
* Adds "Following you" to users following you.
*/
function addIsFollowingYouRelationship() {
var followGrid = document.getElementById('follow_grid');
if (!followGrid) return;
var q = 'table/tbody/tr[contains(@class, "direct-messageable")]'
+ '/td[@class = "user-detail"]/address'
+ '/span[@class = "is-relationship"]';
var allRelationships = xpath(q, null, followGrid);
for (var i = 0; i < allRelationships.snapshotLength; i++) {
var relationship = allRelationships.snapshotItem(i);
q = 'span[contains(@class, "screenname")]/a = "' + sessionUser + '"';
if (xpath(q, XPathResult.BOOLEAN_TYPE, relationship.parentNode)) {
// That's you!
continue;
}
var followingYou = document.createElement('span');
followingYou.className = 'is-following';
followingYou.style.display = 'inline-block';
if (relationship.children.length > 0) {
// put "Following you" below "Following"
followingYou.style.top = '18px';
}
var lbl = document.createElement('strong');
lbl.innerHTML = 'Following you';
followingYou.appendChild(document.createElement('i'));
followingYou.appendChild(lbl);
relationship.appendChild(followingYou);
}
}
/**
* Session user's screen name.
* @type String
*/
var sessionUser = xpath('//meta[@name = "session-user-screen_name"]/@content', XPathResult.STRING_TYPE);
/**
* Page user's screen name.
* @type String
*/
var pageUser = xpath('//meta[@name = "page-user-screen_name"]/@content', XPathResult.STRING_TYPE);
// Add "Following you" to follow grids (except for your own followers list,
// that would be silly).
if (sessionUser != pageUser ||
!window.location.pathname.match(/\/followers$/)) {
addIsFollowingYouRelationship();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment