一覧処理に、自動しおり対策も入れてみる
というか、単純に面倒なので一覧処理に自動栞対策を追加する。
manifest.json
----------
{
"name": "narouListModifier",
"version": "1.0.0",
"manifest_version": 3,
"description": "なろうのBMから最新まで読んだものを非表示にする",
"host_permissions":[
"https://syosetu.com/*"
],
"content_scripts": [{
"run_at": "document_end",
"matches": ["https://syosetu.com/favnovelmain/list/*"],
"js": [
"content.js"
]
},{
"run_at": "document_start",
"matches": ["https://ncode.syosetu.com/*"],
"js": [
"bookmark.js"
]
}]
}
--------
bookmark.jsで自動栞対策を行う。
contents.jsは前回の通り。
bookmark.js
---------
document.addEventListener("DOMContentLoaded", function() {
const sioris = document.body.querySelectorAll("input[name=auto_siori]");
sioris.forEach((x) => {
x.setAttribute("name", "auto_siori_x");
});
if (sioris.length > 0) {
const no = sioris[0].attributes["data-no"].value - 0;
const favno = sioris[0].attributes["data-favno"].value - 0;
if (no == favno + 1) {
const url = sioris[0].attributes["data-url"].value;
const primary = sioris[0].attributes["data-primary"].value;
const token = sioris[0].attributes["data-token"].value;
const targetElement = document.querySelectorAll("div.novel_bn")[1];
if (targetElement) {
const callback = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// TODO
try {
const formData = new FormData();
formData.append('no', no);
formData.append('primary', primary);
formData.append('token', token);
fetch(url, {
method: 'POST',
body: formData,
credentials: "include"
})
.then(response => {
return response.json();
})
.then(data => {
if (data.error.code) {
alert("しおりの更新に失敗しました。")
}
else {
document.querySelector(".js-siori").classList.add("is-active");
}
})
.catch (error => {
alert("しおりの更新に失敗しました。")
});
}
catch (error) {
alert("しおりの更新に失敗しました。");
}
finally {
observer.disconnect();
}
}
});
};
const options = {
root: null,
rootMargin: '0px',
threshold: 1.0
};
const observer = new IntersectionObserver(callback, options);
observer.observe(targetElement);
}
}
}
});
--------
やっぱり、jQuery楽だよなあ、と思う古い人間でした。




