Improve read autofill contents

优化自动填写表单的获取(掘金
This commit is contained in:
Paul 2024-03-05 15:29:20 +08:00
parent b04b6c7ab8
commit 1a0c8b6687
1 changed files with 57 additions and 23 deletions

View File

@ -1,26 +1,19 @@
const getAuthor = () => {
const metaAuthor = document.querySelector<HTMLMetaElement>(`meta[name="author"]`);
const metaOgAuthor = document.querySelector<HTMLMetaElement>(`meta[property="og:article:author"]`);
const getTitle = () => {
const title = document.title;
if (metaAuthor) {
return metaAuthor.getAttribute("content");
if (title.includes(" - ")) {
const end = title.lastIndexOf(" - ");
return title.substring(0, end);
}
if (metaOgAuthor) {
return metaOgAuthor.getAttribute("content");
if (title.includes(" | ")) {
const end = title.lastIndexOf(" | ");
return title.substring(0, end);
}
return "";
}
const getImage = () => {
const metaOgImage = document.querySelector<HTMLMetaElement>(`meta[property="og:image"]`);
if (metaOgImage) {
return metaOgImage.getAttribute("content");
}
return "";
return title;
}
const getDesc = () => {
@ -43,6 +36,44 @@ const getDesc = () => {
return "";
}
const getFrom = () => {
if (location.host === "mp.weixin.qq.com") {
return "wechat";
}
return "web";
}
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"] meta[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"]`);
@ -66,12 +97,14 @@ const getSiteName = () => {
return "";
}
const getFrom = () => {
if (location.host === "mp.weixin.qq.com") {
return "wechat";
const getTags = () => {
const itemPropKeywords = document.querySelector<HTMLMetaElement>(`meta[itemprop="keywords"]`);
if (itemPropKeywords) {
return itemPropKeywords.getAttribute("content");
}
return "web";
return "";
}
chrome.runtime.onMessage.addListener((req, sender, send) => {
@ -79,13 +112,14 @@ chrome.runtime.onMessage.addListener((req, sender, send) => {
if (req.type === "toolbox:getInfo") {
send({
title: document.title,
title: getTitle(),
link: `${location.origin}${location.pathname}`,
desc: getDesc(),
from: getFrom(),
author: getAuthor(),
image: getImage(),
sitename: getSiteName(),
tags: getTags(),
});
}
});