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