Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laterloop commands for Ubiquity. Will eventually move to http://blog.mcfearsome.com/verbs/
if(typeof(McFearsome) == 'undefined') {
McFearsome = {};
}
jQuery.extend(McFearsome, {
laterloop: {
save_key: function( key ) {
Application.prefs.setValue('ubiquity.mcfearsome.laterloop.key', key);
},
get_key: function() {
return Application.prefs.getValue('ubiquity.mcfearsome.laterloop.key', false);
},
save_rss: function( rss ) {
Application.prefs.setValue('ubiquity.mcfearsome.laterloop.rss', rss);
},
get_rss: function() {
return Application.prefs.getValue('ubiquity.mcfearsome.laterloop.rss', false);
},
clear_prefs: function() {
try {
Application.prefs.get('ubiquity.mcfearsome.laterloop.key').reset();
Application.prefs.get('ubiquity.mcfearsome.laterloop.rss').reset();
} catch(e) {
// CmdUtils.log(e);
}
}
}
});
function pageLoad_laterloop() {
var doc = CmdUtils.getDocumentInsecure();
if(doc.location != 'http://www.laterloop.com/tools/' || McFearsome.laterloop.get_key()) return;
displayMessage("Setting your laterloop ubiquity command preferences.")
CmdUtils.loadJQuery(function(jQuery) {
try {
var bookmarklet = jQuery("a.bkml");
if(bookmarklet.length) {
var bookmarklet_src = bookmarklet.get(0).href;
var key_pattern = /d\._ll='([a-zA-Z0-9]*)';/;
var m = bookmarklet_src.match(key_pattern);
if(m) {
var key = m[1];
}
var r = jQuery.ajax({
type: "GET",
url: "http://www.laterloop.com",
async: false
});
if(r.status == 200) {
var h = r.responseText;
var pattern = /<link rel="alternate" type="application\/rss\+xml" title="[^"]*" href="([^"]*)" \/>/;
var m = h.match(pattern);
if(m) {
var rss = m[1];
}
}
if(key && rss) {
McFearsome.laterloop.save_key(key);
McFearsome.laterloop.save_rss(rss);
return true;
}
}
throw("epic fail");
} catch(e) {
displayMessage("There was a problem saving your laterloop key. Please reload the page to try again.");
// CmdUtils.log(e);
}
});
}
noun_type_item = new CmdUtils.NounType( "Page", ["1","2","3","4","5","6","7","8","9","10"]);
var noun_type_integer = {
_name: "number",
suggest: function(text, html) {
if(!text)
return [ CmdUtils.makeSugg("1", null, 1) ];
var num = parseInt(text);
if(isNaN(num)) {
return [];
}
return [ CmdUtils.makeSugg(num.toString(), null, num) ];
}
}
CmdUtils.CreateCommand({
name: "laterloop",
takes: {"page": noun_type_integer},
modifiers: {open: noun_type_item},
icon: "http://www.laterloop.com/favicon.ico",
description: "View your items from Laterloop.",
help: "Displays items from your items feed in lists of 10. More items can be displayed in the preview by entering a page number. An item can be opened by using the 'open' modifier.",
homepage: "http://gist.github.com/10172",
author: { name: "Jesse McPherson", email: "jesse@mcfearsome.com" },
preview: function( pblock, dIn, mod ) {
try{
if(!McFearsome.laterloop.get_rss()) {
pblock.innerHTML = "Login to Laterloop and visit <a href='http://www.laterloop.com/tools/'>http://www.laterloop.com/tools/</a> to setup this command.";
return false;
}
var page = 1;
var open = parseInt(mod.open.text);
if(dIn) {
page = parseInt(dIn.text);
if(isNaN(page)) page = 1;
}
var start = (page-1) * 10;
var end = start + 10;
if(open && (open > 10)) {
open = null;
}
var url = McFearsome.laterloop.get_rss();
var r = jQuery.ajax({ type: "GET",
url: url,
async: false
});
var xml = r.responseXML;
var items = xml.getElementsByTagName("item");
var items = jQuery.grep(items, function(n, i) {
return (start <= i && i < end);
});
// CmdUtils.log(open);
var item = items[open-1];
// CmdUtils.log(item);
CmdUtils.getDocumentInsecure().tmpItem = item;
var tmpl = "<ol style=\"margin: 0;\">{for item in items}{if item_index < 10}<li class=\"gresult\"><a {if (open+1) == item_index}class=\"hilited\"{/if} href=\"${item.getElementsByTagName('link')[0].textContent}\">${item.getElementsByTagName('title')[0].textContent}</a></li>{/if}{/for}</ol>";
var rendered = CmdUtils.renderTemplate(tmpl, {items: items, open: open-2});
pblock.innerHTML = rendered;
} catch(e) {
// CmdUtils.log(e);
}
},
execute: function( directInput, mod ) {
var item = CmdUtils.getDocumentInsecure().tmpItem;
var link = item.getElementsByTagName("link")[0].textContent;
displayMessage("Opening \"" + item.getElementsByTagName("title")[0].textContent + "\"");
Utils.openUrlInBrowser(link);
}
});
CmdUtils.CreateCommand({
name: "laterloop-this",
icon: "http://www.laterloop.com/favicon.ico",
description: "Save the current page for later.",
help: "Saves the current page to laterloop",
homepage: "http://gist.github.com/10172",
author: { name: "Jesse McPherson", email: "jesse@mcfearsome.com" },
preview: function( pblock ) {
if(!McFearsome.laterloop.get_key()) {
pblock.innerHTML = "Login to Laterloop and visit <a href='http://www.laterloop.com/tools/'>http://www.laterloop.com/tools/</a> to setup this command.";
return false;
}
var doc = CmdUtils.getDocumentInsecure();
pblock.innerHTML = CmdUtils.renderTemplate("Save <strong>\"${title}\"</strong> for Later.", {title: doc.title});
},
execute: function( directInput ) {
var key = McFearsome.laterloop.get_key();
if(!key) {
displayMessage("Login to Laterloop and visit http://www.laterloop.com/tools/ to setup this command.")
return false;
}
var doc = CmdUtils.getDocumentInsecure();
var t = doc.title;
doc._ll = key;
var tmpl = "<script type=\"text/javascript\" charset=\"UTF-8\" src=\"${ b }/js/ll.js\"></script>";
CmdUtils.injectHtml(CmdUtils.renderTemplate( tmpl, {b: 'http://static.laterloop.com'}));
s = '[Saving...] ';
doc.title = s + doc.title.replace(s, '');
displayMessage(t + " has been saved for later.");
}
});
CmdUtils.CreateCommand({
name: "laterloop-clear",
icon: "http://www.laterloop.com/favicon.ico",
description: "Clear your laterloop setup for ubiquity.",
help: "When first installed the laterloop ubiquity commands saves your key and rss feed location as preference values. This command will clear those preference values so that another user could be setup.",
homepage: "http://gist.github.com/10172",
author: { name: "Jesse McPherson", email: "jesse@mcfearsome.com" },
preview: "Clear your laterloop setup for ubiquity.",
execute: function( d ) {
McFearsome.laterloop.clear_prefs();
displayMessage("Your laterloop setup has been cleared.");
}
});
CmdUtils.__globalObject["cmd_ll"] = CmdUtils.__globalObject["cmd_laterloop"];
CmdUtils.__globalObject["cmd_ll-t"] = CmdUtils.__globalObject["cmd_laterloop-this"];
CmdUtils.__globalObject["cmd_ll-c"] = CmdUtils.__globalObject["cmd_laterloop-clear"];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.