s4

changeset 989:964a99fe2fb1

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 diff
     1.1 --- a/s4-main.js	Thu Oct 13 07:49:22 2022 +0859
     1.2 +++ b/s4-main.js	Fri Oct 14 23:03:09 2022 +0859
     1.3 @@ -816,6 +816,62 @@
     1.4  	    quizwarnVisible = true;
     1.5  	}
     1.6      }
     1.7 +    function downloadFile(filename, content) {
     1.8 +        let bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
     1.9 +	let str = new Blob([bom, content], {type: "text/csv"});
    1.10 +	var uri = URL.createObjectURL(str);
    1.11 +	let a   = document.createElement("a");
    1.12 +	a.download = filename;
    1.13 +	a.href = uri;
    1.14 +	document.body.appendChild(a);
    1.15 +	a.click();
    1.16 +	document.body.removeChild(a);
    1.17 +    }
    1.18 +    function getTextContentCSV_1(e) {
    1.19 +	let blogtbl = document.querySelector("table.blog_replies");
    1.20 +	if (!blogtbl) return;
    1.21 +	let trw = blogtbl.querySelector("tr.warn"), a;
    1.22 +	if (trw && (a=trw.querySelector("th>a"))) {
    1.23 +	    if (a.title == "Show All") {
    1.24 +		if (window.confirm(`50件以下に表示宣言されています。
    1.25 +取得し直しますか?
    1.26 +Cancelを押すとこのまま取得します。`)) {
    1.27 +		    a.click();
    1.28 +		    return;
    1.29 +		}
    1.30 +	    }
    1.31 +	}
    1.32 +	outcsv = []
    1.33 +	for (let row of blogtbl.querySelectorAll("tr[id]")) {
    1.34 +	    let tds = row.querySelectorAll("td"),
    1.35 +		a      = tds[0].querySelector("a.author"),
    1.36 +		author = a.title,
    1.37 +		name   = a.innerText,
    1.38 +		time   = tds[0].querySelector("span").title,
    1.39 +		id     = tds[1].id,
    1.40 +		body   = tds[1].textContent;
    1.41 +	    //console.log(`${author},${name},${time},#${id},${body}`);
    1.42 +	    outcsv.push({
    1.43 +		"author": author, "name": name, "time": time,
    1.44 +		"id": "#"+id, "body": body});
    1.45 +	}
    1.46 +	let line = new CSV(outcsv, {header:true}).encode(),
    1.47 +	    fn = myurl.replace(/.*\?/, "").replace("+", "-").replace(/#.*/, "");
    1.48 +	downloadFile(fn+".csv", line);
    1.49 +    }
    1.50 +    function getTextContentCSV(e) {
    1.51 +	if (!document.getElementById("csvminjs")) {
    1.52 +	    let csvmin = document.createElement("script");
    1.53 +	    csvmin.src="https://www.yatex.org/libcache/csv.min.js";
    1.54 +	    csvmin.id = "csvminjs";
    1.55 +	    // https://stackoverflow.com/questions/14521108/dynamically-load-js-inside-js
    1.56 +	    csvmin.addEventListener("load", ()=>{
    1.57 +		getTextContentCSV_1(e)}, 10);
    1.58 +	    document.querySelector("head").appendChild(csvmin);
    1.59 +	} else {
    1.60 +	    getTextContentCSV_1(e);
    1.61 +	}
    1.62 +    }
    1.63      function initBlogs() {
    1.64  	// Auto-complete #xxxx
    1.65  	let i, check = collectElementsByAttr("input", "name", "notifyto");
    1.66 @@ -846,6 +902,22 @@
    1.67  	    }
    1.68  	    i = document.getElementById("reload");
    1.69  	    if (i) i.addEventListener("click", ajaxPost, false);
    1.70 +	    // Add CSV download button
    1.71 +	    let td = document.querySelector("table.bloghead tr td");
    1.72 +	    if (td) {
    1.73 +		let btn = document.createElement("button");
    1.74 +		btn.innerText = "CSVget";
    1.75 +		btn.type = "button";
    1.76 +		btn.title = `見えている書き込みをCSVで取得します
    1.77 +全件表示されていることを確認してから利用して下さい。
    1.78 +Get seen TEXT content as CSV.`;
    1.79 +		btn.addEventListener("click", getTextContentCSV, false);
    1.80 +		let artlink = td.querySelector('a[accesskey="f"]');
    1.81 +		let spacer = document.createElement("span");
    1.82 +		spacer.innerText = "|";
    1.83 +		artlink.insertAdjacentElement('beforebegin', btn);
    1.84 +		artlink.insertAdjacentElement('beforebegin', spacer);
    1.85 +	    }
    1.86  	}
    1.87  	for (i of document.querySelectorAll('input[type="file"]')) {
    1.88  	    i.addEventListener('change', (e) => {