/*
* @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
*/
/*
Changelog
- 記事につけられたスターを表示してみる
*/
/*
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']);
let bookmark_container;
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 || "");
// 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};
};
}
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; \
} \
.entry-info-meta { \
display: initial; /* flex */ \
} \
',
}));
// add tab
let comment_tabs = d_.querySelector("ul.entry-comment-tab");
let tab = comment_tabs.appendChild(Object.assign(d_.createElement("li"), {
className: "js-bookmarks-sort-tab",
innerHTML: "全てのブックマーク",
}));
tab.dataset["sort"] = "all";
// add panel
let sort_panel = d_.querySelector("div.js-bookmarks-sort-panels");
bookmark_container = sort_panel.appendChild(Object.assign(d_.createElement("div"), {
className: "bookmarks-sort-panel js-bookmarks-sort-panel",
innerHTML: '<div class="js-bookmarks js-bookmarks-all"></div>',
}));
bookmark_container.dataset["sort"] = "all";
bookmark_container = bookmark_container.firstChild;
tab.click();
// entry url star
d_.querySelector(".js-entry-info").appendChild(Object.assign(d_.createElement("span"), {
id: "entry_star_count",
}));
})();
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);
});
});
Hatena.Star.SiteConfig = {
entryNodes: {
"div.entry-info": {
uri: "h1.entry-info-title a",
title: "h1.entry-info-title a",
container: "#entry_star_count",
},
"div.js-bookmarks-all div.js-bookmark-item": {
uri: "a.js-bookmark-anchor-path",
title: "span.js-bookmark-comment",
container: "span.js-add-star-container"
}
}
};
new Hatena.Star.EntryLoader();
}
}
};
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("");
})();