Skip to content

Instantly share code, notes, and snippets.

@modeverv
Created January 7, 2010 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save modeverv/271174 to your computer and use it in GitHub Desktop.
Save modeverv/271174 to your computer and use it in GitHub Desktop.
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 say(inp){
displayMessage(inp, this);
return true;
}
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;
}
}
}];
return CmdUtils.CreateCommand(o);
}
const posTmpl = <div>
<h4><img src={this.icon}/> Post in this way:</h4>
<![CDATA[${prml}]]>
{tumblrLogo}
</div>;
tumblrCommand({
names: ["post-to-tumblr-m", "mptm"],
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://http://www.youtube.com/">YouTube</a></li>
<li><a href="http://www.nicovideo.jp/">ニコニコ動画</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);
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(/http\:\/\/\w*\.nicovideo\.jp\/watch\/sm.*/(url)) /*nicovideo*/
(function(prm){/*this block is copy of http://d.hatena.ne.jp/takeken/20091113/1258104204 */
var _caption = cap ? ['<br/>',cap,'<br/>'].join("") : '<br/>';
var _params = url.match(/.*(sm[0-9]{7})/)[1];
var _ahref = ['<a href="http://www.nicovideo.jp/watch/',
     _params, '">',ttl,'</a><br/>'].join("");
var _vtag = ['<script type="text/javascript" ',
' src="http://ext.nicovideo.jp/thumb_watch/' , _params ,'"></script><noscript>',_ahref,'</noscript>',_caption].join("");
return prm.type = 'quote',prm.quote = _vtag + CmdUtils.getSelection(),prm.source = _ahref;
})(prm);
else if(s = CmdUtils.getSelection())
prm.type = 'quote',
prm.quote = CmdUtils.getSelection(),
prm.source = <><a href={url}>{ttl}</a>{cap||''}</>;
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: "satyr", homepage: "http://d.hatena.ne.jp/murky-satyr"},
{name: "powchin", homepage: 'http://friendfeed.com/powchin'}],
});
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 say('not posted.');
prm.conversation = CmdUtils.renderTemplate(
chatPosTmpl,
{ss: stats} );
$.post(this._config.api, prm);
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], {
 authors: [
   {name: 'powchin', homepage: 'http://friendfeed.com/powchin'},
   {name: 'modeverv',homepage: 'http://d.hatena.ne.jp/cast_everything/'}
],
licence: 'MIT'
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment