aboutsummaryrefslogtreecommitdiffstats
path: root/blinkelement.js
blob: 81b379eddc54fd3fbd871143e781fa9d6f1efe5f (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
/*
 * ==VimperatorPlugin==
 * @name            blinkelement.js
 * @description     blink specified elements.
 * @description-ja  指定した要素を点滅させる。
 * @author          janus_wel <janus_wel@fb3.so-net.ne.jp>
 * @version         0.31
 * @minversion      2.0pre 2008/10/16
 * ==/VimperatorPlugin==
 *
 * LICENSE
 *  New BSD License
 *
 * CONSTRAINT
 *  need highlight.js
 *
 * USAGE
 *  :blink {element[s] object}
 *  :bl
 *      -> blink specified element[s].
 *
 *  :noblink
 *  :nobl
 *      -> clear blink all elements.
 *
 * SETTING
 *  blink_element_interval: interval time. default is 800 msec.
 *  blink_element_color:    color for blink. default is red.
 *  blink_element_opacity:  opacity value. defualt is 0.5 .
 *
 * EXAMPLE
 *  let blink_element_interval='500'
 *  let blink_element_color='green'
 *  let blink_element_opacity='0.7'
 *
 *  :bl content.document.getElementsByTagName('A');
 *  :bl buffer.evaluateXPath('//a');
 *  :nobl
 * */

// use setTimeout to synchronize ( wait to process highlight.js )
// "liberator.modules.plugins.highlighterFactory" is build by highlight.js .
// it is the factory that build highlight object.
setTimeout( function () {

if (!plugins.highlighterFactory) {
    liberator.log('blinkelement.js needs highlight.js', 0);
    return;
}

// default settings
const defaultColor    = 'red';
const defaultOpacity  = 0.5;
const defaultInterval = 800;

let highlighter = liberator.modules.plugins.highlighterFactory({
    color:    liberator.globalVariables.blink_element_color    || defaultColor,
    opacity:  liberator.globalVariables.blink_element_opacity  || defaultOpacity,
    interval: liberator.globalVariables.blink_element_interval || defaultInterval,
});

// register commands
commands.addUserCommand(
    ['blink', 'bl'],
    'blink',
    function (args) {
        let arg = args.string;
        let element = liberator.eval(arg);

        if (!element) {
            liberator.echoerr('specify element[s]');
            return;
        }

        liberator.log(highlighter, 0);

        // reflect settings ( follow dynamic change of settings )
        highlighter.set({
            color:    liberator.globalVariables.blink_element_color    || defaultColor,
            opacity:  liberator.globalVariables.blink_element_opacity  || defaultOpacity,
            interval: liberator.globalVariables.blink_element_interval || defaultInterval,
        });

        // for getElement[s]By...
        if (element instanceof HTMLCollection) {
            for (let [, e] in Iterator(element)) highlighter.highlight(e);
        }
        // for evaluate
        else if (element instanceof XPathResult) {
            for (let e in element) highlighter.highlight(e);
        }
        // single element
        else if (element) {
            highlighter.highlight(element);
        }
        else {
            liberator.echoerr('specify element[s]');
        }
    },
    {
        completer: function (filter) completion.javascript(filter),
    }
);

commands.addUserCommand(
    ['noblink', 'nobl'],
    'no blink',
    function () highlighter.unhighlightAll(),
    {}
);

}, 0); // setTimeout

// vim: set sw=4 ts=4 et;
n>function(){ // scrape from div element that inserted by NicoNicoPlaylist liberator.modules.commands.addUserCommand( ['nnpgetlist'], 'get NicoNicoPlaylist', function (args) { // check existence of NicoNicoPlaylist let playlistNode = $f('//div[starts-with(@id, "playlistcontroller_")]'); if(!playlistNode) { liberator.echoerr('NicoNicoPlaylist is not found.'); return; } // check existence of items in NicoNicoPlaylist let nodes = util.evaluateXPath( 'id("' + playlistNode.id + '")/div[contains(concat(" ", @class, " "), " playlist-list-outer ")]/ul/li/a' ); let nodesLength = nodes.snapshotLength; if(!nodesLength) { liberator.echoerr('no items in NicoNicoPlaylist.'); return; } // get number of displayed items let numofList = liberator.globalVariables.nnp_coop_numoflist || 10; let arg = args[0]; if (arg && /^\d+$/.test(arg)) numofList = arg; // generate data let items = [], length = 0; for(let node in nodes) { if (length >= numofList) break; // get video id let id = node.href.match(/\d+$/); // evaluate variables and push to list items.push({ index: ++length, thumbnailURL: thumbnailURL(id), title: node.textContent, url: node.href, }); } // evaluate variables let xml = <> {style()} {table({ numofDisplay: (nodesLength < numofList) ? nodesLength : numofList, numofTotal: nodesLength, playTitle: getPlayTitle(), statuses: getStatusText(playlistNode.id), items: items, })} </> liberator.echo(xml, liberator.modules.commandline.FORCE_MULTILINE); }, {} ); // 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'], [['nnprandom'], 'toggle random mode of NicoNicoPlaylist', 'GMNNPRandom'], [['nnploop'], 'toggle loop mode of NicoNicoPlaylist', 'GMNNPLoop'], [['nnpfullscreen'], 'toggle fullscreen mode of NicoNicoPlaylist', 'GMNNPFullScreen'], ].forEach( function ([command, description, eventname]){ liberator.modules.commands.addUserCommand( command, description, function (arg) { let r = document.createEvent('CommandEvent'); r.initCommandEvent(eventname, true, true, arg[0]); window.content.dispatchEvent(r); }, {} ); }); // stuff functions --- // return first node function $f(query, node) { node = node || window.content.document; let result = (node.ownerDocument || node).evaluate( query, node, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ); return result.singleNodeValue ? result.singleNodeValue : null; } function getPlayTitle() { let titleNode = $f('//h1') || $f('//title'); return titleNode.textContent; } function getStatusText(idPrefix) { let statuses = []; [ ['//input[starts-with(@id, "playlist") and contains(@id, "-checkbox-random")]', 'R'], ['//input[starts-with(@id, "playlist") and contains(@id, "-checkbox-loop")]', 'L'], ['//input[starts-with(@id, "playlist") and contains(@id, "-checkbox-full")]', 'F'], ].forEach(function ([query, text]) { if ($f(query).checked) statuses.push(text); }); return (statuses.length) ? ' ' + statuses.join('') : ''; } // thumbnail URL // refer: http://d.hatena.ne.jp/ZIGOROu/20081014/1223991205 function thumbnailURL(videoId) { return [ 'http://tn-skr', (videoId % 2 + 1), '.smilevideo.jp/smile?i=', videoId ].join(''); } // E4X hell --- // style function style(css) { return <style type="text/css">{[ '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; }', ].join('')}</style> } // table function table(data) { return <table class="nnp_coop"> {caption(data)} {thead()} <tbody>{liberator.modules.template.map(data.items, item)}</tbody> </table> } // table caption function caption(data) { return <caption>now playing: {data.playTitle} (display {data.numofDisplay} / {data.numofTotal}{data.statuses})</caption> } // table head function thead() { return <thead> <tr> <td> </td> <td>thumbnail</td> <td>title</td> <td>url</td> </tr> </thead> } // item function item(datum) { return <tr> <td class="index">{datum.index}:</td> <td class="thumbnail"><img src={datum.thumbnailURL} width="33" height="25" /></td> <td>{datum.title}</td> <td>{datum.url}</td> </tr> } })(); // vim:sw=4 ts=4 et: