N embed2link

    @@ -7,37 +7,45 @@ */ // ES6/7/2015+ monkey traning -// TODO more smart Harmony+ way; e.g.; -// ugly Array scan and RegExp captures, to Array#includes +// TODO more smart Harmony+ way e.g. +// - ugly Array scan and RegExp captures, to Array#includes (() => { - var swfSelector = [ + const swfSelector = [ 'embed[type="application/x-shockwave-flash"]', '[src^="http://ext.nicovideo.jp/swf/player/thumbwatch.swf"]', '[flashvars]' ].join(''); - Array.from(document.querySelectorAll(swfSelector)).forEach(swf => { + // TBD Array#find replace to Array#findIndex/Array#indexOf + let selectKVPValue = (ary, key) => { + let kvp = ary.find(a => RegExp(key + '=').test(a)); + if (!kvp) return ''; + + return (kvp.split('=') || [])[1] || ''; + }; + + Array.from(document.body.querySelectorAll(swfSelector)).forEach(swf => { // flashvars can't access from property and bracket-notation; - // -> swf.flashvars / swf['flashvars'] - var queries = decodeURIComponent(swf.getAttribute('flashvars')).split('&'); + // swf.flashvars / swf['flashvars'] -> Error + let queries = decodeURIComponent(swf.getAttribute('flashvars')).split('&'); - // TBD Array#find replace to Array#findIndex - var id = queries.find(q => /v=/.test(q)).match(/(sm\d+)/)[1]; + let container = document.createElement('div'); - var link = document.createElement('a'); + let id = selectKVPValue(queries, 'v'); + container.appendChild(document.createElement('br')); + let title = selectKVPValue(queries, 'thumbTitle'); + container.appendChild(document.createTextNode(title)); + container.appendChild(document.createElement('br')); + + let link = document.createElement('a'); link.href = 'http://www.nicovideo.jp/watch/' + id; - var title = queries.find(q => /thumbTitle=/.test(q)).split('=')[1] || ''; - var detail = queries.find(q => /thumbDescription=/.test(q)).split('=')[1] || ''; - /* - link.title = title + '\n' + detail; - /*/ - link.title = detail; - link.appendChild(document.createTextNode(title)); // TBD plaintext - link.appendChild(document.createElement('br')); - //*/ + let description = selectKVPValue(queries, 'thumbDescription'); + link.title = description; link.appendChild(document.createTextNode(link)); - swf.parentNode.replaceChild(link, swf); + container.appendChild(link); + + swf.parentNode.replaceChild(container, swf); }); -}).call(this); +}());
  • /*
     * @title N embed2link
     * @description replace Nicovideo embed player to link
     * @include http://*
     * @license MIT License http://opensource.org/licenses/MIT
     * @javascript_url
     */
    
    // ES6/7/2015+ monkey traning
    // TODO more smart Harmony+ way e.g.
    //      - ugly Array scan and RegExp captures, to Array#includes
    (() => {
        const swfSelector = [
            'embed[type="application/x-shockwave-flash"]',
            '[src^="http://ext.nicovideo.jp/swf/player/thumbwatch.swf"]',
            '[flashvars]'
        ].join('');
    
        // TBD Array#find replace to Array#findIndex/Array#indexOf
        let selectKVPValue = (ary, key) => {
            let kvp = ary.find(a => RegExp(key + '=').test(a));
            if (!kvp) return '';
    
            return (kvp.split('=') || [])[1] || '';
        };
    
        Array.from(document.body.querySelectorAll(swfSelector)).forEach(swf => {
            // flashvars can't access from property and bracket-notation;
            // swf.flashvars / swf['flashvars'] -> Error
            let queries = decodeURIComponent(swf.getAttribute('flashvars')).split('&');
    
            let container = document.createElement('div');
    
            let id = selectKVPValue(queries, 'v');
            container.appendChild(document.createElement('br'));
            let title = selectKVPValue(queries, 'thumbTitle');
            container.appendChild(document.createTextNode(title));
            container.appendChild(document.createElement('br'));
    
            let link = document.createElement('a');
            link.href = 'http://www.nicovideo.jp/watch/' + id;
            let description = selectKVPValue(queries, 'thumbDescription');
            link.title = description;
            link.appendChild(document.createTextNode(link));
    
            container.appendChild(link);
    
            swf.parentNode.replaceChild(container, swf);
        });
    }());
    
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/12/03 06:36:10 - 2015-12-03
  2. 2015/12/03 06:35:41 - 2015-12-03
  3. 2015/12/03 06:11:02 - 2015-12-03
  4. 2015/11/26 08:01:33 - 2015-11-26