ローカルブラウザメモ
code:.js
// 1. ページ読み込み時にURLのハッシュからDOMを復元
window.addEventListener('DOMContentLoaded', () => {
const hash = window.location.hash.substring(1);
if (hash) {
try {
// Base64をデコードしてHTMLを復元(日本語対応のためデコード処理を追加)
const decodedHtml = decodeURIComponent(escape(atob(hash)));
document.body.innerHTML = decodedHtml;
} catch (e) {
console.error("データの復元に失敗しました", e);
}
}
});
// 2. 編集されるたびにURLを更新
document.addEventListener('input', () => {
const html = document.body.innerHTML;
// HTMLタグをBase64形式に変換
const base64 = btoa(unescape(encodeURIComponent(html)));
// URLのハッシュ部分(#...)だけを更新(これならエラーにならない)
window.history.replaceState(null, null, "#" + base64);
});
url更新縛りきびちい