Skip to content

Instantly share code, notes, and snippets.

@nanto
Created July 18, 2009 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nanto/149553 to your computer and use it in GitHub Desktop.
Save nanto/149553 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name bit.ly referred
// @namespace http://nanto.asablo.jp/blog/
// @include http://*
// @require http://svn.coderepos.org/share/lang/javascript/jsdeferred/trunk/jsdeferred.userscript.js
// ==/UserScript==
D = D();
const XHTML_NS = 'http://www.w3.org/1999/xhtml';
var site = null;
var entries = {};
D.
next(function () {
registerClearCacheMenu();
return GM_getValue('siteinfo', '') ||
D.xhttp.get('http://wedata.net/databases/LDRize/items.json').
next(function (res) {
GM_setValue('siteinfo', res.responseText);
return res.responseText;
});
}).
next(function (siteInfoData) {
var url = location.href;
JSON.parse(siteInfoData).some(function (item) {
if (new RegExp(item.data.domain).test(url)) {
site = item.data;
return true;
}
return false;
});
if (site) showClickCount();
}).
error(function (ex) {
GM_log(ex);
});
function showClickCount() {
var urls = [];
$XA(site.paragraph).forEach(function (p) {
var link = site.link ? $XA(site.link, p)[0] : p;
if (!link || !link.href || link.href in entries) return;
entries[link.href] = { paragraph: p, link: link };
urls.push(link.href);
});
if (!urls.length) return;
var param = urls.map(function(u) 'longUrl=' + encodeURIComponent(u)).join('&');
D.xhttp.get(bitlyAPIURL('shorten', param)).
next(function (res) {
var results = JSON.parse(res.responseText).results;
[i for (i in Iterator(results))].forEach(function ([url, item]) {
D.xhttp.get(bitlyAPIURL('stats', 'hash=' + item.hash)).
next(function (res) {
var clicks = JSON.parse(res.responseText).results.clicks;
var entry = entries[url];
var range = document.createRange();
range.selectNodeContents(entry.paragraph);
var fragment = range.createContextualFragment(
" " +
<span xmlns={ XHTML_NS } class="bitly-clicked-count">
{ clicks } clicks
</span>.toXMLString() +
" "
);
entry.link.parentNode.insertBefore(fragment, entry.link.nextSibling);
});
});
});
}
if (window.AutoPagerize) {
addAutoPagerizeFilter();
} else {
window.addEventListener('GM_AutoPagerizeLoaded', addAutoPagerizeFilter, false);
}
function addAutoPagerizeFilter() {
window.AutoPagerize.addFilter(function () showClickCount());
}
function bitlyAPIURL(command, param) {
if (!bitlyAPIURL.authParam) {
var name = memo('bit.ly Login Name').trim();
var key = memo('bit.ly API Key').trim();
if (!name || !key) throw "Can't use bit.ly API";
bitlyAPIURL.authParam = '&login=' + name + '&apiKey=' + key;
}
return 'http://api.bit.ly/' + command + '?version=2.0.1&' +
param + bitlyAPIURL.authParam;
}
function registerClearCacheMenu() {
GM_registerMenuCommand('bit.ly referred - Clear SITEINFO cache', function () {
GM_setValue('siteinfo', '');
});
}
function $XA(xpath, context) {
var res = document.evaluate(xpath, context || document, null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var nodes = [];
for (var i = 0; i < res.snapshotLength; i++)
nodes.push(res.snapshotItem(i));
return nodes;
}
function memo(name) {
var value = GM_getValue(name, null);
if (value === null) {
value = prompt(name);
if (value !== null)
GM_setValue(name, value);
}
return value;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment