PublishBUKOME
/*
* @title PublishBUKOME
* @コメント一覧ページで「パーマリンク」アイコンを押して開いたブックマークコメントページで実行すると「埋め込みコード」を一部簡略(タグのリンクURL削除など)してクリップボードにコピーする
* @include http://*
* @license MIT License
* @require
*/
javascript:(function(){
/* 1. データの抽出 */
const doc = document;
const url = doc.querySelector('link[rel="canonical"]')?.href || location.href;
const entryTitle = doc.querySelector('.comment-entry-title a')?.innerText || '';
const entryUrl = doc.querySelector('.comment-entry-title a')?.href || '';
const userId = doc.documentElement.dataset.stableRequestUrl?.split('/').pop() || '';
const userIcon = doc.querySelector('.comment-body-username img')?.src || '';
const commentText = doc.querySelector('.comment-body-text')?.innerText || '';
const timestamp = doc.querySelector('.comment-body-date a')?.innerText || '';
const anchorUrl = doc.querySelector('.comment-body-date a')?.href || '';
/* 2. タグの抽出と加工 */
const tags = Array.from(doc.querySelectorAll('.comment-body-tags li a')).map(a => `[${a.innerText}]`);
const tagHtml = tags.length > 0
? `<ul class="comment-tag" style="list-style: none; margin: 0px;">\n<li style="float: left">🏷️</li>\n${tags.map(t => `<li style="float: left">${t}</li>`).join('\n')}\n</ul>`
: '';
/* 3. テキストの組み立て */
const result = `<blockquote class="hatena-bookmark-comment">
<a class="comment-info" href="${url}" data-user-id="${userId}" data-entry-url="${entryUrl}" data-original-href="${entryUrl}" data-user-icon="${userIcon.replace('https://cdn.profile-image.st-hatena.com', '')}">${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>
${tagHtml}
<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="${anchorUrl}">
<span class="datetime-body">${timestamp}</span>
</a>
</blockquote>`;
/* 4. クリップボードへのコピー */
navigator.clipboard.writeText(result).then(() => {
alert('テキストをクリップボードにコピーしました!');
}).catch(err => {
console.log(result);
alert('コピーに失敗しました。コンソール出力をご確認ください。');
});
})();
- Permalink
- このページへの個別リンクです。
- RAW
- 書かれたコードへの直接のリンクです。
- Packed
- 文字列が圧縮された書かれたコードへのリンクです。
- Userscript
- Greasemonkey 等で利用する場合の .user.js へのリンクです。
- Loader
- @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
- Metadata
- コード中にコメントで @xxx と書かれたメタデータの JSON です。