/*
* @title ほぼ完全自動アク禁ブックマークレット2
* @include http://*.open2ch.net/test/read.cgi/*
* @license MIT License
*/
//作った人: Awn
(function() {
//class
class MutationListener {
constructor(f) {
this.target = document.querySelector("div.thread");
this.option = { childList: true };
this.mo = new MutationObserver(f);
}
start() {
this.mo.observe(this.target, this.option);
}
stop() {
this.mo.disconnect();
}
}
class AkukinAgent {
constructor(MESSAGE, FROM, mail) {
let fd = new FormData();
fd.set("bbs", bbs);
fd.set("key", key);
fd.set("submit", "書き込む");
this.fd = fd;
this.update(MESSAGE, FROM, mail);
let xhr = new XMLHttpRequest();
xhr.addEventListener("loadend", this.f_xhr);
this.xhr = xhr;
}
update(MESSAGE = "", FROM = "", mail = "") {
let fd = this.fd;
fd.set("FROM", FROM);
fd.set("mail", mail);
this.fd = fd;
this.MESSAGE = MESSAGE;
}
set(target) {
let fd = this.fd;
fd.set("MESSAGE", this.MESSAGE + "\n" + target);
fd.set("time", Math.floor(Date.now() / 1000));
this.fd = fd;
}
f_xhr(result) {
let response = result.target.response;
if (response.includes("<title>書きこみました。</title>")) {
//console.log("success");
} else {
console.log("error");
console.log(response);
}
}
exec() {
let xhr = this.xhr;
let fd = this.fd;
xhr.open("POST", "/test/bbs.cgi?guid=ON");
xhr.send(fd);
}
}
class ResHead {
set(dts) {
this.dts = dts;
}
parse() {
let reshead = [];
let main, mail;
const REGEXP_MAIN = new RegExp(/^([0-9]{1,4}) :(.*?) ?:.* ID:(.*$)/);
const REGEXP_MAIL = new RegExp(/<a href="mailto:(.*?)">/);
for (let value of this.dts) {
main = value.innerText.match(REGEXP_MAIN);
mail = value.outerHTML.match(REGEXP_MAIL);
reshead.push({
resnum: main[1] - 0,
name: main[2],
id: main[3].replace("(主)", "").replace(" ", "").replace("×", ""),
nusi: main[3].includes("(主)"),
mail: mail === null ? null : mail[1]
});
}
this.data = reshead;
return reshead;
}
get() {
return this.data;
}
}
class ResBody {
set(dds) {
this.dds = dds;
}
parse() {
let nodes, nodeName, node, imgurs, arr;
let resbody = [];
for (let value of this.dds) {
nodes = value.childNodes;
arr = [];
for (let ix = 0, len = nodes.length - 3; ix < len; ix++) {
node = nodes[ix];
nodeName = node.nodeName.toLowerCase();
//note: 通常の目に見えるテキスト,いわゆる本文の各一行
if (nodeName === "#text") {
arr.push(node.textContent.trim());
}
//note: 文末改行および空行
if (nodeName === "br") {
arr.push("\n");
}
//note: リンク
if (nodeName === "a") {
//note: 安価参照の場合はリンクではなく安価テキスト(e.g. >>243-256 )を取得する
if (/^\/test\/read\.cgi\/.*/.test(node.pathname)) {
arr.push(node.textContent.trim());
} else {
arr.push(node.href);
}
}
//note: imgur画像は複数毎がグループ化されているので、それを取り出す
if (nodeName === "div" && node.className === "group" && node.firstChild.className === "imgur") {
imgurs = node.querySelectorAll("a");
for (let imgur of imgurs) {
arr.push(imgur.href);
}
}
if (nodeName === "font") {
arr.push(node.textContent.trim());
}
}
resbody.push(arr.join(""));
}
this.data = resbody;
return resbody;
}
get() {
return this.data;
}
}
class Res {
set(reshead, resbody) {
for (let ix = 0, len = reshead.length; ix < len; ix++) {
reshead[ix].text = resbody[ix];
}
this.data = reshead;
}
get() {
return this.data;
}
}
//member
let _form = {};
let _idList = {};
//instances
let _mo;
let _agent;
let _reshead;
let _resbody;
let _res;
//initializer
_init();
//function
function _init() {
_embedForm();
let enableAkukin = _getStorage("enableAkukin");
let MESSAGE = _getStorage("autoAku_defaultText");
let FROM = _getStorage("autoAku_defaultName");
let mail = _getStorage("autoAku_defaultMail");
_agent = new AkukinAgent(MESSAGE, FROM, mail);
_mo = new MutationListener(_MutationCallback);
if (enableAkukin) {
_setupForm();
if (enableAkukin === "ON") {
_mo.start();
}
}
_assignEventListener();
_createInstances();
}
function _assignEventListener() {
ShowBtnEventListener();
SaveBtnEventListener();
DeleteBtnEventListener();
SpeedLimitRadioBtnEventListener();
}
function _createInstances() {
_res = new Res();
_reshead = new ResHead();
_resbody = new ResBody();
}
function _embedForm() {
let section = document.createElement("section");
section.setAttribute("id", "autoAkuSection");
section.innerHTML = '<hr><h2 style="color:red;">自動アク禁設定</h2><input type="button" name="b_autoAkuShow" id="b_autoAkuShow" value="開く"> <input type="button" name="b_autoAkuDelete" id="b_autoAkuDelete" value="データを全て消す">';
let form = document.createElement("form");
form.setAttribute("name", "fm_autoAku");
form.setAttribute("id", "fm_autoAku");
form.setAttribute("style", "display:none;");
form.innerHTML = '<fieldset><legend>自動アク禁投稿で使用する名前とmailと本文</legend><label>名前:<input type="text" size="40" name="autoAku_defaultName" placeholder="(省略化)自動アク禁用の名前"></label><br><label>mail:<input type="text" size="40" name="autoAku_defaultMail" placeholder="(省略化)自動アク禁用のmail"></label><br><textarea rows="5" cols="56" name="autoAku_defaultText" placeholder="(省略可)ここに書いたメッセージに!aku42といったアク禁コマンドが自動で足されます。"></textarea></fieldset><fieldset><legend>自動アク禁キーワード設定</legend><label>アク禁ワード↓<br><textarea rows="5" cols="56" name="autoAku_words" placeholder="改行区切りで入力"></textarea><br></label><label>アク禁ネーム↓<br><textarea rows="5" cols="56" name="autoAku_names" placeholder="改行区切りで入力"></textarea><br></label><label>アク禁メール↓<br><textarea rows="5" cols="56" name="autoAku_mails" placeholder="改行区切りで入力"></textarea><br></label><label>アク禁id↓<br><textarea rows="5" cols="56" name="autoAku_ids" placeholder="改行区切りで入力"></textarea><br></label></fieldset><fieldset><legend>その他の設定</legend>投稿間隔でアク禁:<label><input type="radio" name="enableSpeedLimit" value="OFF" checked>OFF</label><label><input type="radio" name="enableSpeedLimit" value="ON">ON</label><span id="field_speedLimit" style="display:none;"><input type="number" size="10" min="1" max="3600" name="autoAku_speedLimit" value="1">秒未満の間隔で投稿するidをアク禁する</span><br>正規表現を利用する:<label><input type="radio" name="enableRegexp" value="OFF" checked>OFF</label><label><input type="radio" name="enableRegexp" value="ON">ON</label><br>アク禁を実行する :<label><input type="radio" name="enableAkukin" value="OFF" checked>OFF</label><label><input type="radio" name="enableAkukin" value="ON">ON</label></fieldset><input type="button" id="b_autoAkuSave" name="b_autoAkuSave" value="アク禁設定を保存する"><span id="autoAku_message" style="color:red;"></span>';
document.body.appendChild(section);
section.appendChild(form);
}
function _setupForm() {
let form = document.forms["fm_autoAku"];
let fd = new FormData(form);
let temp = {};
let storage;
for (let [key, value] of fd) {
storage = _getStorage(key);
temp[key] = storage;
form[key].value = storage;
}
_validateForm(temp)
.then(_setInternalValue);
}
function _MutationCallback(record) {
let dl = record[0].addedNodes[0];
//note: dlの中にdt, ddは1組以上含まれる可能性がある
_reshead.set(dl.querySelectorAll("dt"));
_resbody.set(dl.querySelectorAll("dd"));
_res.set(_reshead.parse(), _resbody.parse());
let res = _res.get();
let judge = [];
let isTarget = false;
let target = [];
let limit = _form["limit"] * 1000;
let enableSpeedLimit = _form["enableSpeedLimit"] === "ON" ? true : false;
for (let value of res) {
judge = [];
isTarget = false;
if (!value.nusi) {
judge.push(_form["word"].some((elm) => elm.test(value.text)));
judge.push(_form["name"].some((elm) => elm.test(value.name)));
judge.push(_form["mail"].some((elm) => elm.test(value.mail)));
judge.push(_form["id"].some((elm) => elm.test(value.id)));
isTarget = judge.some((elm) => elm);
if (isTarget) {
target.push(value.resnum);
}
if (enableSpeedLimit) {
if (Date.now() - _idList[value.id] < limit) {
target.push(value.resnum);
}
_idList[value.id] = Date.now();
}
}
}
target = target.filter((elm, ind, ary) => ary.indexOf(elm) === ind);
target = target.map((elm) => "!aku" + elm);
if (target.length) {
_agent.set(target);
_agent.exec();
}
}
/* Event Listener */
function ShowBtnEventListener() {
let elm = document.querySelector("#b_autoAkuShow");
elm.addEventListener("click", function(ev) {
let form = document.forms["fm_autoAku"];
let style = form.getAttribute("style");
if (style.includes("none")) {
form.setAttribute("style", "display:block;");
elm.value = "開じる";
} else {
form.setAttribute("style", "display:none;");
elm.value = "開く";
}
});
}
function SaveBtnEventListener() {
let elm = document.querySelector("#b_autoAkuSave");
elm.addEventListener("click", function(ev) {
let form = ev.target.form;
let fd = new FormData(form);
let temp = {};
for (let [key, value] of fd) {
temp[key] = value;
}
let message = document.querySelector("#autoAku_message");
_validateForm(temp)
.then(function(result) {
_setInternalValue(result);
fd.set("autoAku_speedLimit", result[5]);
for (let [key, val] of fd) {
_setStorage(key, val);
}
if (temp["enableAkukin"] === "ON") {
_mo.start();
} else {
_mo.stop();
}
_agent.update(_form["autoAku_defaultText"], _form["autoAku_defaultName"], _form["autoAku_defaultMail"]);
message.textContent = "保存しますた!";
setTimeout(() => { message.textContent = "" }, 1000);
}, function(reason) {
alert(reason);
message.textContent = "保存してません!";
setTimeout(() => { message.textContent = "" }, 1000);
});
});
}
function _validateForm(data) {
return new Promise(function(resolve, reject) {
let word, name, mail, id, limit;
let words = data["autoAku_words"];
let names = data["autoAku_names"];
let mails = data["autoAku_mails"];
let ids = data["autoAku_ids"];
let arr_words = _splitt(words).filter((elm) => elm !== "");
let arr_names = _splitt(names).filter((elm) => elm !== "");
let arr_mails = _splitt(mails).filter((elm) => elm !== "");
let arr_ids = _splitt(ids).filter((elm) => elm !== "");
if (data["enableRegexp"] === "ON") {
try {
word = arr_words.map((elm) => new RegExp(elm, "m"));
name = arr_names.map((elm) => new RegExp(elm, "m"));
mail = arr_mails.map((elm) => new RegExp(elm, "m"));
id = arr_ids.map((elm) => new RegExp(elm, "m"));
} catch (e) {
let message = "正規表現の指定がおかしいみたいです。。\n↓\n↓\n" + e.message;
reject(message);
return;
}
} else {
word = arr_words.map((elm) => new RegExp(_escapeRegexp(elm), "m"));
name = arr_names.map((elm) => new RegExp(_escapeRegexp(elm), "m"));
mail = arr_mails.map((elm) => new RegExp(_escapeRegexp(elm), "m"));
id = arr_ids.map((elm) => new RegExp(_escapeRegexp(elm), "m"));
}
if (data["enableSpeedLimit"] === "ON") {
limit = data["autoAku_speedLimit"] - 0;
limit = isNaN(limit) ? 1 : limit;
limit = limit >= 1 ? limit : 1;
} else {
limit = 1;
}
resolve([data, word, name, mail, id, limit]);
return;
});
}
function _setInternalValue([temp, word, name, mail, id, limit]) {
_form["word"] = word;
_form["name"] = name;
_form["mail"] = mail;
_form["id"] = id;
_form["limit"] = limit;
_form["enableAkukin"] = temp["enableAkukin"];
_form["enableRegexp"] = temp["enableRegexp"];
_form["enableSpeedLimit"] = temp["enableSpeedLimit"];
_form["autoAku_defaultName"] = temp["autoAku_defaultName"];
_form["autoAku_defaultMail"] = temp["autoAku_defaultMail"];
_form["autoAku_defaultText"] = temp["autoAku_defaultText"];
if (_form["enableSpeedLimit"] === "ON") {
let target = document.querySelector("#field_speedLimit");
target.setAttribute("style", "display:inline;");
}
}
function _splitt(value) {
return value.split("\r\n");
}
function _escapeRegexp(str) {
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1");
}
function DeleteBtnEventListener() {
let elm = document.querySelector("#b_autoAkuDelete");
elm.addEventListener("click", function(ev) {
if (confirm("消しますか?")) {
let form = document.forms["fm_autoAku"];
let fd = new FormData(form);
for (let [key, value] of fd) {
_removeStorage(key);
}
form.reset();
_mo.stop();
let target = document.querySelector("#field_speedLimit");
target.setAttribute("style", "display:none;");
}
});
}
function SpeedLimitRadioBtnEventListener() {
let limit_on = document.querySelector("input[type='radio'][name='enableSpeedLimit'][value='ON']");
let limit_off = document.querySelector("input[type='radio'][name='enableSpeedLimit'][value='OFF']");
let target = document.querySelector("#field_speedLimit");
limit_on.addEventListener("click", function(ev) {
target.setAttribute("style", "display:inline;");
});
limit_off.addEventListener("click", function(ev) {
target.setAttribute("style", "display:none;");
});
}
/* wrapper function */
function _getStorage(key) {
return localStorage.getItem(key);
}
function _setStorage(key, val) {
localStorage.setItem(key, val);
}
function _removeStorage(key) {
localStorage.removeItem(key);
}
return;
})();