しおりダイアログでの、しおり位置設定を常に可能に
と言う訳で、別スレッドの不具合内容の対処。
ボタンの挿入と、ハンドラの設定を行う。
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/*"],
"world": "MAIN",
"js": [
"bookmark.js"
]
}]
}
-----------
worldの指定を行っている。セキュリティ的とかには、適当ではないのだけれど。
bookmark.js
-----------
document.addEventListener("DOMContentLoaded", function() {
const sioris = document.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;
const url = sioris[0].attributes["data-url"].value;
const primary = sioris[0].attributes["data-primary"].value;
const token = sioris[0].attributes["data-token"].value;
window.mover = () => {
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("しおりの更新に失敗しました。");
}
};
const parent = document.querySelector("div.bookmark-change");
let newButton = document.createElement("button");
newButton.setAttribute("type", "button");
newButton.setAttribute("class", "button button_remodal siori_setbtn");
newButton.setAttribute("onclick", "window.mover();this.style.display='none';")
newButton.innerText = "しおりをここへ移動";
parent.insertBefore(newButton, parent.firstChild);
if (no == favno + 1) {
const targetElement = document.querySelectorAll("div.novel_bn")[1];
if (targetElement) {
const callback = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
// TODO
window.mover();
observer.disconnect();
}
});
};
const options = {
root: null,
rootMargin: '0px',
threshold: 1.0
};
const observer = new IntersectionObserver(callback, options);
observer.observe(targetElement);
}
}
}
});
-----------
まあ、とりあえずは動くみたい。動けば正義。