public
Last active

ubiquity tumblr commands.

  • Download Gist
ubiquity.tumblrCommand.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
const tumblrLogo = <div><a href='http://www.tumblr.com/'>
<img src='http://assets.tumblr.com/images/logo.png?alpha&amp;5'
style='border:none;'/>
</a></div>;
function accounts(url){
var token = (Cc['@mozilla.org/security/pk11tokendb;1']
.getService(Ci.nsIPK11TokenDB)
.getInternalKeyToken());
if(token.needsLogin() && !token.isLoggedIn()) return [];
var logins = (Cc['@mozilla.org/login-manager;1']
.getService(Ci.nsILoginManager)
.findLogins({}, url, '', ''));
return logins;
}
function tumblrCommand(o){
o._config = {
api: 'http://www.tumblr.com/api/write',
host: 'http://www.tumblr.com',
};
o._tagSplit = function(txt){
var [t, c] = /^\s*(?:[^;\s][^;]*;)*(?=\s*([\s\S]*)\s*$)/(txt),
tags = t.split(';').slice(0, -1);
return {cap: <p>{c}</p>, tag: tags};
};
o._essential = function(tags, user, pass){
var prm = {email: user, password: pass};
prm.tags = tags;
prm.generator = 'ubiquity - ptm';
return prm;
};
o.arguments = [{
role: "object",
nountype: noun_arb_text,
label: "[tag0;tag1;...; ]caption"
}, {
role: "source",
nountype: {
label: 'tumblr id',
default: (function(us){
var ss = [{text: '', summary: '', pass: ''}];
if(us.length)
ss = [ {text: x.username, summary: x.username, pass: x.password}
for each(x in us) ];
return function nt_tumblr_id_default() ss;
})(accounts('http://www.tumblr.com')),
suggest: function nt_tumblr_id_suggest(t, h){
var ss = [{text: '', summary: '', pass: ''}],
us = accounts('http://www.tumblr.com');
if(us.length)
ss = [ {text: x.username, summary: x.username, pass: x.password}
for each(x in us) if (x.username.match(t)) ];
return ss;
}
}
}];
o.say = function(inp){
displayMessage(inp, this);
return true;
};
return CmdUtils.CreateCommand(o);
}
 
const posTmpl = <div>
<h4><img src={this.icon}/> Will be posted in this way:</h4>
<![CDATA[${prml}]]>
{tumblrLogo}
</div>;
 
tumblrCommand({
names: ["post-to-tumblr", "ptm"],
description: "Post to Tumblr.",
help: '' + <>Supported Services:<ul style="list-style-image:none">
<li><a href="http://reader.google.com/">Google Reader</a></li>
<li><a href="http://reader.livedoor.com/">livedoor Reader</a></li>
<li><a href="http://www.nicovideo.jp/">NICO NICO DOUGA</a></li>
</ul></>,
icon: 'http://assets.tumblr.com/images/favicon.gif?2',
preview: function(pb, {object: {text}, source}){
var {cap, tag} = this._tagSplit(text.length > 0 ? text : "");
var prm = this._determine(
CmdUtils.getDocument(),
this._essential(tag, source.text, source.pass),
cap
);
var prml = [x+": "+prm[x]
for(x in prm) if("password" != x && "email" != x)];
pb.innerHTML = CmdUtils.renderTemplate(
posTmpl.toString(),
{prml:prml.join("<br>")}
);
},
execute: function({object: {text}, source}){
var {cap, tag} = this._tagSplit(text.length > 0 ? text : "");
var prm = this._determine(
CmdUtils.getDocument(),
this._essential(tag, source.text, source.pass),
cap
);
delete prm["prev"];
$.post(this._config.api, prm);
this.say(prm.type+" posted...");
},
_determine: function(doc, prm, cap){
var {url, ttl} = this._supported(doc);
if(pu = $(CmdUtils.getHtmlSelection()).find('img').attr('src')
|| $(CmdUtils.getHtmlSelection()).attr('src'))
prm.type = 'photo',
prm.caption = <><a href={url}>{ttl}</a>{cap||''}</>,
prm.source = new RegExp('^'+doc.location.protocol).test(pu)
? pu : doc.location.protocol+'//'+doc.location.hostname+'/'+pu,
prm.prev = <p><img src={prm.source}/></p>;
else if(CmdUtils.getHtmlSelection().length)
prm.type = 'quote',
prm.quote = CmdUtils.getHtmlSelection(),
prm.source = <><a href={url}>{ttl}</a>{cap||''}</>;
else if(/http\:\/\/\w*\.nicovideo\.jp\/watch\/(sm[0-9]+)/(url))
prm.type = 'video',
prm.caption = <><a href={url}>{ttl}</a>{cap||''}</>,
prm.embed = '<script type="text/javascript"' +
'src="http://ext.nicovideo.jp/thumb_watch/' +
RegExp.$1 + '"></script>';
else if(/http\:\/\/\w*\.youtube\.com\/watch.*/(url))
prm.type = 'video',
prm.caption = <><a href={url}>{ttl}</a>{cap||''}</>,
prm.embed = url;
else
prm.type = 'link',
prm.description = cap||'',
prm.name = ttl,
prm.url = url;
return prm;
},
_supported: function({URL, title}){
var win = CmdUtils.getWindow();
return /^https?:\/\/www\.google.+?\/reader\/view/.test(URL)
? {url: win.wrappedJSObject.getPermalink().url,
ttl: win.wrappedJSObject.getPermalink().title}
: /^https?:\/\/reader\.livedoor\.com\/reader/.test(URL)
? {url: win.wrappedJSObject.get_active_item(1).link,
ttl: win.wrappedJSObject.get_active_item(1).title}
: {url: URL, ttl: title};
},
contributors: [
{name: 'modeverv', homepage: 'http://d.hatena.ne.jp/cast_everything/'},
{name: 'satyr', homepage: 'http://d.hatena.ne.jp/murky-satyr'},
],
});
 
const chaTmpl = <><![CDATA[
<div>
<h1>Welcome to the chaTTer.</h1>
<h2>your conversation post will be...</h2>
<ul>
{if ss.length < 1}
nothing to post.
{/if}
{for s in ss}
<li>${s.user.screen_name}:&nbsp;${s.text}&nbsp;[${s.link}]</li>
{/for}
</ul>
</div>
]]></>.toString(),
chattStyle = <><![CDATA[
<style>
h1{
font: bold 150% consolas;
color: #fff;
border-bottom: solid 1px #888;
letter-spacing: -2px;
overflow:hidden;
margin: 2px 2px 8px 2px;
padding: 2px;
}
h2{
font: italic bold 130% consolas;
color: #ccc;
line-height: 80%;
letter-spacing: -2px;
overflow:hidden;
margin: 2px 2px 6px 2px;
padding: 2px;
}
ul{margin: 2px; padding: 2px}
li{
list-style-type: none;
padding: 0px 0px 0px 8px;
font: 100% meiryo;
letter-spacing: -1px;
color: #ccc;
}
</style>
]]></>.toString(),
chatPosTmpl = <><![CDATA[
{for s in ss}
${s.user.screen_name}: ${s.text} [${s.link}]
{/for}
]]></>.toString(),
twitterLogo = <div><a href='http://twitter.com/'>
<img src='http://a0.twimg.com/a/1262230172/images/twitter_logo_header.png'
style='border:none;'/>
</a></div>;
 
tumblrCommand({
names: ['chaTTer', 'ct'],
icon: 'chrome://ubiquity/skin/icons/favicon.ico',
description:
'twitter timeline >> tumblr conversation post.',
previewDelay: 123,
preview: function(pbl, {object: {text}, source}){
pbl.innerHTML = chattStyle +
CmdUtils.renderTemplate(chaTmpl, {ss: this._stats()}) +
tumblrLogo + twitterLogo;
},
execute: function({object: {text}, source}) {
var {cap, tag} = this._tagSplit(text.length > 0 ? text : "");
var prm = this._essential(tag, source.text, source.pass);
prm.type = 'conversation';
prm.title = cap;
var stats = this._stats();
if(stats.length < 1)
return this.say('not posted.');
prm.conversation = CmdUtils.renderTemplate(
chatPosTmpl,
{ss: stats} );
$.post(this._config.api, prm);
this.say("done!");
},
_stats: function() {
return [
this._toStats(tab) for each(tab in Application.activeWindow.tabs)
if(tab.document.location.href
.match(/twitter.com\/(\w+)?\/status\/(\d+)/))
].sort(function(a, b){return a.id - b.id});
},
_toStats: function(tab) {
return {
id: RegExp.$2,
user: {screen_name: RegExp.$1},
text: $('.entry-content', tab.document).text(),
link: tab.document.location.href
};
},
});
 
$.extend(commands[0], {
author: 'powchin',
homepage: 'http://friendfeed.com/powchin',
licence: 'MIT'
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.