Created
February 7, 2009 05:15
-
-
Save mikedamage/59780 to your computer and use it in GitHub Desktop.
Uses Gists' API to create a new Gist from selected text.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new Github Gist from selection | |
CmdUtils.CreateCommand({ | |
name: "gist", | |
icon: "http://gist.github.com/favicon.ico", | |
author: {name: "Mike Green", email: "[email protected]", url: "http://mikedamage.github.com"}, | |
description: "Creates a new Github Gist from the currently selected text. IMPORTANT: You must give Firefox your Github API token by running github-auth before using this command.", | |
takes: {"username": noun_arb_text}, | |
preview: function(pblock, user) { | |
var template = "<b>New gist by ${user}:</b><p><code>${gist}</code></p>"; | |
var selection = CmdUtils.getSelection(); | |
pblock.innerHTML = CmdUtils.renderTemplate(template, {user: user.text, gist: selection}); | |
}, | |
execute: function(user) { | |
var url = "http://gist.github.com/api/v1/json/new"; | |
var auth = CmdUtils.retrieveLogins(user.text); | |
var selection = CmdUtils.getSelection(); | |
var data = { login: auth.username, token: auth.password, 'files[gist.txt]': selection }; | |
jQuery.post(url, data, function(json) { | |
var urlTemplate = "http://gist.github.com/${repo}"; | |
var repo = json.gists[0].repo; | |
var repoURL = CmdUtils.renderTemplate(urlTemplate, {repo: repo}); | |
CmdUtils.copyToClipboard(repoURL); | |
displayMessage("Successfully created new gist at " + repoURL); | |
}, "json"); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment