Skip to content

Instantly share code, notes, and snippets.

@hiko-p
Last active April 19, 2026 12:51
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 = null;
let sitename = null;
if (document.querySelector("meta[property='og:title']") && document.querySelector("meta[property='og:site_name']")){
title = document.querySelector("meta[property='og:title']").content;
sitename = '【' + document.querySelector("meta[property='og:site_name']").content + '】';
} else {
title = (document.title ? document.title : document.location.pathname.split('/').pop());
}
const url = document.querySelector("meta[property='og:url']") ?
document.querySelector("meta[property='og:url']").content
: document.URL;
const hashtagsearch = /#[a-zA-Z0-9_一-龠ぁ-んァ-ヴーa-zA-Z0-9]+/g;
const hashsearch = /#/g;
const atsearch = /@/g;
const separator = '---';
/*
* 投稿先サーバFQDN
* 利用されているサーバに合わせて書き換えてください
*/
const servername = 'example.com';
/*
* デフォルト公開範囲 public/unlisted/private/direct
* 投稿フォーム画面を開いた後にも変更可能です
*/
const visibility = 'public';
/* サーバタグ (付与しない場合は''を指定) */
const servertag = '';
/* タグ文字列 */
/* タイトルに含まれるハッシュタグをタグ文字列に加える(hashtag-barに乗る形で編集) */
let tags = servertag;
if (title.match(hashtagsearch))
tags = (tags + ' ' + title.match(hashtagsearch).join(' ').replace(',',' ').replace(/[  ]+/g,' ')).trim();
/* サイト名がある場合はタイトルとサイト名を結合し、ハッシュ、アットマークを置換してリンク化を回避 */
if (sitename)
title = title + sitename;
title = title.replace(hashsearch,'#').replace(atsearch,'@').replace(/\r?\n/g, ' ');
/* 本文 追加でコメント入力するための空行と引用記事の区切りを追加し、タグ文字列はhashtag-barに乗せるため、手前に空行を挟む */
const text = '\n' + separator + '\n'+ 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