aboutsummaryrefslogtreecommitdiffstats
path: root/ldrize_cooperation_fetch_flv.js
blob: fa6ee390fe3ef4ee994297732eb12c5673c13999 (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
href='#n116'>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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
/**
 * I LOVE :echo
 * I LOVE :js
 *
 * 説明
 * :echo コマンドが大好きな人用
 * とにかく、ワンラインでタブ補完しつつコードを実行するためのネタ的プラグイン
 *
 * 完成度は低い
 *
 *  設計思想
 *   *  最初に与えた引数を変換に変換を重ねてコネくり回すことが主眼
 *   *  各メソッドやgetterは常にvalue(またはxhr)メンバを持つObject
 *   *  メソッドやgetterはvalue値に相応しいメソッドまたはgetterである
 *
 *
 * Example:
 *        1                                        2      3      4   5        6           7                              8
 *  :echo $x("http://d.hatena.ne.jp/teramako/rss").open().send().xml.toObject.get("item").map(function(item) item.title).value
 *  ブログのRSSをゲットしてオブジェクト化して、各item要素内のtitle文字列を格納した配列をechoする
 *
 *  1. $xにURLを渡し
 *  2. XMLHttpRequest.open()し
 *  3. リクエストを送信し
 *  4. レスポンスをDOMオブジェクトを得て(XMLHttpRequest.responseXML)
 *  5. さらにObject化し(toObjectはXMLのタグ名をキーにツリー構造を模したObject)
 *  6. オブジェクト内の"item"メンバ(配列)を得て
 *  7. map関数で各item内のtitleを得て
 *  8. その値 :echo に渡す(RSSのタイトル一覧の出力となる)
 *
 *  :js $_.url.MD5Hash.copy()
 *  現在開いているURLのMD5ハッシュ値をクリップボードにコピー
 *
 *  :js $x("http://example.com").open().send().xml.stack()
 *   とりあえず、http://exapmle.comのDOMドキュメントをstack
 *  :echo $_.cache.last.inspect()
 *   最後にstackしたものを取り出し、DOM Inspectorが入っている場合はDOM Inspectorに出力
 *
 *  :echo $('//a').evaluate().grep(/^http/).join("\n").copy()
 *  リンクを抽出してクリップボードにコピー
 *
 *  :echo $('//a').evaluate().map(function(v)v.href.replace(/.*\//,'')).copy()
 *  :echo $('//a').evaluate().map($f.href().replace(/.*\//,'')).copy()
 *  リンクのファイル名部分のみをクリップボードにコピー
 *
 */

(function(){
var cache = [];
var cacheXHR = null;

function $(arg){ //{{{
    if (!arg) return new $c();
    if (typeof arg == "string"){
        let s = new $s(arg);
        if (/^https?:\/\/./.test(arg)){
            s.open = function(){ var x = modules.$x(arg); return x.open(); };
        }
        return s;
    } else if (typeof arg == "xml"){
        return new $e4x(arg);
    } else if (arg instanceof Array){
        return new $a(arg);
    } else if (arg instanceof Element || arg instanceof Document){
        return new $xml(arg);
    } else if (typeof arg == "object"){
        return new $o(arg);
    }
} //}}}
modules.$f = (function(){ //{{{
    const pests = [
        '__defineGetter__', '__defineSetter__', 'hasOwnProperty', 'isPrototypeOf',
        '__lookupGetter__', '__lookupSetter__', '__parent__', 'propertyIsEnumerable',
        '__proto__', 'toSource', 'toString', 'toLocaleString', 'unwatch', 'valueOf', 'watch'
    ];

    function id(value)
        value;

    function memfn(parent)
        function(name,args)
            FFF(function(self)
                    let (s = parent(self))
                        let (f = s[name])
                            (f instanceof Function ? s[name].apply(s, args) : f));

    function mem(parent)
        function(name)
            FFF(function(self)
                    parent(self)[name]);

    function FFF(parent){
        parent.__noSuchMethod__ = memfn(parent);
        parent.m = {__noSuchMethod__: mem(parent)};
        pests.forEach(function(it) (parent[it] = function() parent.__noSuchMethod__(it, arguments)));
        return parent;
    }

    return FFF(id);
})(); //}}}


modules.$ = $;
modules.$x = function $x(url, method, user, password){ //{{{
    if (!cacheXHR){
        cacheXHR = new $xhr(url, method, user, password);
    } else if (cacheXHR.success && cacheXHR.url == url){
        return cacheXHR;
    } else {
        cacheXHR = new $xhr(url      || cacheXHR.url,
                            method   || cacheXHR.method,
                            user     || cacheXHR.user,
                            password || cacheXHR.password);
    }
    return cacheXHR;
}; //}}}
modules.$_ = { //{{{
    cache: {
        get: function(num) $(cache[num]),
        get length() cache.length,
        get first() this.length > 0 ? this.get(0)             : null,
        get last()  this.length > 0 ? this.get(this.length-1) : null,
        get all() $(cache),
        shift: function() $(cache.shift()),
        pop: function() $(cache.pop()),
        get xhr() cacheXHR,
        clear: function(){
            cache = [];
            cacheXHR = null;
        }
    },
    env: {
        maxCacheLength: 20,
        autoCache: false,
        xhr: { user: null, password: null }
    },
    get clipboard() $(util.readFromClipboard()),
    get url() $(buffer.URL),
    get sel() $(content.window.getSelection().toString()),
    get lastInputValue(){
        if (buffer.lastInputField){
            return $(buffer.lastInputField.value);
        }
        return null;
    }
}; // }}}
const DOMINSPECTOR = Application.extensions.has("inspector@mozilla.org") && Application.extensions.get("inspector@mozilla.org").enabled;

// -----------------------------------------------------------------------------
// Core
// --------------------------------------------------------------------------{{{
function $c(arg){ this.value = arg || null; }
$c.prototype = {
    echo: function(flag){
        liberator.echo(this.value, flag);
        return this;
    },
    log: function(level){
        liberator.log(this.value, level || 0);
        return this;
    },
    copy: function(){
        util.copyToClipboard(this.value);
        return this;
    },
    stack: function(){
        cache.push(this.value);
        return this;
    },
    toString: function(){
        return this.value.toString();
    },
    __noSuchMethod__: function(name, args){
        return $(this.value[name].apply(this.value, args));
    }
};
// }}}

// -----------------------------------------------------------------------------
// String
// --------------------------------------------------------------------------{{{
function $s(arg){ this.value = arg || null; }
$s.prototype = new $c();
createPrototype($s, {
    get htmlEscape() $(this.value.replace("&","&amp;","g").replace("<","&lt;","g").replace(">","&gt;","g")),
    get utf16() $(["\\u"+("0000"+this.value.charCodeAt(i).toString(16).toUpperCase()).slice(-4) for (i in this.value)].join("")),
    get numCharRef() $(["&#" + this.value.charCodeAt(i) + ";" for (i in this.value)].join("")),
    get base64() $(window.btoa(this.value)),
    get encodeURICompoenent() $(encodeURIComponent(this.value)),
    get MD5Hash(){
        var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
        converter.charset = "UTF-8";
        var result = {};
        var data = converter.convertToByteArray(this.value, result);
        var ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
        ch.init(ch.MD5);
        ch.update(data, data.length);
        var hash = ch.finish(false);
        function toHexString(charCode){
            return ("0" + charCode.toString(16)).slice(-2);
        }
        var s = [i < hash.length ? toHexString(hash.charCodeAt(i)) : "" for (i in hash)].join("");
        return $(s);
    },
    s: function(from, to) $(this.value.replace(from, to)),
    split: function(reg) $(this.value.split(reg)),
    get toJSON(){
        var json;
        try {
            json = Cc["@mozilla.org/dom/json;1"].getService(Ci.nsIJSON);
            return $(json.decode(this.value));
        } catch (e){
            return null;
        }
    },
    evaluate: function(doc, context){
        if (!doc) doc = content.document;
        if (!context) context = doc;
        var result = [];
        var nodes = doc.evaluate(this.value, context, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
        var node;
        while (node = nodes.iterateNext()){
            result.push(node);
        }
        return $(result);
    }
});
// }}}

// -----------------------------------------------------------------------------
// Array
// --------------------------------------------------------------------------{{{
function $a(arg){ this.value = arg; }
$a.prototype = new $c();
createPrototype($a, {
    get length() this.value.length,
    item:    function(index) 0 <= index && index < this.length ? $(this.value[index]) : null,
    join:    function(str) $(this.value.join(str)),
    grep:    function(reg) $(this.value.filter(function(v) reg.test(v.toString()))),
    forEach: function(func, thisp) $(this.value.forEach(func, thisp)),
    map:     function(func, thisp) $(this.value.map(func, thisp)),
    some:    function(func, thisp) $(this.value.some(func, thisp)),
    filter:  function(func, thisp) $(this.value.filter(func, thisp)),

    push:    function(arg){
        this.value.push(arg);
        return this;
    },
    unshift: function(arg){
        this.value.unshift(arg);
        return this;
    },
    get first() $(this.value[0]),
    get last() $(this.value[this.length - 1])
});
// }}}

// -----------------------------------------------------------------------------
// Object
// --------------------------------------------------------------------------{{{
function $o(arg){ this.value = arg; }
$o.prototype = new $c();
createPrototype($o, {
    get toArrayName()  $([i             for (i in this.value)]),
    get toArrayValue() $([this.value[i] for (i in this.value)]),
    get: function(prop){
        if (prop in this.value) return $(this.value[prop]);
    },
    getItemsByKeyName: function(itemName){
        var a = [];
        function walk(obj){
            for (let item in obj){
                if (itemName == item) a.push(obj[itemName]);
                if (typeof obj[item] == "object") walk(obj[item]);;
            }
        }
        walk(this.value);
        return $(a);
    },
    map: function(func, thisp){
        if (typeof func != "function") throw new TypeError();
        var obj = {};
        var thisp = arguments[1];
        for (let i in this.value){
            obj[i] = func.call(thisp, this.value[i], i, this);
        }
        return $(obj);
    },
    forEach: function(func, thisp){
        if (typeof func != "function") throw new TypeError();
        var thisp = arguments[1];
        for (let i in this.value){
            func.call(thisp, this.value[i], i, this);
        }
        return this;
    },
    filter: function(func, thisp){
        if (typeof func != "function") throw new TypeError();
        var obj = {};
        var thisp = arguments[1];
        for (let i in this.value){
            if (func.call(thisp, this.value[i], i, this)){
                obj[i] = this.value[i];
            }
        }
        return $(obj);
    },
    get toJSON(){
        var json = Cc["@mozilla.org/dom/json;1"].getService(Ci.nsIJSON);
        return $(json.encode(this.value));
    }
});
if (DOMINSPECTOR){ createPrototype($o, { inspect: function(){ inspectObject(this.value); return ""; } }); }
// }}}

// -----------------------------------------------------------------------------
// XML
// --------------------------------------------------------------------------{{{
function $xml(arg){ this.value = arg; }
$xml.prototype = new $c();
createPrototype($xml, {
    get toObject(){ // {{{2
        function parseElement(node){ //{{{3
            var res = {};
            var isTextOnly = true;
            if (node.attributes && node.attributes.length > 0){
                isTextOnly = false;
                let attrs = node.attributes;
                for (let i=0; i<attrs.length; i++){
                    res["@"+attrs[i].nodeName] = attrs[i].nodeValue;
                }
            }
            if (isTextOnly){
                for (let i=0; i<node.childNodes.length; i++){
                    let type = node.childNodes[i].nodeType;
                    if (type != 3 && type != 4){
                        isTextOnly = false;
                    }
                }
            }
            if (isTextOnly){
                res = "";
                for (let i=0; i<node.childNodes.length; i++){
                    res += node.childNodes[i].nodeValue;
                }
            } else {
                for (let i=0; i<node.childNodes.length; i++){
                    let child = node.childNodes[i];
                    let name = child.nodeName;
                    let value = parse(child);
                    if (!value) continue;
                    if (!res[name]){
                        res[name] = value;
                    } else {
                        if (!(res[name] instanceof Array)){
                            res[name] = [res[name]];
                        }
                        res[name].push(value);
                    }
                }
            }
            return res;
        } //3}}}
        function parse(node){ //{{{3
            switch(node.nodeType){
                case node.ELEMENT_NODE:
                    return parseElement(node);
                case node.TEXT_NODE:
                case node.CDATA_SECTION_NODE:
                    return /[^\x00-\x20]/.test(node.nodeValue) ? node.nodeValue : null;
            }
            return null;
        } //3}}}
        var elm;
        if (this.value instanceof Document){
            elm = this.value.documentElement;
        } else {
            elm = this.value;
        }
        var o = parse(elm);
        return $(o);
    }, // 2}}}
    evaluate: function(expression){ // {{{2
        function nsResolver(prefix){ // {{{3
            var ns = {
                xhtml:   "http://www.w3.org/1999/xhtml",
                rdf:     "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
                dc:      "http://purl.org/dc/elements/1.1/",
                content: "http://purl.org/rss/1.0/modules/content/",
                taxo:    "http://purl.org/rss/1.0/modules/taxonomy/",
                rss:     "http://purl.org/rss/1.0/",
                atom:    "http://purl.org/atom/ns#"
            };
            return ns[prefix] || null;
        } // 3}}}
        var xpe = new XPathEvaluator();
        var result, found = [], res;
        try {
            result = xpe.evaluate(expression, this.value, nsResolver, 0, null);
            while (res = result.iterateNext()) found.push(res);
            return $(found);
        } catch (e){ }
    }, // 2}}}
});
if (DOMINSPECTOR){
    createPrototype($xml, {
        inspect: function(){
            if (this.value instanceof Document)
                inspectDOMDocument(this.value);
            else
                inspectDOMNode(this.value);
        }
    });
}
// }}}

// -----------------------------------------------------------------------------
// XMLHttpRequest
// --------------------------------------------------------------------------{{{
function $xhr(url, method, user, password, xhr){
    this.url = url || null;
    this.method = method || "GET";
    this.user = user || null;
    this.password = password || null;
    this.xhr = xhr || new XMLHttpRequest();
    this.success = null;
}
$xhr.prototype = {
    open: function(url, user, password){
        if (this.xhr.readyState != 0) return this;
        if (url) this.url = url;
        if (user) this.user = user;
        if (password) this.password = password;
        this.xhr.open(this.method, this.url, false, this.user, this.password);
        return this;
    },
    setMIME: function(type, charset){
        this.xhr.overrideMimeType((type || "text/html") +
                                  (charset ? ("; charset=" + charset) : ""));
        return this;
    },
    send: function(){
        if (this.xhr.readyState < 3) this.xhr.send(null);
        if (this.xhr.status == 200){
            this.success = true;
            return new $xhrResult(this.xhr);
        } else {
            this.success = false;
            return this;
        }
    }
};
// }}}

// -----------------------------------------------------------------------------
// XMLHttpRequest Result
// --------------------------------------------------------------------------{{{
function $xhrResult(xhr){
    this.value = xhr;
}
$xhrResult.prototype = new $c();
createPrototype($xhrResult, {
    get allHeaders(){
        return $(this.value.getAllResponseHeaders());
    },
    getHeader: function(header){
        return $(this.value.getResponseHeader(header));
    },
    get text(){
        return $(this.value.responseText);
    },
    get xml(){
        if (this.value.responseXML){
            return $(this.value.responseXML);
        } else if (this.value.getResponseHeader("Content-Type").indexOf("text/html") == 0){
            let str = this.value.responseText.
                                 replace(/^[\s\S]*?<html(?:[ \t\r\n][^>]*)?>|<\/html[ \t\n\t]*>[\S\s]*$/ig, "").
                                 replace(/[\r\n]+/g, " ");
            let htmlFragment = document.implementation.createDocument(null, "html", null);
            //let range = window.content.document.createRange();
            let range = document.getElementById("liberator-multiline-output").contentDocument.createRange();
            range.setStartAfter(window.content.document.body);
            htmlFragment.documentElement.appendChild(htmlFragment.importNode(range.createContextualFragment(str), true));
            return $(htmlFragment);
        } else {
            return this.text;
        }
    }
});
// }}}

// -----------------------------------------------------------------------------
// E4X
// --------------------------------------------------------------------------{{{
function $e4x(arg){ this.value = arg; }
$e4x.prototype = new $c();
createPrototype($e4x, {
    get: function(str) $(window.eval("this.value." + str)),
    item: function(num) $(this.value[num]),
    get length() this.value.length(),
    toXMLString: function() $(this.value.toXMLString()),
    toArray: function(){
        var a = [];
        for (let i=0; i<this.length; i++){
            a.push(this.xml[i]);
        }
        return $(a);
    },
    toDocument: function(rootName){
        var parser = new DOMParser;
        rootName = rootName || "root";
        var str = this.length > 1 ? "<" + rootName +">" + this.value.toXMLString() + "</" + rootName + ">" : this.value.toXMLString();
        var doc = parser.parseFromString(str, "text/xml");
        if (doc.firstChild.tagName == "parsererror"){
            return;
        }
        return $(doc);
    },
    get toHTMLDOM() $(util.xmlToDom(this.value, document))
});
// }}}

function createPrototype(class, obj){
    var flag;
    for (let i in obj){
        flag = false;
        if (obj.__lookupGetter__(i)){
            class.prototype.__defineGetter__(i, obj.__lookupGetter__(i));
            flag = true;
        }
        if (obj.__lookupSetter__(i)){
            class..prototype.__defineSetter__(i, obj.__lookupSetter__(i));
            flag = true;
        }
        if (!flag) class.prototype[i] = obj[i];
    }
}
})();
// vim:sw=4 ts=4 et si fdm=marker: