aboutsummaryrefslogtreecommitdiffstats
path: root/nnp_cooperation.js
blob: e615a5beb179962b3d7976e35e67c24485cd7860 (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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * ==VimperatorPlugin==
 * @name            niconicoplaylist_cooperation.js
 * @description     this script give you keyboard opration for NicoNicoPlaylist.
 * @description-ja  NicoNicoPlaylist ���L�[�{�[�h�ő����ł����悤�ɂ����B
 * @author          janus_wel <janus_wel@fb3.so-net.ne.jp>
 * @version         0.30
 * @minversion      1.2
 * ==VimperatorPlugin==
 *
 * CONSTRAINT
 *   need NicoNicoPlaylist version 1.11 or above
 *
 * LICENSE
 *   New BSD License
 *
 * USAGE
 *   :nnppushallvideos
 *     ���݂̃y�[�W���̂��ׂĂ̓������Đ����X�g�ɑ����B
 *     �����L���O���}�C���X�g�̂ق��A�����y�[�W�ł̓I�X�X�����悪�lj��������B
 *   :nnppushthisvideo
 *     ���݌��Ă��铮�����Đ����X�g�ɑ����B
 *   :nnpplaynext [next]
 *     �Đ����X�g�̎��̓������Đ������B
 *   :nnpremove [index]
 *     index �Ԗڂ̓������Đ����X�g�������菜���B index �� 0 ���琔�����B
 *     �w�肵�Ȃ��ꍇ�͈��ԏオ���菜�������B
 *   :nnpclear
 *     �Đ����X�g�����ׂăN���A�����B
 *   :nnpgetlist [numof]
 *     �Đ����X�g�̏ォ�� numof �‚��\�������B�w�肵�Ȃ��ꍇ�� g:nnp_coop_numoflist ���g�������B
 *
 * VARIABLES
 *   g:nnp_coop_numoflist
 *     :NNPGetList �ŕ\�����郊�X�g�̌����w�肷���B�f�t�H���g�� 10 �B
 *
 * HISTORY
 *   2008/07/11 ver. 0.1    initial written.
 *   2008/07/15 ver. 0.2    refactoring.
 *   2008/09/25 ver. 0.3    change XPath expression.
 *
 * */
/*
�ȉ��̃R�[�h�� _vimperatorrc �ɓ\���t�����ƍK���ɂȂ��邩���B
�R�}���h ( [',nn'] �� [',nr'] �̕��� ) �͓K�X�ς��ĂˁB

javascript <<EOM

// [N],nn
// N �Ԗڂ̓������Đ������B
// �w���Ȃ��̏ꍇ���̓��悪�Đ��������B
liberator.mappings.addUserMap(
    [liberator.modes.NORMAL],
    [',nn'],
    'play next item in NicoNicoPlaylist',
    function(count) {
        if(count === -1) count = 1;
        liberator.execute(':nnpplaynext ' + count);
    },
    { flags: liberator.Mappings.flags.COUNT }
);

// [N],nr
// �ォ�� N �‚̓������폜�����B
// �w���Ȃ��̏ꍇ���ԏ��̓��悪�폜�������B
liberator.mappings.addUserMap(
    [liberator.modes.NORMAL],
    [',nr'],
    'remove item in NicoNicoPlaylist',
    function(count) {
        if(count === -1) count = 1;
        for(var i=0 ; i<count ; ++i) liberator.execute(':nnpremove');
        liberator.execute(':nnpgetlist');
    },
    { flags: liberator.Mappings.flags.COUNT }
);

EOM

*/

(function(){

// thumbnail URL
const thumbnailURL = 'http://tn-skr1.smilevideo.jp/smile?i=';

// style
const styles = [
    '<style>',
        'table.nnp_coop .index     { text-align:right; width:2em; }',
        'table.nnp_coop .thumbnail { text-align:center; }',
        'table.nnp_coop caption    { color:green; }',
        'table.nnp_coop thead      { text-align:center; }',
    '</style>',
].join('');

// table
const tableTemplate = [
    '<table class="nnp_coop">',
        '$CAPTION',
        '$THEAD',
        '<tbody>$ITEMS</tbody>',
    '</table>',
].join('');

// table caption
const captionTemplate = '<caption>$NUMOFDISPLAY / $NUMOFTOTAL items from NicoNicoPlaylist</caption>';

// table head
const thead = [
    '<thead>',
        '<tr>',
            '<td>&nbsp;</td>',
            '<td>thumbnail</td>',
            '<td>title</td>',
            '<td>url</td>',
        '</tr>',
    '</thead>',
].join('');

// item
const itemHTML = [
    '<tr>',
        '<td class="index">$INDEX:</td>',
        '<td class="thumbnail"><image src="$THUMBNAILURL$ID" width="33" height="25" /></td>',
        '<td>$TITLE</td>',
        '<td>$URL</td>',
    '</tr>',
].join('');


// scrape from div element that inserted by NicoNicoPlaylist
liberator.commands.addUserCommand(['nnpgetlist'], 'get NicoNicoPlaylist',
    function(arg) {
        // check existence of NicoNicoPlaylist
        var playlist = $f('//div[contains(@id, "playlistcontroller_")]');
        if(!playlist) {
            liberator.echoerr('NicoNicoPlaylist is not found.');
            return;
        }

        // check existence of items in NicoNicoPlaylist
        var nodes = $s('./div[contains(concat(" ", @class, " "), " playlist-list-outer ")]/ul/li/a', playlist);
        var nodesLength = nodes.length
        if(nodesLength === 0) {
            liberator.echoerr('no items in NicoNicoPlaylist.');
            return;
        }

        // get number of displayed items
        var numofList = arg.match(/^\d+$/)
            ? arg
            : (liberator.globalVariables.nnp_coop_numoflist || 10);

        // struct display string
        // generate data
        var items = new Array;
        for(var i=0 ; i<nodesLength && i<numofList ; ++i ) {
            // get video id
            var id = nodes[i].href.match(/\d+$/);
            // evaluate variables and push to list
            items.push(
                itemHTML.replace(/\$INDEX/g,        i + 1)
                        .replace(/\$THUMBNAILURL/g, thumbnailURL)
                        .replace(/\$ID/g,           id)
                        .replace(/\$TITLE/g,        nodes[i].textContent)
                        .replace(/\$URL/g,          nodes[i].href)
            );
        }

        // evaluate variables
        var caption = captionTemplate
            .replace(/\$NUMOFDISPLAY/g, (nodesLength < numofList) ? nodesLength : numofList)
            .replace(/\$NUMOFTOTAL/g,   nodesLength);

        // final processing
        var str = styles + tableTemplate.replace(/\$CAPTION/g, caption)
                                        .replace(/\$THEAD/g,   thead)
                                        .replace(/\$ITEMS/g,   items.join(''));

        liberator.echo(str, liberator.commandline.FORCE_MULTILINE);
    },{}
);

// stuff functions
// return first node
function $f(query, node) {
    node = node || window.content.document;
    var result = (node.ownerDocument || node).evaluate(
        query,
        node,
        null,
        XPathResult.FIRST_ORDERED_NODE_TYPE,
        null
    );
    return result.singleNodeValue ? result.singleNodeValue : null;
}

// return snapshot nodes list
function $s(query, node) {
    node = node || window.content.document;
    var result = (node.ownerDocument || node).evaluate(
        query,
        node,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );
    var nodes = [];
    for(var i=0 ; i<result.snapshotLength ; ++i) nodes.push(result.snapshotItem(i));
    return nodes;
}

// define other commands
// only send CommandEvent to NicoNicoPlaylist script
[
    [['nnppushallvideos'], "push all videos to NicoNicoPlaylist",    'GMNNPPushAllVideos'],
    [['nnppushthisvideo'], "push current video to NicoNicoPlaylist", 'GMNNPPushThisVideo'],
    [['nnpplaynext'],      "play next in NicoNicoPlaylist",          'GMNNPPlayNext'],
    [['nnpremove'],        "remove item in NicoNicoPlaylist",        'GMNNPRemove'],
    [['nnpclear'],         "clear all items in NicoNicoPlaylist",    'GMNNPClear'],
].forEach(
    function ([command, description, eventname]){
        liberator.commands.addUserCommand(command, description,
            function (arg) {
                var r = document.createEvent("CommandEvent");
                r.initCommandEvent(eventname, true, true, arg);
                window.content.dispatchEvent(r);
            },{}
        );
    }
);

})();

// vim:sw=4 ts=4 et: