From 16f506b0f5ce2c250de021e5fcfb8b12450ebe7c Mon Sep 17 00:00:00 2001 From: Kebo Date: Sun, 22 Jun 2025 20:54:02 -0500 Subject: [PATCH] feat: youtube + github support & logo gets excited when you're on a supported autofill site --- README.md | 5 ++++- manifest.json | 6 +++++- popup.js | 31 ++++++++++++++++++++++++++----- service-worker.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 service-worker.js diff --git a/README.md b/README.md index 5e2ea05..5bfca67 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,13 @@ This is just a tool I made for myself, but if you want to use it for your own im ## Supported Sites You can technically use this extension on *any* site, as it will always automatically fill in the URL and date accessed fields for you. -However, I've built in additional autofill support for certain websites. +However, I've built in additional autofill support for certain websites. When you visit one of these sites, a badge will appear on the +moffsoft icon to indicate that additional autofill is available. +- GitHub: Title and author fields are autofilled from scraping the page. - Medium: Title, author, and date published fields are autofilled from the handy [`NewsArticle` JSON-LD schema](https://schema.org/NewsArticle) included on every page. +- Youtube: Title, author, and date published fields are autofilled from the `watch7-content` metadata. ## Wait, what is this even for? diff --git a/manifest.json b/manifest.json index e688691..7063fb5 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,17 @@ { "name": "Sharefeed Entry Tool", "description": "Creates a sharefeed entry based on the current webpage.", - "version": "1.0", + "version": "1.1", "manifest_version": 3, "action": { "default_icon": "moffsoft_64.png", "default_popup": "popup.html" }, + "background": { + "service_worker": "service-worker.js" + }, "permissions": [ + "tabs", "activeTab", "scripting" ] diff --git a/popup.js b/popup.js index f9133de..85c4270 100644 --- a/popup.js +++ b/popup.js @@ -2,7 +2,8 @@ function parsePage() { let entry = { url: window.location.href, title: "", - publishedDate: "" + publishedDate: "", + author: "", }; // fix the URL's of certain websites @@ -10,17 +11,37 @@ function parsePage() { entry.url = entry.url.replace('https://freedium.cfd/', ''); } - // if schema.org JSON-LD is present, read that data + // if schema.org NewsArticle JSON-LD is present, read that data const jsonLDElem = document.querySelector('script[type="application/ld+json"]'); if (jsonLDElem) { const jsonLDObj = JSON.parse(jsonLDElem.textContent); if (jsonLDObj['@type'] == 'NewsArticle') { - entry.publishedDate = jsonLDObj.datePublished?.slice(0, 19) ?? ""; - entry.author = jsonLDObj.author?.name ?? "" - entry.title = jsonLDObj.headline ?? "" + entry.publishedDate = jsonLDObj.datePublished?.slice(0, 19); + entry.author = jsonLDObj.author?.name + entry.title = jsonLDObj.headline } } + // youtube specific data section + const youtubeMetaElem = document.getElementById('watch7-content'); + if (youtubeMetaElem) { + // url fix + entry.url = document.querySelector('link[itemprop="url"]').getAttribute('href'); + entry.title = document.querySelector('meta[itemprop="name"]').getAttribute('content'); + entry.author = document.querySelector('span[itemprop="author"] link[itemprop="name"]').getAttribute('content'); + entry.publishedDate = document.querySelector('meta[itemprop="datePublished"]').getAttribute('content').slice(0, 19); + } + + // github scraping + const githubIdentElem = document.getElementById('repository-container-header'); + if (githubIdentElem) { + githubRepoName = document.querySelector('strong[itemprop="name"] a').textContent; + githubRepoDesc = document.querySelector('div#responsive-meta-container div p').textContent; + + entry.title = githubRepoName + ' - ' + githubRepoDesc.slice(9, -6); + entry.author = document.querySelector('a[rel="author"]').textContent.slice(9); + } + return entry; } diff --git a/service-worker.js b/service-worker.js new file mode 100644 index 0000000..a3c265e --- /dev/null +++ b/service-worker.js @@ -0,0 +1,31 @@ +chrome.tabs.onActivated.addListener((activeInfo) => { + chrome.tabs.query({ + active: true, + currentWindow: true + }, tabs => { + const supportedUrls = [ + 'medium.com', + 'youtube.com', + 'github.com' + ]; + + let supportedSite = false; + + for (const i in supportedUrls) { + //console.log(supportedUrls[i]); + const pattern = new URLPattern({ hostname: `{*.}?${supportedUrls[i]}` }); + console.log(tabs[0].url); + supportedSite = pattern.test(tabs[0].url); + if (supportedSite) { + break; + } + } + + if (supportedSite) { + chrome.action.setBadgeText({text: ':o'}); + chrome.action.setBadgeBackgroundColor({color: "#8F8"}); + } else { + chrome.action.setBadgeText({}); + } + }); +}); \ No newline at end of file