1+ // ==UserScript==
2+ // @name Github Gist Share
3+ // @namespace http://userscripts.org/scripts/show/157850
4+ // @description Github Gist Share
5+ // @include *://gist.github.com/*
6+ // @version 4
7+ // ==/UserScript==
8+
9+ String . format = function ( string ) {
10+ var args = Array . prototype . slice . call ( arguments , 1 , arguments . length ) ;
11+ return string . replace ( / { ( \d + ) } / g, function ( match , number ) {
12+ return typeof args [ number ] != "undefined" ? args [ number ] : match ;
13+ } ) ;
14+ } ;
15+
16+ var socials = {
17+ Twitter : {
18+ show : function ( url , user , description , files , stars , forks , revisions ) {
19+ return true ;
20+ } ,
21+ submit : function ( url , user , description , files , stars , forks , revisions ) {
22+ var stats = [ ] ;
23+ if ( files > 1 ) {
24+ stats . push ( files + " files" ) ;
25+ }
26+ if ( stars == 1 ) {
27+ stats . push ( stars + " star" ) ;
28+ } else if ( stars > 1 ) {
29+ stats . push ( stars + " stars" ) ;
30+ }
31+ if ( forks == 1 ) {
32+ stats . push ( forks + " fork" ) ;
33+ } else if ( forks > 1 ) {
34+ stats . push ( forks + " forks" ) ;
35+ }
36+ if ( revisions > 1 ) {
37+ stats . push ( revisions + " revisions" ) ;
38+ }
39+
40+ var tweet = String . format ( "Check out {0} #gist {1} on @github{2} |" ,
41+ user == document . querySelector ( ".name" ) . textContent . trim ( ) ? "my" : user + "'s" ,
42+ description ? "\"" + description + "\"" : "" ,
43+ stats . length > 0 ? " | " + stats . join ( ", " ) : "" ) ;
44+
45+ return "https://twitter.com/intent/tweet?original_referer=" + encodeURIComponent ( url ) +
46+ "&source=tweetbutton&url=" + encodeURIComponent ( url ) +
47+ "&text=" + encodeURIComponent ( tweet ) ;
48+ } ,
49+ icon : "https://si0.twimg.com/favicons/favicon.ico"
50+ } ,
51+ Dabblet : {
52+ show : function ( url , user , description , files , stars , forks , revisions ) {
53+ // already defined in another UserScript: http://userscripts.org/users/31497/scripts
54+ return ! document . getElementById ( "Github_Gist_Dabblet" ) ;
55+ } ,
56+ submit : function ( url , user , description , files , stars , forks , revisions ) {
57+ var linkLong ;
58+ if ( ( linkLong = document . querySelector ( ".site-container.js-site-container" ) ) && linkLong . dataset . url ) {
59+ var linkLongParts = linkLong . dataset . url . split ( "/" ) ;
60+ linkLongParts . shift ( ) ;
61+ if ( / ^ (?: r e v i s i o n s | f o r k s | s t a r s ) $ / gi. test ( linkLongParts [ linkLongParts . length - 1 ] ) ) {
62+ linkLongParts . pop ( ) ;
63+ }
64+ if ( new RegExp ( user , "gi" ) . test ( linkLongParts [ 0 ] ) ) {
65+ linkLongParts . shift ( ) ;
66+ }
67+ url = "/" + linkLongParts . join ( "/" ) ;
68+ } else {
69+ url = url . replace ( new RegExp ( "https?:\/\/gist\.github\.com/" + user , "gi" ) , "" ) ;
70+ }
71+ return "http://dabblet.com/gist" + url ;
72+ } ,
73+ icon : "http://dabblet.com/favicon.ico"
74+ } ,
75+ UserScript : {
76+ show : function ( url , user , description , files , stars , forks , revisions ) {
77+ return ! ! document . querySelector ( ".bubble[id^='file-'] .raw-url[href$='.user.js']" ) ;
78+ } ,
79+ submit : function ( url , user , description , files , stars , forks , revisions ) {
80+ return ( document . querySelector ( ".bubble[id^='file-'] .raw-url[href$='.user.js']" ) || { href : "" } ) . href . trim ( ) ;
81+ } ,
82+ icon : "http://wiki.greasespot.net/favicon.ico"
83+ }
84+ } ;
85+
86+ function addMenuItem ( ) {
87+ var link , url , menu , li , user , description , files , stars , forks , revisions ;
88+
89+ if ( ( link = document . querySelector ( "[name='link-field']" ) ) && ( menu = document . querySelector ( 'ul.menu.gisttabs' ) ) ) { // check if we're on an actual gists;
90+ url = link . value ;
91+ user = document . querySelector ( ".author.vcard" ) . textContent . trim ( ) ;
92+ description = ( document . querySelector ( ".gist-description" ) || document . querySelector ( ".js-current-repository" ) || { textContent : "" } ) . textContent . trim ( ) ;
93+ files = document . querySelectorAll ( ".bubble[id^='file-']" ) . length ;
94+ stars = ( menu . querySelector ( "a[href$='/stars'] .counter" ) || { textContent : "" } ) . textContent . trim ( ) ;
95+ forks = ( menu . querySelector ( "a[href$='/forks'] .counter" ) || { textContent : "" } ) . textContent . trim ( ) ;
96+ revisions = ( menu . querySelector ( "a[href$='/revisions'] .counter" ) || { textContent : "" } ) . textContent . trim ( ) ;
97+
98+ menu . appendChild ( li = document . createElement ( "li" ) ) ;
99+ li . id = "Github_Gist_Share" ;
100+
101+ for ( var key in socials ) {
102+ var social = socials [ key ] ,
103+ socialA = document . createElement ( "a" ) ,
104+ socialImg = document . createElement ( "img" ) ;
105+
106+ if ( social . show ( url , user , description , files , stars , forks , revisions ) !== true ) continue ;
107+
108+ li . appendChild ( socialA ) ;
109+ socialA . appendChild ( socialImg ) ;
110+ socialA . id = String . format ( "{0}_{1}" , li . id , key . replace ( / \s + / g, "_" ) ) ;
111+ socialA . href = social . submit && social . submit ( url , user , description , files , stars , forks , revisions ) ;
112+ socialA . title = String . format ( "[{0}] {1}" , key , socialA . href ) ;
113+ socialA . style . display = "inline-block" ;
114+ socialA . target = "_blank" ;
115+ socialImg . src = social . icon ;
116+ socialImg . alt = key ;
117+ }
118+ }
119+ }
120+
121+ // init;
122+ addMenuItem ( ) ;
123+
124+ // on pjax;
125+ $ ( document ) . on ( "pjax:success" , addMenuItem ) ;
126+
127+
128+
129+ // ==UserStats==
130+ // Chars (excl. spaces): 4.076
131+ // Chars (incl. spaces): 4.740
132+ // Words: 473
133+ // Lines: 134
134+ // ==/UserStats==
0 commit comments