Skip to content

Instantly share code, notes, and snippets.

@nora-tetsu
Created February 19, 2023 02:27
Show Gist options
  • Save nora-tetsu/5f649fa7eef9dbb4042ab68c1ce64659 to your computer and use it in GitHub Desktop.
Save nora-tetsu/5f649fa7eef9dbb4042ab68c1ce64659 to your computer and use it in GitHub Desktop.
Scrapboxの今日の日付のページに今開いているページのリンクと選択範囲を追記する
(function (){
const projectName = prompt('今開いているページをScrapboxに記録します。\nプロジェクトURLを入力してください。');
if(!projectName) return; // 未記入ならreturn
const today = new Date(); // 現在日時のDateオブジェクト
const year = today.getFullYear(); // 年(四桁)
const month = ('00' + (today.getMonth() + 1)).slice(-2); // 月(常に二桁表示になるように加工)
const date = ('00' + today.getDate()).slice(-2); // 日(常に二桁表示になるように加工)
const pageTitle = encodeURIComponent(`${year}/${month}/${date}`); // 文字列を作ってエンコードする
let url = `https://scrapbox.io/${projectName}/${pageTitle}`; // プロジェクトURLとページタイトルからURLを作る
const link = `[${document.title} ${window.location.href}]`; // 開いているページのタイトルとURLを取得してリンク記法にする
let selection = window.getSelection().toString(); // 選択範囲の文字列を取得する
if(selection.trim()) selection = '\n> ' + selection.replace(/\n/g, '\n> ').replace(/\>\s$/,''); // 選択範囲があれば各行を引用記法にする
url += `?body=${encodeURIComponent(link + selection)}`; // urlに加える
window.open(url); // Scrapboxを開く
})();
/*
javascript:(function(){const projectName=prompt('今開いているページをScrapboxに記録します。\nプロジェクトURLを入力してください。');if(!projectName)return;const today=new Date();const year=today.getFullYear();const month=('00'+(today.getMonth()+1)).slice(-2);const date=('00'+today.getDate()).slice(-2);const pageTitle=encodeURIComponent(`${year}/${month}/${date}`);let url=`https://scrapbox.io/${projectName}/${pageTitle}`;const link=`[${document.title} ${window.location.href}]`;let selection=window.getSelection().toString();if(selection.trim())selection='\n> '+selection.replace(/\n/g,'\n> ').replace(/\>\s$/,'');url+=`?body=${encodeURIComponent(link + selection)}`;window.open(url)})()
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment