Add Paul's Toolbox Contents

This commit is contained in:
2024-03-01 18:33:29 +08:00
parent 65b2b86b78
commit 3ef694a468
28 changed files with 1534 additions and 366 deletions
+44
View File
@@ -0,0 +1,44 @@
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") {
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,
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,
});
}
else {
send(false);
}
}
});
+91
View File
@@ -0,0 +1,91 @@
const getAuthor = () => {
const metaAuthor = document.querySelector<HTMLMetaElement>(`meta[name="author"]`);
const metaOgAuthor = document.querySelector<HTMLMetaElement>(`meta[property="og:article:author"]`);
if (metaAuthor) {
return metaAuthor.getAttribute("content");
}
if (metaOgAuthor) {
return metaOgAuthor.getAttribute("content");
}
return "";
}
const getImage = () => {
const metaOgImage = document.querySelector<HTMLMetaElement>(`meta[property="og:image"]`);
if (metaOgImage) {
return metaOgImage.getAttribute("content");
}
return "";
}
const getDesc = () => {
const articleFirstParagraph = document.querySelector<HTMLParagraphElement>("article p") || document.querySelector<HTMLParagraphElement>("p");
const metaDescription = document.querySelector<HTMLMetaElement>(`meta[name="description"]`);
const metaOgDescription = document.querySelector<HTMLMetaElement>(`meta[property="og:description"]`);
if (metaOgDescription) {
return metaOgDescription.getAttribute("content");
}
if (metaDescription) {
return metaDescription.getAttribute("content");
}
if (articleFirstParagraph) {
return articleFirstParagraph.innerText;
}
return "";
}
const getSiteName = () => {
const title = document.title;
const metaSiteName = document.querySelector<HTMLMetaElement>(`meta[property="og:site_name"]`);
if (metaSiteName) {
return metaSiteName.getAttribute("content");
}
if (title.includes(" - ")) {
const start = title.lastIndexOf(" - ") + 3;
return title.substring(start);
}
if (title.includes(" | ")) {
const start = title.lastIndexOf(" | ") + 3;
return title.substring(start);
}
return "";
}
const getFrom = () => {
if (location.host === "mp.weixin.qq.com") {
return "wechat";
}
return "web";
}
chrome.runtime.onMessage.addListener((req, sender, send) => {
console.log(req);
if (req.type === "toolbox:getInfo") {
send({
title: document.title,
link: `${location.origin}${location.pathname}`,
desc: getDesc(),
from: getFrom(),
author: getAuthor(),
image: getImage(),
sitename: getSiteName(),
});
}
});