aboutsummaryrefslogtreecommitdiffstats
path: root/nicovideo_comment.js
blob: 4157649e1496693cc8667543399a384316dc0df2 (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
/**
 * ==VimperatorPlugin==
 * @name           nicovideo_comment.js
 * @description    This plugin allows you to fill in the comment form for NICO NICO DOUGA.
 * @description-ja ニコニコ動画のコメント欄を入力
 * @minVersion     0.6pre
 * @author         otsune
 * @version        0.2
 * ==/VimperatorPlugin==
 *
 * Usage:
 * :nico {String}             -> Fill comment form
 * :nico! {String}            -> Fill mail form
 */
(function(){

liberator.commands.addUserCommand(['nico'],'Fill comment form in nicovideo',
    function(arg, special){
        var flvp = window.content.document.getElementById('flvplayer'); 
        var form = special ? 'MailInput.text' : 'ChatInput.text' ;
        flvp.wrappedJSObject.SetVariable(form, arg.toString() );
    },{
        bang: true,
        completer: function(filter, special){
            var templates = [];
            var commands = [
                'ue',
                'shita',
                'big',
                'medium',
                'small',
                'white',
                'red',
                'pink',
                'orange',
                'yellow',
                'green',
                'cyan',
                'blue',
                'purple',
                '184',
                'sage'
            ];
            var premiumcommands = [
                'invisible',
                'niconicowhite',
                'truered',
                'passionorange',
                'madyellow',
                'elementalgreen',
                'marineblue',
                'nobleviolet',
                'black'
            ];
            if (special){
                commands.forEach(function(command){
                    if (command[0].indexOf(filter.toLowerCase()) === 0){
                        templates.push(command);
                    }
                });
            }
            if (special && !(flvp.GetVariable('premiumNo')) ){
                premiumcommands.forEach(function(premiumcommand){
                    if (premiumcommand[0].indexOf(filter.toLowerCase()) === 0){
                        templates.push(premiumcommand);
                    }
                });
            }

            return [0, templates];
        }
    }
);

})();

// vim: set fdm=marker sw=4 ts=4 et:
n class="p">['goo[dictionary]'], url: 'http://dictionary.goo.ne.jp/search.php?MT=%s&kind=all&mode=0&IE=UTF-8', shortHelp: 'goo \u8F9E\u66F8', xpath: 'id("incontents")/*[@class="ch04" or @class="fs14" or contains(@class,"diclst")]', multi: true, dictionary: 'en-US', srcEncode: 'EUC-jp', urlEncode: 'UTF-8' },{ names: ['answers'], url: 'http://www.answers.com/%s', shortHelp: 'Answers.com(\u82F1\u82F1\u8F9E\u66F8)', xpath: 'id("firstDs")', dictionary: 'en-US' },{ names: ['wikipe[diaja]'], url: 'http://ja.wikipedia.org/wiki/%s', shortHelp: 'Wikipedia lite(ja)', xpath: 'id("bodyContent")/p[1]', dictionary: 'ja' },{ names: ['wikipe[diaen]'], url: 'http://en.wikipedia.org/wiki/%s', shortHelp: 'Wikipedia lite(en)', xpath: 'id("bodyContent")/p[1]', dictionary: 'en' }]; let (siteDef = liberator.globalVariables.lookupDictionary_site_definition) { if (siteDef) { if (siteDef instanceof String) siteDef = eval(siteDef); if (siteDef.forEach instanceof Function) siteDef.forEach(function (obj) { SITE_DEFINITION.push(obj); }); else SITE_DEFINITION.push(siteDef); } } // class definition function SpellChecker() { this.initialize.apply(this, arguments); } SpellChecker.prototype = { initialize: function () { const MYSPELL = "@mozilla.org/spellchecker/myspell;1"; const HUNSPELL = "@mozilla.org/spellchecker/hunspell;1"; const ENGINE = "@mozilla.org/spellchecker/engine;1"; var spellclass = MYSPELL; if (HUNSPELL in Components.classes) spellclass = HUNSPELL; if (ENGINE in Components.classes) spellclass = ENGINE; this.engine = Components.classes[spellclass] .createInstance(Components.interfaces.mozISpellCheckingEngine); }, /** * @return {Array} */ getDictionaryList: function () { var dictionaries = {}; this.engine.getDictionaryList(dictionaries, {}); return dictionaries.value; }, /** * @return {String} */ dictionary: function () { var dict; try { dict = this.engine.dictionary; } catch (e) {} return dict ? dict : null; }, /** * @param {String} dict */ setDictionary: function (dict) { var dictionaries = this.getDictionaryList(); for (let i=0, max=dictionaries.length ; i<max ; ++i) { if (dictionaries[i] === dict) { this.engine.dictionary = dict; return dict; } } return null; }, /** * @param {Boolean} isBeginningWith */ setBeginningWith: function (isBeginningWith) { this.isBeginningWith = isBeginningWith; }, /** * @param {String} spell * @return {Boolean} */ check: function (spell) { return this.engine.check(spell); }, /** * @param {String} spell * @return {Array} */ suggest: function (spell) { var suggestions = {}; this.engine.suggest(spell, suggestions, {}); suggestions = suggestions.value; if (this.isBeginningWith) { suggestions = suggestions.filter( function (cand) { return (cand.toLowerCase().indexOf(spell) === 0); }); } return suggestions; }, }; var spellChecker = buildSpellChecker(); SITE_DEFINITION.forEach(function (dictionary) { commands.addUserCommand( dictionary.names, dictionary.shortHelp, function (args) { var arg = args.string; var sel = (window.content.window.getSelection) ? window.content.window.getSelection().toString() : null; if (args.bang && sel) arg = sel; if (!arg) return; var url; if (dictionary.urlEncode) { let ttbu = Components.classes['@mozilla.org/intl/texttosuburi;1'] .getService( Components.interfaces.nsITextToSubURI); url = dictionary.url.replace(/%s/g, ttbu.ConvertAndEscape(dictionary.urlEncode, arg)); } else { url = dictionary.url.replace(/%s/g,encodeURIComponent(arg)); } //liberator.log('URL: ' +url); var result; getHTML(url, function (str) { var doc = createHTMLDocument(str); var result = getNodeFromXPath(dictionary.xpath, doc, dictionary.multi); if (!result) { liberator.echoerr('Nothing to show...'); } var xs = new XMLSerializer(); liberator.echo(new XMLList('<div style="white-space:normal;"><base href="' + util.escapeHTML(url) + '"/>' + xs.serializeToString( result ).replace(/<[^>]+>/g,function (all) all.toLowerCase() ) + '</div>'), true); }, dictionary.srcEncode ? dictionary.srcEncode : null); }, { completer: function (arg) { if (!spellChecker || !dictionary.dictionary || !spellChecker.setDictionary(dictionary.dictionary)) return [0, []]; var suggestions = spellChecker.suggest(arg); var candidates = []; for (let i=0, max=suggestions.length ; i<max ; ++i) { candidates.push([suggestions[i], 'suggest']); } if (!spellChecker.check(arg)) { candidates.unshift(['', 'not exist']); } return [0, candidates]; }, bang: true } ); }); commands.addUserCommand( ['availabledictionaries'], 'display available dictionaries', function () { liberator.echo('available dictionaries: ' + spellChecker.getDictionaryList()); }, {} ); /** * @param {String} url * @param {Function} callback * @param {String} charset */ function getHTML(url, callback, charset) { var xhr= new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { if (xhr.status == 200) { callback.call(this,xhr.responseText); } else { throw new Error(xhr.statusText); } } }; xhr.open('GET',url,true); if (charset) xhr.overrideMimeType('text/html; charset=' + charset); xhr.send(null); } /** * @param {String} str * @return {DOMDocument} */ function createHTMLDocument(str) { str = str.replace(/^[\s\S]*?<html(?:[ \t\r\n][^>]*)?>[ \t\n\r]*|[ \t\n\r]*<\/html[ \t\r\n]*>[\S\s]*$/ig,'').replace(/[\r\n]+/g,' '); var htmlFragment = content.document.implementation.createDocument(null,'html',null); var range = content.document.createRange(); range.setStartAfter(window.content.document.body); htmlFragment.documentElement.appendChild(htmlFragment.importNode(range.createContextualFragment(str),true)); return htmlFragment; } /** * @param {String} xpath XPath Expression * @param {DOMDocument} doc * @param {Boolean} isMulti * @return {Element} */ function getNodeFromXPath(xpath,doc,isMulti) { if (!xpath || !doc) return; var result; if (isMulti) { let nodesSnapshot = doc.evaluate(xpath,doc.documentElement,null,XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,null); if (nodesSnapshot.snapshotLength == 0) return; result = document.createElementNS(null,'div'); for (let i=0; i<nodesSnapshot.snapshotLength; result.appendChild(nodesSnapshot.snapshotItem(i++))); } else { let node = doc.evaluate(xpath,doc.documentElement,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null); if (!node.singleNodeValue) return; result = node.singleNodeValue; } return result; } /** * @return {Object} */ function buildSpellChecker() { var enable = liberator.globalVariables.lookupDictionary_enableSuggestion; enable = (enable === undefined) ? true : !!parseInt(enable, 10); if (!enable) return; var spellChecker = new SpellChecker(); var isBeginningWith = liberator.globalVariables.lookupDictionary_beginningWith; isBeginningWith = (isBeginningWith === undefined) ? false : !!parseInt(isBeginningWith, 10); spellChecker.setBeginningWith(isBeginningWith); return spellChecker; } })(); // vim: fdm=marker sw=4 ts=4 et: