/*
* @title Yet Another 勝手にスター
* @description クリックしたところに、勝手に はてなスターを置きます(一回だけ)
* @include http://*
* @include https://*
* @license MIT License
* @javascript_url
*/
// inspired by
// http://let.hatelabo.jp/noromanba/let/hLHWyeejvP4c
// http://let.hatelabo.jp/pacochi/let/hLHWyNLcvcJD
(() => {
const d_ = document
function put_star_container(e) {
if (! d_.querySelector('script[src*="s.hatena.ne.jp"]')) {
d_.head.appendChild(Object.assign(d_.createElement("script"), {
type: "text/javascript",
src: "//s.hatena.ne.jp/js/HatenaStar.js",
onload: () => put_star_container(e),
}))
return
}
let uri = d_.location.href,
title = d_.title
let link = e.tagName == "A" && e.href ? e : e.querySelector("a[href]")
if (link) {
uri = link.href
title = link.textContent.trim()
e = link.parentNode.insertBefore(d_.createElement("span"), link.nextSibling)
}
// img にも appendChild できちゃうんだねえ
if (e.tagName == "IMG") {
e = e.parentNode.insertBefore(d_.createElement("span"), e.nextSibling)
}
// hr にも appendChild できちゃうんだが、childNode は見える
// 他にも駄目なタグありそう
let c = Hatena.Star.EntryLoader
const entry = new Hatena.Star.Entry({
entryNode: e,
uri: uri,
title: title,
comment_container: e.appendChild(c.createCommentContainer()),
star_container: e.appendChild(c.createStarContainer()),
});
entry.showButtons();
if (c.entries) {
c.entries.push(entry)
} else {
c.entries = [ entry ]
}
new Ten.JSONP('//s.hatena.ne.jp/entries.json?uri=' + uri, c, 'receiveStarEntries');
}
function one_time_handler(e) {
e.stopPropagation();
e.preventDefault();
put_star_container(e.target);
d_.removeEventListener('click', one_time_handler);
d_.removeEventListener('touchstart', one_time_handler);
}
d_.addEventListener('click', one_time_handler);
d_.addEventListener('touchstart', one_time_handler);
})()