Skip to content

Instantly share code, notes, and snippets.

@hiko-p
Last active November 14, 2025 07:42
Show Gist options
  • Select an option

  • Save hiko-p/6aab32b10807bdd742808ff6f5d416f0 to your computer and use it in GitHub Desktop.

Select an option

Save hiko-p/6aab32b10807bdd742808ff6f5d416f0 to your computer and use it in GitHub Desktop.
閲覧中のWebページをmastodonに共有するためのブックマークレット。デフォルト付与するハッシュタグを決めておいたり、タイトルに含まれるハッシュタグなんかもカバーします。
javascript: (function () {
let title = document.title ? document.title : document.location.pathname.split('/').pop();
const url = document.URL;
const hashtagsearch = /\s*#\S+/g;
const hashsearch = /#/g;
const atsearch = /@/g;
/*
* 投稿先サーバFQDN
* 利用されているサーバに合わせて書き換えてください
*/
const servername = 'fedibird.com';
/*
* デフォルト公開範囲 public/unlisted/private/direct
* 投稿フォーム画面を開いた後にも変更可能です
*/
const visibility = 'public';
/* サーバタグ (デフォルト付与) */
const servertag = '#fedibird';
/* カテゴリタグ(以下をデフォルト値とする) */
let categorytag = '#nowreading';
/* 以下のURLとハッシュタグのマッピングに従ってカテゴリタグを上書き */
const taglist = {
'www.nicovideo.jp':'#nowwatching',
'seiga.nicovideo.jp':'#nowwatching',
'live.nicovideo.jp':'#nowwatching',
'www.youtube.com':'#nowwatching',
'music.youtube.com':'#nowplaying'
};
for (key in taglist){
if (url.includes(key)){
categorytag = taglist[key];
break;
}
}
/* タグ文字列 */
let tags = servertag + ' ' + categorytag;
/* タイトルに含まれるハッシュタグをタグ文字列に加える(hashtag-barに乗る形で編集) */
if (title.match(hashtagsearch))
tags = tags + ' ' + title.match(hashtagsearch).join('','').replace(',','').trim();
/* タイトルのハッシュ、アットマークを置換してリンク化を回避 */
title = title.replace(hashsearch,'#').replace(atsearch,'@');
/* 本文 タグ文字列はhashtag-barに乗せるため、手前に空行を挟む */
const text = title + '\n' + url + '\n\n' + tags;
/* 投稿フォーム画面を開く(ポップアップ) */
window.open(`https://${servername}/share?visibility=${visibility}&text=${encodeURIComponent(text)}`,'_blank','width=640,height=800');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment