gist: 10172 Download_button fork
public
Description:
Laterloop commands for Ubiquity. Will eventually move to http://blog.mcfearsome.com/verbs/
Public Clone URL: git://gist.github.com/10172.git
laterloop.ubiq.js
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
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"];
 

Owner

mcfearsome

Revisions