WorkFlowyのExportをもうちょっとなんとかしたい
WorkFlowy.appをハッキングするの一環。
Exportのテキストがみっちり詰まっているので、それを何とかしたい。
選択範囲のテキストをどうこうする。
ただし、window.getSelection()では選択したテキストは得られない
addedToSelectionclassがついたdivを取得し、そのinnerTextを処理する。
改行を二重に重ねる。
code:sample.js
let sel = document.querySelector(".addedToSelection");
console.log(sel.innerText.replace(/\n/g,"\n\n"));
これで項目と項目の間に空改行が入ったテキストが得られる。
それをショートカットキーで発動できるようにしておくと便利
クリップボードに直接入れるか、それようのモーダルdivを表示させるか。
雑にalertでもいいが、文字列が多くなると面倒かもしれない。
code:script.js
(function(){
document.body.addEventListener('keydown',event => {
if (event.altKey && event.metaKey){
if(event.keyCode===80){
console.log("hit key");
const p = document.querySelector(".addedToSelection");
if (!(p)){return;}
rep = p.innerText;
rep = rep.split("\n");
ptext = rep.join("\n\n");
const copyTextarea = document.createElement("textarea");
copyTextarea.value = ptext;
copyTextarea.style.width = "500px";
copyTextarea.style.height = "500px";
copyTextarea.onfocus = function() {
console.log("クリックされました");
this.select();
};
const maindiv = document.createElement("div");
maindiv.style.position = "fixed";
maindiv.style.top = "120px";
maindiv.style.left = "40px";
maindiv.style.background = "gainsboro";
maindiv.appendChild(copyTextarea)
const add_code = '<h2>コピーしてください</h2><div style="width:100px;font-size:20px;" onclick="this.parentElement.remove();">×</div>'
maindiv.insertAdjacentHTML( 'afterbegin', add_code);
document.getElementById('app').appendChild(maindiv);
}
}
});
})();
`https://scrapbox.io/api/code/rashitamemo/WorkFlowy%E3%81%AEExport%E3%82%92%E3%82%82%E3%81%86%E3%81%A1%E3%82%87%E3%81%A3%E3%81%A8%E3%81%AA%E3%82%93%E3%81%A8%E3%81%8B%E3%81%97%E3%81%9F%E3%81%84/script.js
code:append.js
var script2 = document.createElement('script');script2.src = 'https://scrapbox.io/api/code/rashitamemo/WorkFlowy%E3%81%AEExport%E3%82%92%E3%82%82%E3%81%86%E3%81%A1%E3%82%87%E3%81%A3%E3%81%A8%E3%81%AA%E3%82%93%E3%81%A8%E3%81%8B%E3%81%97%E3%81%9F%E3%81%84/script.js';document.body.appendChild(script2);
https://gyazo.com/92db9a3dae535b5acb8215de81dd0b0c
ref
新しいHTML要素を作成する | GRAYCODE JavaScript
【JavaScript】親要素・親ノードを取得する【parentElement・parentNode】|Into the Program
キーコード一覧 【JavaScript 動的サンプル】
指定したHTML要素を削除する | GRAYCODE JavaScript
CSSでposition: fixedを使うと文字が重なる原因と対処法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン
[JavaScript テキストボックスにフォーカスを当てた時に全選択状態にする – コピペで使える JavaScript逆引きリファレンス https://javascript.programmer-reference.com/js-text-select/]
JavaScriptで作成した要素にイベントを追加する方法を現役エンジニアが解説【初心者向け】 | TechAcademyマガジン