Skip to content

Instantly share code, notes, and snippets.

@nora-tetsu
Created October 14, 2023 12:58
Show Gist options
  • Save nora-tetsu/5b1b60c5fae705767ddc7318fab7aa3d to your computer and use it in GitHub Desktop.
Save nora-tetsu/5b1b60c5fae705767ddc7318fab7aa3d to your computer and use it in GitHub Desktop.
Dynalistにて表示中のノードからTextManager(自作ツール)のJSON文字列を取得するブックマークレット(※説明用サンプル)
(function () {
// 最上位ノードまたはフォーカスしているノードの子項目を取得する(孫以下は含めない)
const parent = document.querySelector('.Node-children');
const children = parent.querySelectorAll(':scope > .Node-outer > .Node');
/**
* DateオブジェクトからUNIX時間を取得する関数
* @param {Date} date
* @returns
*/
function getUnix(date) {
return Math.floor(date.getTime() / 1000);
}
// データのオブジェクトを入れる配列を用意する
const data = [];
// 各ノードについて必要な値を取得しTextManager形式のオブジェクトにする
children.forEach(outer => {
// 作成日時を取得する
const bulletElm = outer.querySelector('.node-line.Node-bullet');
const regexp = /Created at ([0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}) on ([0-9]{4}\/[0-9]{1,2}\/[0-9]{1,2})/;
const match = bulletElm.title.match(regexp);
const date = match ? getUnix(new Date(match[2] + ' ' + match[1])) : 0;
// 下位項目も含めて本文を取得する
const textElms = outer.querySelectorAll('.Node-content.node-line.needsclick');
const textArr = Array.from(textElms).map(elm => elm.textContent.trim());
const text = textArr.join('/n');
// データのオブジェクトを作り、配列dataに追加する
const obj = {
unix: date,
keywords: ['@QuickDynalist'],
attr: '',
body: text,
title: '',
}
data.push(obj);
})
// 配列dataをJSON文字列にする
const txt = JSON.stringify(data, null, '\t');
// クリップボードにコピーする
navigator.clipboard.writeText(txt)
.then(() => {
// コピーに成功したら
console.group('クリップボードにコピーしました。');
console.log(txt.length < 100 ? txt : txt.slice(0, 100) + '…');
console.groupEnd();
alert('クリップボードにコピーしました。');
})
.catch(err => {
// コピーに失敗したら
console.log('コピーできませんでした。', err);
alert('コピーできませんでした。');
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment