Github Gist Share

Share your GitHub Gist to Twitter, Dabblet & as userscript.

目前為 2014-02-24 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        Github Gist Share
// @namespace   https://greasyfork.org/scripts/54
// @description Share your GitHub Gist to Twitter, Dabblet & as userscript.
// @homepage    https://github.com/jerone/UserScripts/tree/master/Github_Gist_Share
// @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Gist_Share
// @downloadURL https://greasyfork.org/scripts/54/code.user.js
// @include     *://gist.github.com/*
// @version     1.20140224100236
// @grant       none
// @updateURL https://greasyfork.org/scripts/54/code.meta.js
// ==/UserScript==


String.format = function (string) {
	var args = Array.prototype.slice.call(arguments, 1, arguments.length);
	return string.replace(/{(\d+)}/g, function (match, number) {
		return typeof args[number] != "undefined" ? args[number] : match;
	});
};

var socials = {
	Twitter: {
		show: function(url, user, description, files, stars, forks, revisions){
			return true;
		},
		submit: function(url, user, description, files, stars, forks, revisions){
			var stats = [];
			if(files > 1){
				stats.push(files + " files");
			}
			if(stars == 1){
				stats.push(stars + " star");
			} else if(stars > 1){
				stats.push(stars + " stars");
			}
			if(forks == 1){
				stats.push(forks + " fork");
			} else if(forks > 1){
				stats.push(forks + " forks");
			}
			if(revisions > 1){
				stats.push(revisions + " revisions");
			}

			var tweet = String.format("Check out {0} #gist {1} on @github{2} |",
										user == document.querySelector(".name").textContent.trim() ? "my" : user + "'s",
										description ? "\"" + description + "\"" : "",
										stats.length > 0 ? " | " + stats.join(", ") : "");

			return "https://twitter.com/intent/tweet?original_referer=" + encodeURIComponent(url) +
						"&source=tweetbutton&url=" + encodeURIComponent(url) +
						"&text=" + encodeURIComponent(tweet);
		},
		icon: "https://si0.twimg.com/favicons/favicon.ico"
	},
	Dabblet: {
		show: function(url, user, description, files, stars, forks, revisions){
			// already defined in another UserScript: http://userscripts.org/users/31497/scripts
			return !document.getElementById("Github_Gist_Dabblet");
		},
		submit: function(url, user, description, files, stars, forks, revisions){
			var linkLong;
			if ((linkLong = document.querySelector(".site-container.js-site-container")) && linkLong.dataset.url) {
				var linkLongParts = linkLong.dataset.url.split("/");
				linkLongParts.shift();
				if (/^(?:revisions|forks|stars)$/gi.test(linkLongParts[linkLongParts.length - 1])) {
					linkLongParts.pop();
				}
				if (new RegExp(user,"gi").test(linkLongParts[0])) {
					linkLongParts.shift();
				}
				url = "/" + linkLongParts.join("/");
			} else {
				url = url.replace(new RegExp("https?:\/\/gist\.github\.com/" + user, "gi"), "");
			}
			return "http://dabblet.com/gist" + url;
		},
		icon: "http://dabblet.com/favicon.ico"
	},
	UserScript: {
		show: function(url, user, description, files, stars, forks, revisions){
			return !!document.querySelector(".bubble[id^='file-'] .raw-url[href$='.user.js']");
		},
		submit: function(url, user, description, files, stars, forks, revisions){
			return (document.querySelector(".bubble[id^='file-'] .raw-url[href$='.user.js']") || { href: ""}).href.trim();
		},
		icon: "http://wiki.greasespot.net/favicon.ico"
	}
};

function addMenuItem() {
	var link, url, menu, li, user, description, files, stars, forks, revisions;

	if ((link = document.querySelector("[name='link-field']")) && (menu = document.querySelector('ul.menu.gisttabs'))) {  // check if we're on an actual gists;
		url = link.value;
		user = document.querySelector(".author.vcard").textContent.trim();
		description = (document.querySelector(".gist-description") || document.querySelector(".js-current-repository") || { textContent: ""}).textContent.trim();
		files = document.querySelectorAll(".bubble[id^='file-']").length;
		stars = (menu.querySelector("a[href$='/stars'] .counter") || { textContent: ""}).textContent.trim();
		forks = (menu.querySelector("a[href$='/forks'] .counter") || { textContent: ""}).textContent.trim();
		revisions = (menu.querySelector("a[href$='/revisions'] .counter") || { textContent: ""}).textContent.trim();

		menu.appendChild(li = document.createElement("li"));
		li.id = "Github_Gist_Share";

		for(var key in socials){
			var social = socials[key],
				socialA = document.createElement("a"),
				socialImg = document.createElement("img");
				
			if (social.show(url, user, description, files, stars, forks, revisions) !== true) continue;
				
			li.appendChild(socialA);
			socialA.appendChild(socialImg);
			socialA.id = String.format("{0}_{1}", li.id, key.replace(/\s+/g, "_"));
			socialA.href = social.submit && social.submit(url, user, description, files, stars, forks, revisions);
			socialA.title = String.format("[{0}] {1}", key, socialA.href);
			socialA.style.display = "inline-block";
			socialA.target = "_blank";
			socialImg.src = social.icon;
			socialImg.alt = key;
		}
	}
}

// init;
addMenuItem();

// on pjax;
$(document).on("pjax:success", addMenuItem);