// ==UserScript== // @name Favstar // @namespace https://gist.github.com/1129263 // @description show favo count from favstar.fm // @include http://twitter.com/* // @include https://twitter.com/* // ==/UserScript== (function () { var IMG = "star"; var TEXT = "i"; var elemPool, starList, userName; function init () { elemPool = []; starList = null; userName = getUserName(); if (!userName) return; //Greasemonkey access violation: unsafeWindow cannot call GM_xmlhttpRequest. setTimeout(function () { GM_xmlhttpRequest({ method: "get", url: "http://favstar.fm/users/" + userName + "/rss", onreadystatechange: function (req) { if (req.readyState === 4) { run(req); } } }); }, 0); }; init(); function run (req) { starList = {}; var dom = (new DOMParser()).parseFromString(req.responseText, 'text/xml'); Array.prototype.forEach.call( dom.querySelectorAll('item'), function (item) { var text = item.textContent; var count = (/\d+/.exec(text)||[])[0]; var id = (/status\/(\d+)/.exec(text)||[,])[1]; if (id && count) starList[id] = count; }); elemPool.forEach(function (target) { putStar(target);}); }; function putStar (elem) { var id = elem.getAttribute('data-item-id'); if (starList[id]) { var target = elem.getElementsByClassName('tweet-user-name')[0]; if (0 < starList[id] && starList[id] < 6) { createStar(target, starList[id]); } else { createStar(target, 1); target.innerHTML += ["<", TEXT, ">x", starList[id], ""].join(''); } } }; function createStar (target, count) { for (var i = 0; i < count; i++ ) { target.innerHTML += ["<", IMG,">"].join(''); } }; document.addEventListener("DOMNodeInserted", function (e) { var target = e.target; if (!target.className || target.className.indexOf('stream-item') === -1) return; if (userName !== getUserName()) { init(); //reload } starList ? putStar(target) : elemPool.push(target) }, false); function getUserName () { return (/^#!\/(\w+)/.exec(location.hash)||[,])[1]; }; (function () { var imgUrl = "http://a2.twimg.com/a/1314996488/phoenix/img/sprite-icons.png"; var css = document.createElement('style'); css.innerHTML = [".tweet-user-name ", IMG, "{", "background: transparent url(", imgUrl, ") no-repeat;", "background-position:-64px 0; display:inline-block; width:15px;height:15px; }", ".tweet-user-name ", TEXT, "{ color: #aaa; }"].join(''); document.head.appendChild(css); })(); })();