display all bookmarks including no-comment @ Hatena::Bookmark

    
      
  • /*
     * @title display all bookmarks including no-comment @ Hatena::Bookmark
     * @description コメントがないブックマークも全て表示する
     * @include http://b.hatena.ne.jp/entry/*
     * @license MIT http://opensource.org/licenses/MIT
     * @javascript_url
     */
    /*
        test case
        http://b.hatena.ne.jp/entry/s/www.slideshare.net/enakai/it-51854916
            コメントなしブックマークが多い
        http://b.hatena.ne.jp/entry/s/kazayo.com/gozaisho-sounan/
            コメントありブックマークが多い
        http://b.hatena.ne.jp/entry/kazayo.com/gozaisho-sounan/
            コメントありの最初のブックマークよりも前に、コメントなしのブックマークがある
        http://b.hatena.ne.jp/entry/ift.tt/2wV68BP
            コメントありのブックマークがない
    */
    (() => {
        const d_ = document;
        const entry_url = encodeURIComponent(d_.documentElement.dataset['entryUrl']);
        const bookmark_container = d_.querySelector(".js-bookmarks-recent");
        const bookmark_template = d_.getElementById("autoloader-bookmark-item").innerHTML.replace(/^\s+/, "");
        const _2d = n => (n < 10 ? "0" : "") + n;
        const date_string = (d, sep) => [
            d.getFullYear(),
            _2d(d.getMonth() + 1),
            _2d(d.getDate())
        ].join(sep || "");
    
    
        function set_tab_title(n) {
            const loading = '<img src="http://cdn-ak.b.st-hatena.com/images/loading.gif">';
            let e = d_.querySelector('.js-bookmarks-sort-tab[data-sort="recent"]');
            e.innerHTML = "全てのブックマーク(" + (n || loading) + ")";
        }
    
        // init
        (() => {
            // inactivate auto loader
            let readmore = d_.querySelector(".js-read-more-button");
            if (readmore) {
                /*
                    https://cdn-ak.b.st-hatena.com/js/v4/bookmark.js
                    BookmarkAutoLoaderView#getLabelHeight
    
                    要素を見えなくしちゃうと、getBoundingClientRect は、top = 0 を返すので、
                    メソッドを乗っ取る
                */
                readmore.getBoundingClientRect = () => {
                    return {top: 1000000};
                };
            }
    
            bookmark_container.innerHTML = "";
            set_tab_title();
    
            d_.head.appendChild(Object.assign(d_.createElement("style"), {
                innerHTML: '                                                \
                    .hatena-star-comment-button {                           \
                        display: initial !important;                        \
                        margin-right: 8px !important;                       \
                    }                                                       \
                    .js-bookmarks-sort-tab[data-sort="recent"] > img {      \
                        width: 12px;                                        \
                        margin: 0 0.5ex;                                    \
                    }                                                       \
                    .entry-comment-readmore {                               \
                        display: none;                                      \
                    }                                                       \
                ',
            }));
    
        })();
    
        function append_bookmark(b, bookmark_container) {
            /*
                #enable_button ~ /enable_button       -- not implement
                #is_public ~ /is_public               -- not implement
                #should_nofollow ~ /should_nofollow   -- not implement
                anchor_path
                comment_expanded
                comment_page_path
                created
                profile_image_url
                tags
                user_name
                user_page_path
            */
            let created = new Date(b.created);
            let date = date_string(created, "/");
            let date2 = date_string(created);
            let legacyTagLinks = b.tags.map(tag => {
                return '<li><a href="/' + b.user.name + '/' + encodeURIComponent(tag) + '/">' + tag + '</a></li>';
            }).join("");
            let keyword_map = {
                anchor_path       : '/' + b.user.name + '/' + date2 + '#bookmark-' + b.location_id,
                comment_expanded  : b.comment_expanded,
                comment_page_path : "/entry/" + b.location_id + "/comment/" + b.user.name,
                created           : date,
                profile_image_url : b.user.image.image_url,
                tags              : legacyTagLinks,
                user_name         : b.user.name,
                user_page_path    : "/" + b.user.name,
            };
            let x = d_.createElement("div");
            x.innerHTML = bookmark_template.replace(/[{]{2,3}\s*([^}\s]+)\s*[}]{2,3}/g, (m, p) => {
                return keyword_map[p] || "";
            });
            // remove comment permalink with no comment
            if (b.comment == "") {
                let comment_permalink = x.querySelector(".entry-comment-permalink");
                comment_permalink.parentNode.removeChild(comment_permalink);
            }
            bookmark_container.appendChild(x.firstChild);
        }
    
        const xhr = new XMLHttpRequest();
        const resp_list = [];
        xhr.onload = (ev) => {
            if (ev.target.status < 400) {
                const resp = ev.target.response;
                /*
                    https://cdn-ak.b.st-hatena.com/js/v4/bookmark.star.js
                    EntryStarView.prototype.observeBookmarkListChange
    
                    はてなスターは MutationObserver が作ってくれるのだけれど、
                    Hatena.Star.EntryLoader は、static な領域を使って処理をするので、
                    load するたびにブックマークを追加すると、最後のブロックしか
                    スターが展開されない。
                */
                resp_list.push(resp.bookmarks);
                if (resp.cursor) {
                    load_bookmark(resp.cursor);
                } else {
                    let n = 0;
                    resp_list.forEach(bookmarks => {
                        n += bookmarks.length;
                        bookmarks.forEach(b => {
                            append_bookmark(b, bookmark_container);
                        });
                    });
                    set_tab_title(n);
                }
            }
        };
        xhr.responseType = "json";
        function load_bookmark(cursor) {
            let url = [
                "http://b.hatena.ne.jp/api/entry/",
                entry_url,
                "/bookmarks?cursor=" + cursor,
                "&limit=500&commented_only=0"
            ].join("");
            xhr.open("GET", url, true);
            xhr.send(null);
        }
    
        load_bookmark("");
    
    })();
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2018/03/23 08:30:53 - 2018-03-23
  2. 2018/03/19 23:37:26 - 2018-03-19
  3. 2018/03/19 23:37:06 - 2018-03-19
  4. 2017/09/11 16:49:36 - 2017-09-11
  5. 2017/09/09 01:57:19 - 2017-09-09
  6. 2017/09/08 23:25:01 - 2017-09-08
  7. 2017/09/08 13:35:17 - 2017-09-08