Skip to content

Instantly share code, notes, and snippets.

@sprsquish
Created September 5, 2008 22:39
Show Gist options
  • Select an option

  • Save sprsquish/9044 to your computer and use it in GitHub Desktop.

Select an option

Save sprsquish/9044 to your computer and use it in GitHub Desktop.
/** * @author chuck */
Tumblr = {
addUsername: function(username) { Application.prefs.setValue('tumblr_user_username', username); },
addPassword: function(password) { Application.prefs.setValue('tumblr_user_password', password); },
addGroup: function(group) { Application.prefs.setValue('tumblr_group', group); },
getUsername: function() { return Application.prefs.get('tumblr_user_username').value; },
getPassword: function() { return Application.prefs.get('tumblr_user_password').value; },
getGroup: function() { return Application.prefs.get('tumblr_group').value; },
everythingIsOK: function() { return Application.prefs.has('tumblr_user_username') && Application.prefs.has('tumblr_user_password'); },
createPostObj: function(postObj) {
postObj = jQuery.extend(postObj, {
email: Tumblr.getUsername(),
password: Tumblr.getPassword(),
generator: 'Tumblr Ubiquity by Chuck v0.1'
});
if ((group = Tumblr.getGroup()).length > 2)
postObj.group = group;
return postObj;
},
post: function(postObj) {
if (Tumblr.everythingIsOK())
jQuery.post('http://www.tumblr.com/api/write', Tumblr.createPostObj(postObj), function() { displayMessage('Tumbl post completed!'); });
else
displayMessage('Your email and password hasn\'t been set. Please use the "tumblr-setup" command to set your email and password.');
}
};
var tumblrtypes = ['regular', 'photo', 'quote', 'link', 'conversation', 'video', 'audio'];
var noun_type_tumblrtype = new CmdUtils.NounType('tumblr_type', tumblrtypes);
var noun_type_url = {
/* TODO longterm, noun_type_url could suggest URLs you've visited before, by querying
* the awesomebar's data source
*/
_name : 'url',
suggest: function(fragment) {
var regexp = /(ftp|http|https):\/\/(\w+:{01}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
// Magic words 'page' or 'url' result in the URL of the current page
if (fragment == 'page' || fragment == 'url') {
var url = Application.activeWindow.activeTab.document.URL;
return [CmdUtils.makeSugg(url)];
}
// If it's a valid URL, suggest it back
if (regexp.test(fragment)) {
return [CmdUtils.makeSugg(fragment)];
} else if (regexp.test( 'http://' + fragment ) ) {
return [CmdUtils.makeSugg('http://' + fragment)];
}
return [];
}
};
var noun_type_title = {
_name : 'title',
suggest: function(fragment) {
if (fragment == 'page' || fragment == 'title')
return [CmdUtils.makeSugg(Application.activeWindow.activeTab.document.title)];
else
return [CmdUtils.makeSugg(fragment)];
}
};
CmdUtils.CreateCommand({
name: 'tumblr-regular',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add regular to your Tumblog',
help: 'Try it out: issue "tumblr-regular <i>hello world</i> title <i>my title</i>"',
license: 'MPL',
takes: {
tumbl: noun_arb_text
},
modifiers: {
title: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Regular post<br />' +
'Try it out: issue "tumblr-regular <i>hello world</i> title <i>my title</i>"';
},
execute: function(tumbl, mods) {
Tumblr.post({
type: 'regular',
title: mods.title.text || '',
body: tumbl.text
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-link',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add regular to your Tumblog',
help: 'Try it out: issue "tumblr-link <i>http://www.google.com</i> name <i>Google</i> description <i>Search Here!</i>"',
license: 'MPL',
takes: {
tumbl: noun_type_url
},
modifiers: {
name: noun_type_title,
description: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Link post<br />' +
'Try it out: issue "tumblr-link <i>http://www.google.com</i> name <i>Google</i> description <i>Search Here!</i>"';
},
execute: function(tumbl, mods) {
Tumblr.post({
type: 'link',
url: tumbl.text,
name: mods.name.text || '',
description: mods.description.text || ''
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-quote',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add regular to your Tumblog',
help: 'Try it out: issue "tumblr-quote <i>this is a quote</i> title <i>johan</i>"',
license: 'MPL',
takes: {
tumbl: noun_arb_text
},
modifiers: {
source: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Quote post<br />' +
'Try it out: issue "tumblr-quote <i>"this is a quote"</i> source <i>johan</i>"';
},
execute: function(tumbl, mods) {
Tumblr.post({
type: 'quote',
quote: tumbl.text,
source: mods.source.text || ''
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-conversation',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add regular to your Tumblog',
help: 'Try it out: issue "tumblr-quote <i>this is a quote</i> title <i>johan</i>"',
license: 'MPL',
takes: {
tumbl: noun_arb_text
},
modifiers: {
title: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Conversation post<br />' +
'Try it out: issue "tumblr-quote <i>"this is a quote"</i> source <i>johan</i>"';
},
execute: function(tumbl, mods) {
Tumblr.post({
type: 'conversation',
conversation: tumbl.text,
title: mods.title.text || ''
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-photo',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add photo to your Tumblog',
help: 'Try it out: issue "tumblr-photo <i>photo url</i> caption <i>my caption</i> click-through-url <i>http://www.flickr.com</i>"',
license: 'MPL',
takes: {
tumbl: noun_arb_text
},
modifiers: {
caption: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Photo post<br />' +
'Try it out: issue "tumblr-photo <i>photo url</i> caption <i>my caption</i> click-through-url <i>http://www.flickr.com</i>"';
},
execute: function(tumbl, mods) {
Tumblr.post({
type: 'photo',
source: tumbl.text,
caption: mods.caption.text || ''
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-video',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Add video to your Tumblog',
help: 'Try it out: issue "tumblr-video <i>video url</i> caption <i>my caption</i>"<br /> or if on a YouTube or Vimeo page, just issue: "tumblr-video"',
license: 'MPL',
takes: {
tumbl: noun_type_url
},
modifiers: {
caption: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML =
'Will create a new Tumblr Video post<br />' +
'Try it out: issue "tumblr-video <i>video url</i> caption <i>my caption</i>"<br /> or if on a YouTube or Vimeo page, issue: "tumblr-video"';
},
execute: function(tumbl, mods) {
var doc = Application.activeWindow.activeTab.document;
var myTumbl = tumbl.text;
var myCaption = mods.caption.text || '';
if (myTumbl == '' || myTumbl == undefined) myTumbl = doc.location.href;
if (myCaption == '' || myTumbl == undefined) myCaption = '';
Tumblr.post({
type: 'video',
embed: myTumbl,
caption: myCaption
});
}
});
CmdUtils.CreateCommand({
name: 'tumblr-setup',
homepage: 'http://www.tumblr.com',
author: {
name: 'Chuck Masucci',
homepage: 'http://www.pokenewyork.com/'
},
description: 'Set your Tumblr email, password and group. For use with the "tumblr" command',
help: 'Try it out: issue "tumblr-setup <i>username</i> pass <i>password</i> group <i>group name</i>"',
license: 'MPL',
takes: {
username: noun_arb_text
},
modifiers: {
pass: noun_arb_text,
group: noun_arb_text
},
preview: function(pblock, tumbl) {
pblock.innerHTML = 'Will set your Tumblr email, password and group.';
},
execute: function(data, mods) {
var username = data.text;
var password = mods.pass.text;
if (username.length < 1) {
displayMessage('This command requires a Tumblr email.');
} else if (password.length < 1) {
displayMessage('This command requires a Tumblr password.');
} else {
Tumblr.addUsername(username);
Tumblr.addPassword(password);
Tumblr.addGroup(mods.group.text || '');
if(Tumblr.everythingIsOK()) displayMessage('Your settings have been saved.');
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment