const getTitle = () => { const title = document.title; const metaTitle = document.querySelector(`meta[name="title"]`); const metaOgTitle = document.querySelector(`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("article p") || document.querySelector("p"); const metaDescription = document.querySelector(`meta[name="description"]`); const metaOgDescription = document.querySelector(`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(`meta[name="author"]`); const metaOgAuthor = document.querySelector(`meta[property="og:article:author"]`); const itemPropAuthor = document.querySelector(`[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(`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"]`); 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(`meta[name="keywords"]`); const itemPropKeywords = document.querySelector(`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(), }); } });