Mercurial > hgrepos > hgweb.cgi > s4
comparison s4-main.js @ 989:964a99fe2fb1 draft
Add CSVget button
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Fri, 14 Oct 2022 23:03:09 +0859 |
parents | e71a86651d72 |
children | 82a624dbb16d |
comparison
equal
deleted
inserted
replaced
988:e77d6258ad54 | 989:964a99fe2fb1 |
---|---|
814 i.classList.add("hideauthor"); | 814 i.classList.add("hideauthor"); |
815 } | 815 } |
816 quizwarnVisible = true; | 816 quizwarnVisible = true; |
817 } | 817 } |
818 } | 818 } |
819 function downloadFile(filename, content) { | |
820 let bom = new Uint8Array([0xEF, 0xBB, 0xBF]); | |
821 let str = new Blob([bom, content], {type: "text/csv"}); | |
822 var uri = URL.createObjectURL(str); | |
823 let a = document.createElement("a"); | |
824 a.download = filename; | |
825 a.href = uri; | |
826 document.body.appendChild(a); | |
827 a.click(); | |
828 document.body.removeChild(a); | |
829 } | |
830 function getTextContentCSV_1(e) { | |
831 let blogtbl = document.querySelector("table.blog_replies"); | |
832 if (!blogtbl) return; | |
833 let trw = blogtbl.querySelector("tr.warn"), a; | |
834 if (trw && (a=trw.querySelector("th>a"))) { | |
835 if (a.title == "Show All") { | |
836 if (window.confirm(`50件以下に表示宣言されています。 | |
837 取得し直しますか? | |
838 Cancelを押すとこのまま取得します。`)) { | |
839 a.click(); | |
840 return; | |
841 } | |
842 } | |
843 } | |
844 outcsv = [] | |
845 for (let row of blogtbl.querySelectorAll("tr[id]")) { | |
846 let tds = row.querySelectorAll("td"), | |
847 a = tds[0].querySelector("a.author"), | |
848 author = a.title, | |
849 name = a.innerText, | |
850 time = tds[0].querySelector("span").title, | |
851 id = tds[1].id, | |
852 body = tds[1].textContent; | |
853 //console.log(`${author},${name},${time},#${id},${body}`); | |
854 outcsv.push({ | |
855 "author": author, "name": name, "time": time, | |
856 "id": "#"+id, "body": body}); | |
857 } | |
858 let line = new CSV(outcsv, {header:true}).encode(), | |
859 fn = myurl.replace(/.*\?/, "").replace("+", "-").replace(/#.*/, ""); | |
860 downloadFile(fn+".csv", line); | |
861 } | |
862 function getTextContentCSV(e) { | |
863 if (!document.getElementById("csvminjs")) { | |
864 let csvmin = document.createElement("script"); | |
865 csvmin.src="https://www.yatex.org/libcache/csv.min.js"; | |
866 csvmin.id = "csvminjs"; | |
867 // https://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js | |
868 csvmin.addEventListener("load", ()=>{ | |
869 getTextContentCSV_1(e)}, 10); | |
870 document.querySelector("head").appendChild(csvmin); | |
871 } else { | |
872 getTextContentCSV_1(e); | |
873 } | |
874 } | |
819 function initBlogs() { | 875 function initBlogs() { |
820 // Auto-complete #xxxx | 876 // Auto-complete #xxxx |
821 let i, check = collectElementsByAttr("input", "name", "notifyto"); | 877 let i, check = collectElementsByAttr("input", "name", "notifyto"); |
822 if (check) | 878 if (check) |
823 for (i of check) { | 879 for (i of check) { |
844 i.value = "送信(予備)" | 900 i.value = "送信(予備)" |
845 b.parentNode.appendChild(i); | 901 b.parentNode.appendChild(i); |
846 } | 902 } |
847 i = document.getElementById("reload"); | 903 i = document.getElementById("reload"); |
848 if (i) i.addEventListener("click", ajaxPost, false); | 904 if (i) i.addEventListener("click", ajaxPost, false); |
905 // Add CSV download button | |
906 let td = document.querySelector("table.bloghead tr td"); | |
907 if (td) { | |
908 let btn = document.createElement("button"); | |
909 btn.innerText = "CSVget"; | |
910 btn.type = "button"; | |
911 btn.title = `見えている書き込みをCSVで取得します | |
912 全件表示されていることを確認してから利用して下さい。 | |
913 Get seen TEXT content as CSV.`; | |
914 btn.addEventListener("click", getTextContentCSV, false); | |
915 let artlink = td.querySelector('a[accesskey="f"]'); | |
916 let spacer = document.createElement("span"); | |
917 spacer.innerText = "|"; | |
918 artlink.insertAdjacentElement('beforebegin', btn); | |
919 artlink.insertAdjacentElement('beforebegin', spacer); | |
920 } | |
849 } | 921 } |
850 for (i of document.querySelectorAll('input[type="file"]')) { | 922 for (i of document.querySelectorAll('input[type="file"]')) { |
851 i.addEventListener('change', (e) => { | 923 i.addEventListener('change', (e) => { |
852 warnFileSize(document.forms[0]); | 924 warnFileSize(document.forms[0]); |
853 }, false) | 925 }, false) |