H#id link

    @@ -1,38 +1,55 @@ /* - * @title leanpub outline - * @description append TOC links by fragment identifires on leanpub - * @include https://leanpub.com/* - * @contributor efcl http://let.hatelabo.jp/efcl/let/hJmfwszo56oZ (Fork of) + * @title H#id link + * @description append Fragment Identifier link to headings + * @include http://* + * @include https://* + * @contributor efcl http://let.hatelabo.jp/efcl/let/hJmfwszo56oZ + * @contributor noromanba http://let.hatelabo.jp/noromanba/let/hJmfwve26ZM4 (Fork of) * @license MIT License http://opensource.org/licenses/MIT * @javascript_url */ -// - fix syntax error (illegal block-comment) -// - work in Firefox and others (Array.from) - -// e.g. -// https://leanpub.com/oopinjavascript/read (function () { - -if (!/leanpub\.com$/.test(location.hostname) || - !/read/.test(location.pathname.split('/').pop())) { - return; -} - -var headers = document.querySelectorAll("h1[id], h2[id], h3[id], h4[id], h5[id]"); -[].slice.call(headers).forEach(function(header) { - var headerID = header.getAttribute("id"); - if (!headerID) { return; } - - var linkName = document.createElement("a"); - linkName.href = "#" + headerID; - wrap(header, linkName); -}); - -function wrap(el, wrapper) { - el.parentNode.insertBefore(wrapper, el); - wrapper.appendChild(el); -} - + var wrap = function (node, wrapper) { + node.parentNode.insertBefore(wrapper, node); + wrapper.appendChild(node); + }; + + var hSelector = Array(6).join().split(',').map(function (_, idx) { + return 'h' + (idx + 1) + '[id]'; + }); + + var slice = Array.prototype.slice; + var appendFrag = function (ctx) { + if (!ctx.querySelectorAll) return; + + var headings = (function () { + hSelector.some(function (sel) { + if (ctx.nodeName.toLowerCase() === sel.slice(0, 2)) { // lame + headings = [ctx]; + return true; + } + }); // TBD concat w/ map or filter + return slice.call(ctx.querySelectorAll(hSelector)); + })(); + + headings.forEach(function (heading) { + var id = heading.id; + if (!id) return; + + // TBD replaceState + var frag = document.createElement('a'); + frag.href = '#' + id; + wrap(heading, frag); + }); + }; + appendFrag(document.body); + + // TBD omit + new MutationObserver(function (records) { + records.forEach(function (record) { + appendFrag(record.target); + }); + }).observe(document.body, { childList: true, subtree: true }); })();
  • /*
     * @title H#id link
     * @description append Fragment Identifier link to headings
     * @include http://*
     * @include https://*
     * @contributor efcl        http://let.hatelabo.jp/efcl/let/hJmfwszo56oZ
     * @contributor noromanba   http://let.hatelabo.jp/noromanba/let/hJmfwve26ZM4 (Fork of)
     * @license MIT License http://opensource.org/licenses/MIT
     * @javascript_url
     */
    
    (function () {
        var wrap = function (node, wrapper) {
            node.parentNode.insertBefore(wrapper, node);
            wrapper.appendChild(node);
        };
    
        var hSelector = Array(6).join().split(',').map(function (_, idx) {
            return 'h' + (idx + 1) + '[id]';
        });
    
        var slice = Array.prototype.slice;
        var appendFrag = function (ctx) {
            if (!ctx.querySelectorAll) return;
    
            var headings = (function () {
                hSelector.some(function (sel) {
                    if (ctx.nodeName.toLowerCase() === sel.slice(0, 2)) { // lame
                        headings = [ctx];
                        return true;
                    }
                }); // TBD concat w/ map or filter
                return slice.call(ctx.querySelectorAll(hSelector));
            })();
    
            headings.forEach(function (heading) {
                var id = heading.id;
                if (!id) return;
    
                // TBD replaceState
                var frag = document.createElement('a');
                frag.href = '#' + id;
                wrap(heading, frag);
            });
        };
        appendFrag(document.body);
    
        // TBD omit
        new MutationObserver(function (records) {
            records.forEach(function (record) {
                appendFrag(record.target);
            });
        }).observe(document.body, { childList: true, subtree: true });
    })();
    
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/06/22 05:49:15 - 2015-06-22
  2. 2015/06/22 05:48:58 - 2015-06-22