diff --git a/contents/read.ts b/contents/read.ts index f32a112..689c16d 100644 --- a/contents/read.ts +++ b/contents/read.ts @@ -1,26 +1,19 @@ -const getAuthor = () => { - const metaAuthor = document.querySelector(`meta[name="author"]`); - const metaOgAuthor = document.querySelector(`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(`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(`meta[name="author"]`); + const metaOgAuthor = document.querySelector(`meta[property="og:article:author"]`); + const itemPropAuthor = document.querySelector(`[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(`meta[property="og:image"]`); + + if (metaOgImage) { + return metaOgImage.getAttribute("content"); + } + + return ""; +} + const getSiteName = () => { const title = document.title; const metaSiteName = document.querySelector(`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(`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(), }); } });