aboutsummaryrefslogtreecommitdiffstats
path: root/pukka.js
blob: eaa5339a0b76b2daa94a3e8448c6e8bcc3c64858 (plain)
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
/*
 * liberator plugin
 * Add `pukka' http://codesorcery.net/pukka/ command to Bookmark del.icio.us
 * For liberator 0.6pre
 * @author otsune (based on teramako)
 * @version 0.3
 *
 * Variable:
 *  g:pukka_normalizelink
 *      Specifies keys that use Pathtraq URL Normalizer
 *      usage: let g:pukka_normalizelink = true
 * Mappings:
 *  '[C-z]':
 * Commands:
 *  'pukka' or 'pu':
 *      Post bookmark to del.icio.us with Pukka
 *      usage: :pukka http://example.com/
 * Options:
 *  not implemented
 */

(function(){
    var useNormalizelink = liberator.globalVariables.pukka_normalizelink || true;
liberator.commands.addUserCommand(['pukka','pu'], 'Post to Pukka bookmark',
function(args){
    if (!liberator.buffer.title || !liberator.buffer.URL || liberator.buffer.URL=='about:blank'){
        return false;
    }
    var str = 'pukka:';
    var title = encodeURIComponent(liberator.buffer.title);
    var url = encodeURIComponent(liberator.buffer.URL);
    var extend = encodeURIComponent(window.content.getSelection().toString() || '');
    if (args){
        url = encodeURIComponent(args);
    }
    liberator.open(str + 'url=' + url + '&title=' + title + '&extended=' + extend);
},{
    completer: function(filter){
        var complist = [];

        complist.push([liberator.buffer.URL, 'Raw URL: ' + liberator.buffer.title]);

        if(useNormalizelink){
            complist.push([getNormalizedPermalink(liberator.buffer.URL), 'Normalized URL: ' + liberator.buffer.title]);
        }

        // detect rel="bookmark"
        var elem;
        var relb = liberator.buffer.evaluateXPath(
//          '//*[@rel="bookmark"]',
            '//*[contains(concat(" ", normalize-space(@rel), " "), " bookmark ")]', 
            null, null, true);
        while ((elem = relb.iterateNext()) !== null) {
            complist.push([elem.toString(), '@rel="bookmark" URL: ' + elem]);
        }

        return [0, complist];
    }
}
);

liberator.mappings.addUserMap([liberator.modes.NORMAL],
['<C-z>'], 'Post to Pukka',
function() {
    var urlarg = liberator.globalVariables.pukka_normalizelink ?
                 getNormalizedPermalink(liberator.buffer.URL) :
                 liberator.buffer.URL ;
    liberator.commandline.open(
        ':',
        'pukka ' + urlarg,
        liberator.modes.EX
    );
},{
}
);

// copied from trapezoid's direct_hb.js
function getNormalizedPermalink(url){
    var xhr = new XMLHttpRequest();
    xhr.open("GET","http://api.pathtraq.com/normalize_url?url=" + url,false);
    xhr.send(null);
    if(xhr.status != 200){
        liberator.echoerr("Pathtraq: FAILED to normalize URL!!");
        return url;
    }
    return xhr.responseText;
}
})();