januswel (owner)

Fork Of

Revisions

gist: 45079 Download_button fork
public
Description:
nstall greasemonkey script easily in gist, and tell you the greasemonkey link to detail page in anyone's repository
Public Clone URL: git://gist.github.com/45079.git
gist_link_user_js.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// ==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;
  };
})();