# HG changeset patch # User HIROSE Yuuji # Date 1665756216 -32373 # Node ID 964a99fe2fb19e823a3a5868643990aa6c4dded6 # Parent e77d6258ad549b072cc2f39dcf661109159590bc Add CSVget button diff -r e77d6258ad54 -r 964a99fe2fb1 s4-main.js --- a/s4-main.js Thu Oct 13 07:49:22 2022 +0859 +++ b/s4-main.js Fri Oct 14 23:03:09 2022 +0859 @@ -816,6 +816,62 @@ quizwarnVisible = true; } } + function downloadFile(filename, content) { + let bom = new Uint8Array([0xEF, 0xBB, 0xBF]); + let str = new Blob([bom, content], {type: "text/csv"}); + var uri = URL.createObjectURL(str); + let a = document.createElement("a"); + a.download = filename; + a.href = uri; + document.body.appendChild(a); + a.click(); + document.body.removeChild(a); + } + function getTextContentCSV_1(e) { + let blogtbl = document.querySelector("table.blog_replies"); + if (!blogtbl) return; + let trw = blogtbl.querySelector("tr.warn"), a; + if (trw && (a=trw.querySelector("th>a"))) { + if (a.title == "Show All") { + if (window.confirm(`50件以下に表示宣言されています。 +取得し直しますか? +Cancelを押すとこのまま取得します。`)) { + a.click(); + return; + } + } + } + outcsv = [] + for (let row of blogtbl.querySelectorAll("tr[id]")) { + let tds = row.querySelectorAll("td"), + a = tds[0].querySelector("a.author"), + author = a.title, + name = a.innerText, + time = tds[0].querySelector("span").title, + id = tds[1].id, + body = tds[1].textContent; + //console.log(`${author},${name},${time},#${id},${body}`); + outcsv.push({ + "author": author, "name": name, "time": time, + "id": "#"+id, "body": body}); + } + let line = new CSV(outcsv, {header:true}).encode(), + fn = myurl.replace(/.*\?/, "").replace("+", "-").replace(/#.*/, ""); + downloadFile(fn+".csv", line); + } + function getTextContentCSV(e) { + if (!document.getElementById("csvminjs")) { + let csvmin = document.createElement("script"); + csvmin.src="https://www.yatex.org/libcache/csv.min.js"; + csvmin.id = "csvminjs"; + // https://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js + csvmin.addEventListener("load", ()=>{ + getTextContentCSV_1(e)}, 10); + document.querySelector("head").appendChild(csvmin); + } else { + getTextContentCSV_1(e); + } + } function initBlogs() { // Auto-complete #xxxx let i, check = collectElementsByAttr("input", "name", "notifyto"); @@ -846,6 +902,22 @@ } i = document.getElementById("reload"); if (i) i.addEventListener("click", ajaxPost, false); + // Add CSV download button + let td = document.querySelector("table.bloghead tr td"); + if (td) { + let btn = document.createElement("button"); + btn.innerText = "CSVget"; + btn.type = "button"; + btn.title = `見えている書き込みをCSVで取得します +全件表示されていることを確認してから利用して下さい。 +Get seen TEXT content as CSV.`; + btn.addEventListener("click", getTextContentCSV, false); + let artlink = td.querySelector('a[accesskey="f"]'); + let spacer = document.createElement("span"); + spacer.innerText = "|"; + artlink.insertAdjacentElement('beforebegin', btn); + artlink.insertAdjacentElement('beforebegin', spacer); + } } for (i of document.querySelectorAll('input[type="file"]')) { i.addEventListener('change', (e) => {