// ==UserScript==
// @name github monkey
// @namespace http://github.com/janus_wel
// @description install greasemonkey script easily in gist, and tell you the greasemonkey link to detail page in anyone's repository
// @include http://github.com/*
// @include http://gist.github.com/*
// @include https://gist.github.com/*
// ==/UserScript==
(function() {
$X('id("files")//div[@class="info"]/span').forEach(function(e) {
var filename = e.innerHTML;
if (/\.user\.js$/.test(filename)) {
var link = $X('parent::div/following-sibling::div[@class="actions"]/a', e)[0];
if (link) {
var el = link.cloneNode(true);
el.href += '?' + filename;
el.innerHTML = filename;
e.parentNode.replaceChild(el,e);
}
}
});
const xpath = 'id("browser")//a[' + end_with('@href', '.user.js') + ']';
$X(xpath).forEach( function (e) { e.href += '?'; });
// refer: http://piro.sakura.ne.jp/latest/blosxom/mozilla/xul/2007-09-13_selector-to-xpath.htm
function end_with(target, string) {
return ['substring(', target, ', string-length(', target, ') - string-length("', string, '") + 1) = "', string, '"'].join('');
}
function $X (exp, context) {
context = context || document;
var resolver = function (prefix) {
var o = document.createNSResolver(context)(prefix);
return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml";
}
var exp = document.createExpression(exp, resolver);
var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
// not ensure the order.
var ret = [];
var i = null;
while (i = result.iterateNext()) {
ret.push(i);
}
return ret;
}
}
return null;
};
})();