-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinboard.js
91 lines (79 loc) · 3.1 KB
/
pinboard.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
(function() {
function buildURL(url, queries) {
// see https://blog.pinboard.in/2012/07/api_authentication_tokens/
var token = options.get('pinboardtoken').get();
if (token != null && token != '') {
queries.push(['auth_token', token]);
}
var result = [url];
for (var i = 0; i < queries.length; i++)
if (queries[i][1])
result.push('&', queries[i][0], '=', encodeURIComponent(queries[i][1]));
return result.join("");
}
function httpPost(url, callback) {
try {
let xmlhttp = new XMLHttpRequest();
if (callback)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4)
callback(xmlhttp);
}
xmlhttp.open('POST', url, !!callback);
xmlhttp.send(null);
return xmlhttp;
} catch (e) {
liberator.log('Error opening ' + url + ': ' + e, 1);
}
}
if (options.get('pinboardtoken') == null) {
options.add(['pinboardtoken'], 'Pinboard.in token (username:token)', 'string', '');
}
commands.addUserCommand(['pin[board]'], 'Bookmark page at pinboard.in', function(args) {
var url = buffer.URL;
var title = args['-t'] || buffer.title || url;
var description = args['-d'] || String(window.content.getSelection());
var shared = args['-s'] ? 'yes' : 'no';
var toread = args['-r'] ? 'no' : 'yes';
httpPost(buildURL('https://api.pinboard.in/v1/posts/add?',
[['url', url],
['description', title],
['extended', description],
['tags', args.join(" ")],
['shared', shared],
['toread', toread]]), function(xhr) {
var result = xhr.status == 200 ?
xhr.responseXML.documentElement.getAttribute('code') :
'failed with status ' + xhr.status;
liberator.echo('Bookmarking ' + url + ' at pinboard.in ' + result);
});
}, {
argCount: '*',
options: [[['-t', 'title'], commands.OPTION_STRING, null, function() [[buffer.title]]],
[['-d', 'description'], commands.OPTION_STRING, null, null],
[['-s', 'shared'], commands.OPTION_NOARG, null, null],
[['-r', 'markasread'], commands.OPTION_NOARG, null, null]],
completer: function(context) {
if (context.result) {
context.completions = context.result;
return;
}
context.title = ['Tags', 'Type'];
context.incomplete = true;
var xhr = util.httpGet(buildURL('https://api.pinboard.in/v1/posts/suggest?',
[['url', buffer.URL]]), function(xhr) {
context.incomplete = false;
if (xhr.status != 200) {
context.completions = context.result = [];
return;
}
var result = [];
var tags = xhr.responseXML.documentElement.getElementsByTagName('*');
for (var i = 0; i < tags.length; i++)
result.push([tags[i].textContent, tags[i].localName]);
context.completions = context.result = result;
});
context.cancel = function() xhr.abort();
}
}, true);
})();