Home-Toolbox-Plugin/contents/read.ts

130 lines
3.2 KiB
TypeScript

const getTitle = () => {
const title = document.title;
const metaTitle = document.querySelector<HTMLMetaElement>(`meta[name="title"]`);
const metaOgTitle = document.querySelector<HTMLMetaElement>(`meta[name="og:title"]`);
if (metaOgTitle) {
return metaOgTitle.getAttribute("content");;
}
if (metaTitle) {
return metaTitle.getAttribute("content");;
}
if (title.includes(" - ")) {
const end = title.lastIndexOf(" - ");
return title.substring(0, end);
}
if (title.includes(" | ")) {
const end = title.lastIndexOf(" | ");
return title.substring(0, end);
}
return title;
}
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 getAuthor = () => {
const metaAuthor = document.querySelector<HTMLMetaElement>(`meta[name="author"]`);
const metaOgAuthor = document.querySelector<HTMLMetaElement>(`meta[property="og:article:author"]`);
const itemPropAuthor = document.querySelector<HTMLMetaElement>(`[itemprop="author"] [itemprop="name"]`);
if (metaAuthor) {
return metaAuthor.getAttribute("content");
}
if (metaOgAuthor) {
return metaOgAuthor.getAttribute("content");
}
if (itemPropAuthor) {
return itemPropAuthor.getAttribute("content");
}
return "";
}
const getImage = () => {
const metaOgImage = document.querySelector<HTMLMetaElement>(`meta[property="og:image"]`);
if (metaOgImage) {
return metaOgImage.getAttribute("content");
}
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 getTags = () => {
const metaKeywords = document.querySelector<HTMLMetaElement>(`meta[name="keywords"]`);
const itemPropKeywords = document.querySelector<HTMLMetaElement>(`meta[itemprop="keywords"]`);
if (metaKeywords) {
return metaKeywords.getAttribute("content");
}
if (itemPropKeywords) {
return itemPropKeywords.getAttribute("content");
}
return "";
}
chrome.runtime.onMessage.addListener((req, sender, send) => {
if (req.type === "toolbox:getInfo") {
send({
title: getTitle(),
link: `${location.origin}${location.pathname}`,
desc: getDesc(),
author: getAuthor(),
image: getImage(),
sitename: getSiteName(),
tags: getTags(),
});
}
});