This commit is contained in:
2024-03-09 15:57:54 +08:00
parent c8e7af5f16
commit 82948a0659

View File

@@ -1,7 +1,7 @@
// ==UserScript== // ==UserScript==
// @name EH Gallery Script // @name EH Gallery Script
// @namespace https://github.com/lifegpc/userscript // @namespace https://github.com/lifegpc/userscript
// @version 0.1.10 // @version 0.1.11
// @description :( // @description :(
// @author lifegpc // @author lifegpc
// @match https://*.e-hentai.org/g/*/* // @match https://*.e-hentai.org/g/*/*
@@ -65,6 +65,8 @@ let db = undefined;
let need_reinit = false; let need_reinit = false;
let storage = navigator.storage || globalThis['WorkerNavigator']['storage']; let storage = navigator.storage || globalThis['WorkerNavigator']['storage'];
const BLACK_LIST_HOST = [ const BLACK_LIST_HOST = [
"gss2.bdstatic.com",
"8.blog.xuite.net",
]; ];
const REFERER_NEED_HOST = { const REFERER_NEED_HOST = {
"tva1.sinaimg.cn": "https://www.weibo.com", "tva1.sinaimg.cn": "https://www.weibo.com",
@@ -102,7 +104,7 @@ async function fetch_image(url, referer) {
} }
return res; return res;
} }
async function filter_html(html) { async function filter_html(html, tag) {
let doc = (new DOMParser).parseFromString(html, "text/html"); let doc = (new DOMParser).parseFromString(html, "text/html");
doc.querySelectorAll('a').forEach((a) => { doc.querySelectorAll('a').forEach((a) => {
a.target = "_blank"; a.target = "_blank";
@@ -124,14 +126,18 @@ async function filter_html(html) {
if (REFERER_NEED_HOST[u.host]) { if (REFERER_NEED_HOST[u.host]) {
const loadRefererImages = GM_config.get("loadRefererImages"); const loadRefererImages = GM_config.get("loadRefererImages");
if (loadRefererImages == "remove") { if (loadRefererImages == "remove") {
console.log(tag);
console.log("Remove referer needed URL: ", u.toString()); console.log("Remove referer needed URL: ", u.toString());
img.remove(); img.remove();
return; return;
} else { } else {
const re = await fetch_image(img.src, REFERER_NEED_HOST[u.host]); const re = await fetch_image(img.src, REFERER_NEED_HOST[u.host]);
if (re.status == 200) { if (re.status == 200) {
if (!img.title) img.title = img.src;
img.setAttribute('origin_src', img.src);
img.src = URL.createObjectURL(await re.blob()); img.src = URL.createObjectURL(await re.blob());
} else { } else {
console.log(tag);
console.error("Failed to fetch image: ", re.status, re.statusText); console.error("Failed to fetch image: ", re.status, re.statusText);
img.remove() img.remove()
return; return;
@@ -139,6 +145,7 @@ async function filter_html(html) {
} }
} }
if (BLACK_LIST_HOST.includes(u.host)) { if (BLACK_LIST_HOST.includes(u.host)) {
console.log(tag);
console.log("Remove blacklist URL: ", u.toString()); console.log("Remove blacklist URL: ", u.toString());
img.remove(); img.remove();
return; return;
@@ -149,7 +156,7 @@ async function filter_html(html) {
}) })
return doc.body.innerHTML; return doc.body.innerHTML;
} }
function filter_html2(html) { async function filter_html2(html, tag) {
let doc = (new DOMParser).parseFromString(html, "text/html"); let doc = (new DOMParser).parseFromString(html, "text/html");
doc.querySelectorAll('img[src="#"]').forEach((img) => { doc.querySelectorAll('img[src="#"]').forEach((img) => {
let title = img.title; let title = img.title;
@@ -161,7 +168,37 @@ function filter_html2(html) {
img.remove(); img.remove();
} }
}) })
doc.querySelectorAll('img').forEach((img) => { await asyncForEach(doc.querySelectorAll('img'), async (img) => {
try {
let u = new URL(img.src);
if (REFERER_NEED_HOST[u.host]) {
const loadRefererImages = GM_config.get("loadRefererImages");
if (loadRefererImages == "remove") {
console.log(tag);
console.log("Remove referer needed URL: ", u.toString());
img.remove();
return;
} else {
const re = await fetch_image(img.src, REFERER_NEED_HOST[u.host]);
if (re.status == 200) {
if (!img.title) img.title = img.src;
img.setAttribute('origin_src', img.src);
img.src = URL.createObjectURL(await re.blob());
} else {
console.log(tag);
console.error("Failed to fetch image: ", re.status, re.statusText);
img.remove()
return;
}
}
}
if (BLACK_LIST_HOST.includes(u.host)) {
console.log(tag);
console.log("Remove blacklist URL: ", u.toString());
img.remove();
return;
}
} catch (_) { }
img.style.height = "12px"; img.style.height = "12px";
img.referrerPolicy = "no-referrer"; img.referrerPolicy = "no-referrer";
}) })
@@ -467,7 +504,7 @@ let observer = new MutationObserver(async (data) => {
} }
if (html) { if (html) {
e.setAttribute("tippy-id", set_instance(tippy(e, { e.setAttribute("tippy-id", set_instance(tippy(e, {
content: await filter_html(html), content: await filter_html(html, { t, value }),
allowHTML: true, allowHTML: true,
interactive: true, interactive: true,
theme: 'light-border', theme: 'light-border',
@@ -505,7 +542,7 @@ let observer = new MutationObserver(async (data) => {
const value = await get_tag(t); const value = await get_tag(t);
e.setAttribute('otag', t); e.setAttribute('otag', t);
if (value) { if (value) {
const name = filter_html2(replaceAll(marked.parse(value.name), /<\/?p>/, '')); const name = await filter_html2(replaceAll(marked.parse(value.name), /<\/?p>/, ''), {t, value});
if (e.id) { if (e.id) {
e.children[0].innerHTML = name; e.children[0].innerHTML = name;
} else { } else {
@@ -520,7 +557,7 @@ let observer = new MutationObserver(async (data) => {
} }
if (html) { if (html) {
e.setAttribute("tippy-id", set_instance(tippy(e, { e.setAttribute("tippy-id", set_instance(tippy(e, {
content: await filter_html(html), content: await filter_html(html, { t, value }),
allowHTML: true, allowHTML: true,
interactive: true, interactive: true,
theme: 'light-border', theme: 'light-border',
@@ -602,7 +639,7 @@ async function handle_tags() {
} }
if (html) { if (html) {
group.setAttribute("tippy-id", set_instance(tippy(group, { group.setAttribute("tippy-id", set_instance(tippy(group, {
content: await filter_html(html), content: await filter_html(html, { t, value }),
allowHTML: true, allowHTML: true,
interactive: true, interactive: true,
theme: 'light-border', theme: 'light-border',
@@ -636,7 +673,7 @@ async function handle_tags() {
const value = await get_tag(t); const value = await get_tag(t);
i.setAttribute('otag', t); i.setAttribute('otag', t);
if (value) { if (value) {
const name = filter_html2(replaceAll(marked.parse(value.name), /<\/?p>/, '')); const name = await filter_html2(replaceAll(marked.parse(value.name), /<\/?p>/, ''), {t, value});
if (i.id) { if (i.id) {
i.children[0].innerHTML = name; i.children[0].innerHTML = name;
} else { } else {
@@ -651,7 +688,7 @@ async function handle_tags() {
} }
if (html) { if (html) {
i.setAttribute("tippy-id", set_instance(tippy(i, { i.setAttribute("tippy-id", set_instance(tippy(i, {
content: await filter_html(html), content: await filter_html(html, { t, value }),
allowHTML: true, allowHTML: true,
interactive: true, interactive: true,
theme: 'light-border', theme: 'light-border',