Home-Toolbox-Plugin/contents/bili.ts

87 lines
2.9 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { PlasmoCSConfig } from "plasmo";
export const config: PlasmoCSConfig = {
matches: ["https://mall.bilibili.com/*"]
};
chrome.runtime.onMessage.addListener((req, sender, send) => {
if (req.type === "toolbox:getBiliToy") {
// 疑似新版?
if (window.location.pathname.includes("neul-next")) {
const item = document.querySelectorAll<HTMLDivElement>(".franky-swiper-item.image-list");
if (!item.length) {
send(false);
return;
}
const project = document.querySelector<HTMLDivElement>(".tagC-ip .tagC-ip-con")?.innerText || "";
const made = document.querySelector<HTMLDivElement>(".tagC-ip .tagC-ip-con-brand")?.innerText || "";
const sale = document.querySelector<HTMLDivElement>(".item-complex:nth-child(4) .item-complex-value")?.innerText || "";
const projectExp = new RegExp(`\s?${project}\s?`);
const madeExp = new RegExp(`\s?${made}\s?`);
const name = document.querySelector<HTMLDivElement>(".clamp-real-text")
.innerText.replace(/\[\S+\]/, "")
.replace(projectExp, "")
.replace(madeExp, "")
.replaceAll(/\s?Q版手办\s?|\s?粘土人\s?/g, "")
.trim();
const images = [];
item.forEach((item) => {
images.push(
item.querySelector("img").src
);
});
send({
name,
project: project.replace(":", ""),
made,
sale: sale.replace("-", "/"),
images,
productId: new URLSearchParams(location.search).get("itemsId"),
});
}
// 旧版,不确定是否还在使用
const item = document.querySelectorAll<HTMLDivElement>(".silde-item");
if (item.length) {
const project = document.querySelector<HTMLDivElement>(".tagC-ip .tagC-ip-con").innerText;
const made = document.querySelector<HTMLDivElement>(".tagC-ip .tagC-ip-con-brand").innerText;
const projectExp = new RegExp(`\s?${project}\s?`);
const madeExp = new RegExp(`\s?${made}\s?`);
const name = document.querySelector<HTMLDivElement>(".title-text-wrap")
.innerText.replace(/\[\S+\]/, "")
.replace(projectExp, "")
.replace(madeExp, "")
.replaceAll(/\s?Q版手办\s?|\s?粘土人\s?/g, "")
.trim();
const images = [];
item.forEach((item) => {
images.push(
item.style.backgroundImage.replace("url(\"", "").replace("\")", "").replace("//", "https://")
);
});
send({
name,
project: project.replace(":", ""),
made: document.querySelector<HTMLDivElement>(".tagC-ip .tagC-ip-con-brand").innerText,
sale: document.querySelector<HTMLDivElement>(".item-complex:nth-child(4) .item-complex-value").innerText.replace("-", "/"),
images,
productId: new URLSearchParams(location.search).get("itemsId"),
});
}
else {
send(false);
}
}
});