-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwpdotcom.js
executable file
·67 lines (55 loc) · 1.5 KB
/
wpdotcom.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
#!/usr/bin/env node
var AlfredNode = require('alfred-workflow-nodejs');
var actionHandler = AlfredNode.actionHandler;
var workflow = AlfredNode.workflow;
var settings = AlfredNode.settings;
workflow.setName("alfred-wpdotcom-workflow");
var Item = AlfredNode.Item;
settings.getPassword("alfred", function(error, token){
main(token);
});
function main(token) {
if ( ! token ) {
var item = new Item({
title: 'Set your token with wpcomsetkey',
arg: '',
subtitle: '',
valid: false
});
workflow.addItem(item);
workflow.feedback();
return;
}
var wpcom = require( 'wpcom' )( token );
// Search user's sites
actionHandler.onAction("sites", function(query) {
var me = wpcom.me();
me.sites({ fields: 'name,URL' }, function(err, list) {
if (err) throw err;
// Iterate over sites
list.sites.forEach( function(site) {
// If site doesn't match query, move on.
var re = new RegExp(query, "i");
if ( ! re.test( site.name ) ) {
return;
}
// Strip protocol
url = site.URL.replace(/https?:\/\//, '');
// Create the item
var item1 = new Item({
title: site.name,
arg: url,
subtitle: url,
valid: true
});
workflow.addItem(item1);
});
// Send it
workflow.feedback();
});
});
actionHandler.onAction("setkey", function(password) {
settings.setPassword("alfred", password);
});
AlfredNode.run();
}