Skip to content

Instantly share code, notes, and snippets.

@awn-git
Created January 3, 2017 15:27
Show Gist options
  • Select an option

  • Save awn-git/fe60d010de2144bc7a53ab66061a5b21 to your computer and use it in GitHub Desktop.

Select an option

Save awn-git/fe60d010de2144bc7a53ab66061a5b21 to your computer and use it in GitHub Desktop.
おーぷん2ちゃんねるに書き込みするスクリプト

おーぷん2ちゃんねるに書き込みするスクリプト

これはなに?

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
  • mail
  • bbs
  • key
  • submit
  • time である。

MESSAGE

本文。

FROM

名前欄。

mail

メール欄。

bbs

bbsの名前。たとえば「news4vip」とか。 これはread.cgiの場合<script>タグの中で変数宣言されているので、変数bbsを入れればおk。

key

スレのキー。たとえば「1435906894」とか。 これはread.cgiの場合<script>タグの中で変数宣言されているので、変数keyを入れればおk。

submit

submitボタンの値(value)。"書き込む"と入れておくのが無難。"書"でもいけるかもしれない?

time

投稿時間。Math.floor(Date.now() / 1000)とかを入れておけばおk。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment