Skip to content

Instantly share code, notes, and snippets.

@zaknak
Created September 24, 2008 12:17
Show Gist options
  • Save zaknak/12539 to your computer and use it in GitHub Desktop.
Save zaknak/12539 to your computer and use it in GitHub Desktop.
ubiquity user command
var noun_type_opt = new CmdUtils.NounType('option', ['new','same']);
CmdUtils.CreateCommand({
name : 'metab',
icon : 'http://b.hatena.ne.jp/favicon.ico',
description : 'Get Hatena bookmarks',
homepage : 'http://d.hatena.ne.jp/zaknak/20080927/1222524088',
author : { name: 'zaknak' },
takes : { 'option' : noun_type_opt },
cache : {},
execute : function(argv){
if(this.cache.recentEntry){
if(argv.text == 'new'){
Application.activeWindow.open(Utils.url(this.cache.recentEntry));
}else{
Application.activeWindow.activeTab.document.location.href = this.cache.recentEntry;
}
}
},
preview : function(view){
var doc = Application.activeWindow.activeTab.document;
var cache = this.cache;
var showEntry = function(entry){
var elm = doc.createElement('p');
elm.innerHTML = entry == 'terminated' ? entry : '<a href="' + entry + '">' + entry; + '</a>';
view.appendChild(elm);
}
jQuery(view).empty();
(function(url){
var calee = arguments.callee;
var req = 'http://b.hatena.ne.jp/entry/json/' + url.replace(/#/g,'%23');
if(cache[req]){
showEntry(cache[req]);
if(cache[req] != 'terminated'){
calee(cache[req]);
}
}else{
jQuery.getJSON(req , function(json){
if(json && json.entry_url){
cache[req] = cache.recentEntry = json.entry_url;
Utils.setTimeout(function(){
calee(json.entry_url);
},500);
}else{
cache[req] = 'terminated';
}
showEntry(cache[req]);
});
}
})(doc.location.href);
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment