Skip to content

Instantly share code, notes, and snippets.

@siuying
Created April 15, 2009 15:12
Show Gist options
  • Save siuying/95823 to your computer and use it in GitHub Desktop.
Save siuying/95823 to your computer and use it in GitHub Desktop.
CmdUtils.CreateCommand({
name: 'pastie',
description: 'paste selected text to pastie',
help: 'pastie',
homepage: 'http://www.reality.hk',
author: {
name: 'siuying',
email: 'siuying [at] reality [dot] hk'
},
license: 'MPL',
takes: {
count: noun_arb_text
},
preview: "Create pastie using current selection",
/**
* Insert list of recents bookmarks into current rich text field, or show error message if not in rich text field
*/
execute: function(selection) {
var url = 'http://pastie.org/pastes';
var doc = context.focusedWindow.document;
CmdUtils.log("title: ", doc.title)
CmdUtils.log("selected text: ", selection.text);
var params = {
'paste[body]': doc.title + '\n' +
doc.location + '\n\n' + selection.text,
'paste[title]': doc.title,
'paste[restricted]': 0,
'paste[parser_id]': 11,
'paste[key]': '',
'paste[authorization]': 'burger',
'paste[commit]': 'Paste'
};
CmdUtils.log("POST ", url, params);
jQuery.ajax({
type: "POST",
url: url,
data: params,
dataType: "html",
error: function handleError(data) {
CmdUtils.log('err result ', data);
displayMessage("Error creating pastie!");
},
success: function handleResult(data) {
var links = jQuery(data).find('a.utility');
if (links.length > 0) {
displayMessage("Posting to pastie ...");
var redir = links[0].href;
redir = redir.substr(0, redir.length - 4) + '?wrap=1';
CmdUtils.log('redirect to: ', redir);
Utils.openUrlInBrowser(redir);
} else {
displayMessage("No pastie created. Pastie return unexpected result!");
}
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment