Bookmark Count @ jinriki

  • /*
     * @title Bookmark Count @ jinriki
     * @description Add bookmark count to Hatena Question
     * @include http://q.hatena.ne.jp/*
     * @license MIT License
     * @javascript_url
     */
    var a_kuma3_bookmark = {
        add_bookmark_count: function (result) {
            var copy_attr = function(dest, from) {
                for (var k in from) { dest[k] = from[k]; }
            };
            Array.prototype.forEach.call(document.body.querySelectorAll('A.question-content-container'), function(link) {
                var bm_count = result[link.href];
                if (typeof(bm_count) == "number") { // bookmark count can be zero
                    if (bm_count > 0) {
                        var bm_ele = document.createElement("A");
                        copy_attr(bm_ele.style, {
                            color: (bm_count > 1 ? "#ff2c6f" : "gray"),
                            fontWeight: (bm_count > 1 ? "bolder" : "normal"),
                            textDecoration: "none",
                            fontSize: "90%",
                            marginLeft: "1em",
                        });
                        copy_attr(bm_ele, {
                            innerHTML: bm_count + " user" + (bm_count > 1 ? "s" : ""),
                            href: "http://b.hatena.ne.jp/entry/" + link.href,
                            target: "_blank",
                            title: "ブックマーク数",
                        });
                        link.nextSibling.appendChild(bm_ele);
                    }
                    link.bookmark_count_added = true;
                }
            });
    
            if (this.remained_to_process) {
                this.get_bookmark_count(document.body);
            }
        },
        get_bookmark_count: function  (context) {
            // hatena bookmark getcount API
            // http://developer.hatena.ne.jp/ja/documents/bookmark/apis/getcount
            this.remained_to_process = false;
            var request_link_count = 0;
            var self = this;
            var request_url = "http://api.b.st-hatena.com/entry.counts?callback=a_kuma3_bookmark.add_bookmark_count&" + 
                Array.prototype.filter.call(context.querySelectorAll('A.question-content-container'),
                    function(link, index, array) {
                        if (! link.bookmark_count_added) {
                            if (request_link_count < 50) {
                                request_link_count += 1;
                                return true;
                            } else {
                                self.remained_to_process = true;
                            }
                        }
                        return false;
                    })
                .map(function(link) {
                        return "url=" + link.href;
                    })
                .join("&");
    
            if (request_link_count > 0) {
                var script = document.createElement("script");
                script.src = request_url;
                document.body.appendChild(script);
            }
    
        },
        run: function() {
            this.get_bookmark_count(document.body);
    
            // https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
            var MutationObserver = window.MutationObserver || window.WebkitMutationObserver;
            new MutationObserver(function (records) {
                records.forEach(function (record) {
                    if (record.addedNodes) {
                        if (Array.prototype.find.call(record.addedNodes, function(e) {
                            return e.tagName == "UL" && /list-question/.exec(e.className);
                        })) {
                            a_kuma3_bookmark.get_bookmark_count(document.body);
                        }
                    }
                });
            }).observe(document.body, { childList: true, subtree: true });
        },
    };
    
    a_kuma3_bookmark.run();
    
    //  another method, maybe easier...
    //  http://b.hatena.ne.jp/entry/image/
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/07/24 15:50:51 - 2015-07-24
  2. 2015/07/24 15:49:03 - 2015-07-24