changeset 997:f7cd4528926b draft

Warning message for BOM fixed
author HIROSE Yuuji <yuuji@gentei.org>
date Mon, 17 Oct 2022 17:11:04 +0859
parents a50f5b0b2c3d
children f73eece61ee4
files s4-main.js
diffstat 1 files changed, 17 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/s4-main.js	Sun Oct 16 07:56:34 2022 +0859
+++ b/s4-main.js	Mon Oct 17 17:11:04 2022 +0859
@@ -816,12 +816,13 @@
 	    quizwarnVisible = true;
 	}
     }
-    function downloadFile(filename, content) {
+    function downloadFile(filename, content, BOM) {
         let bom = new Uint8Array([0xEF, 0xBB, 0xBF]);
-	let str = new Blob([bom, content], {type: "text/csv"});
+	let str = new Blob(BOM ? [bom, content] : [content],
+			   {type: "text/csv"});
 	var uri = URL.createObjectURL(str);
 	let a   = document.createElement("a");
-	a.download = filename;
+	a.download = (BOM ? "BOM-" : "")+filename;
 	a.href = uri;
 	document.body.appendChild(a);
 	a.click();
@@ -829,6 +830,7 @@
     }
     function getTextContentCSV_1(e) {
 	let blogtbl = document.querySelector("table.blog_replies");
+	let needBOM = e.ctrlKey;
 	if (!blogtbl) return;
 	let trw = blogtbl.querySelector("tr.warn"), a;
 	if (trw && (a=trw.querySelector("th>a"))) {
@@ -843,6 +845,16 @@
 		}
 	    }
 	}
+	if (navigator.userAgent.match(/Windows/)) {
+	    if (!e.ctrlKey && !e.shiftKey && !window.confirm(`Excelで読ませるCSVの場合はBOMが必要です。
+その場合は一度キャンセルして Ctrl キーを押しながらボタンクリックして下さい。
+逆にExcel以外(GoogleスプレッドシートやLibreOfficeや他のツール)で読む場合はBOMをつけるとファイルの1行目の先頭にゴミのようなものが見える場合あるのでそのときは除去する必要があります。
+今後もBOM不要の場合はShiftキーを押しながらクリックして下さい。
+If you feed this CSV into Microsoft Excel, consider adding BOM sequence that can be prepended by pressing Control key with click.
+In this case, click CSVget with ctrl key after Cancel this dialog.
+If you never need BOM, press Shift key with click.`))
+		return;
+	}
 	outcsv = []
 	for (let row of blogtbl.querySelectorAll("tr[id]")) {
 	    let tds = row.querySelectorAll("td"),
@@ -860,7 +872,7 @@
 	let line = new CSV(outcsv, {header:true}).encode(),
 	    fn = myurl.replace(/.*\?/, "").replaceAll("+", "-")
 	    .replace(/#.*/, "").replace("-n:all", "");
-	downloadFile(fn+".csv", line);
+	downloadFile(fn+".csv", line, needBOM);
     }
     function getTextContentCSV(e) {
 	if (!document.getElementById("csvminjs")) {
@@ -913,6 +925,7 @@
 		btn.type = "button";
 		btn.title = `見えている書き込みをCSVで取得します
 全件表示されていることを確認してから利用して下さい。
+Excelで利用する場合は Ctrl を押しながらクリックして下さい。
 Get seen TEXT content as CSV.`;
 		btn.addEventListener("click", getTextContentCSV, false);
 		let artlink = td.querySelector('a[accesskey="f"]');

yatex.org