FormDataでformDataを作って、XMLHttpRequestでpostするだけのスクリプト
//note: POST用formのデータを作る
var fd = new FormData();
fd.set("MESSAGE", generateRandomText());
fd.set("FROM", "");
fd.set("mail", "sage");
fd.set("bbs", bbs);
fd.set("key", key);
fd.set("submit", "書き込む");
fd.set("time", Math.floor(Date.now() / 1000));
//note: XHRでPOSTする
var xhr = new XMLHttpRequest();
xhr.addEventListener("loadend", f_xhr);
xhr.open("POST", "/test/bbs.cgi?guid=ON");
xhr.send(fd);
function f_xhr(result) {
var response = result.target.response;
if (response.includes("<title>書きこみました。</title>")) {
console.log("success");
} else {
console.log("error");
console.log(response);
}
}
//note: おまけ
function generateRandomText() {
return Date.now().toString(Date.now() % 35 + 2);
}formのフィールドにはおそらく最低限上記のnameが必要、つまり
- MESSAGE
- FROM
- bbs
- key
- submit
- time である。
本文。
名前欄。
メール欄。
bbsの名前。たとえば「news4vip」とか。
これはread.cgiの場合<script>タグの中で変数宣言されているので、変数bbsを入れればおk。
スレのキー。たとえば「1435906894」とか。
これはread.cgiの場合<script>タグの中で変数宣言されているので、変数keyを入れればおk。
submitボタンの値(value)。"書き込む"と入れておくのが無難。"書"でもいけるかもしれない?
投稿時間。Math.floor(Date.now() / 1000)とかを入れておけばおk。