/*
* @title PublishBUKOME
* @コメント一覧ページで「パーマリンク」アイコンを押して開いたブックマークコメントページで実行すると「埋め込みコード」を一部簡略(タグのリンクURL削除など)してクリップボードにコピーする
* @include http://*
* @license MIT License
* @require
*/
javascript:(function(){
/* 1. データの抽出 */
const getMeta = (attr) => document.documentElement.getAttribute(attr);
const entryUrl = getMeta('data-entry-url');
const entryTitle = document.querySelector('.comment-entry-title a')?.innerText || '';
const userId = getMeta('data-stable-request-url')?.split('/').pop() || '';
const userIcon = document.querySelector('.comment-body-username img')?.src || '';
const commentText = document.querySelector('.comment-body-text')?.innerText || '';
const timestampText = document.querySelector('.comment-body-date a')?.innerText || '';
const permalink = document.querySelector('.comment-body-date a')?.href || '';
/* タグの取得と成形 */
const tags = Array.from(document.querySelectorAll('.comment-body-tags li a'))
.map(a => `<li style="float: left">[${a.innerText}]</li>`).join('\n');
/* 2. テキストの組み立て */
const output = `<blockquote class="hatena-bookmark-comment">
<a class="comment-info" href="${entryUrl}" data-user-id="${userId}" data-entry-url="${entryUrl}" data-original-href="${entryUrl}" data-entry-favicon="https://cdn-ak2.favicon.st-hatena.com/64?url=${encodeURIComponent(entryUrl)}" data-user-icon="/users/${userId}/profile.png">${entryTitle}</a>
<a href="http://b.hatena.ne.jp/entry/${entryUrl}" target="_blank"><img border="0" src="http://b.hatena.ne.jp/entry/image/${entryUrl}" alt="" /></a>
<br>
<ul class="comment-tag" style="list-style: none; margin: 0px;">
<li style="float: left">🏷️</li>
${tags}
</ul>
<br>
<p style="clear: left">
<span class="comment-body-username">
<a href="/${userId}/bookmark"><img src="${userIcon}" alt="${userId}">${userId}</a>
</span>
<br>
${commentText}
</p>
<a class="datetime" href="${permalink}">
<span class="datetime-body">${timestampText}</span>
</a>
</blockquote>`;
/* 3. クリップボードへのコピーと通知 */
console.log(output);
navigator.clipboard.writeText(output).then(() => {
alert('テキストをコピーしました。');
}).catch(err => {
alert('コピーに失敗しました。コンソールを確認してください。');
});
})();