changeset 989:964a99fe2fb1 draft

Add CSVget button
author HIROSE Yuuji <yuuji@gentei.org>
date Fri, 14 Oct 2022 23:03:09 +0859
parents e77d6258ad54
children 82a624dbb16d
files s4-main.js
diffstat 1 files changed, 72 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- 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) => {

yatex.org