function parsePage() { let entry = { url: window.location.href, title: "", publishedDate: "", author: "", }; // fix the URL's of certain websites if (entry.url.startsWith('https://freedium.cfd/')) { // extract original article link from freedium url entry.url = entry.url.replace('https://freedium.cfd/', ''); } // if schema.org 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') { // medium, etc. entry.publishedDate = jsonLDObj.datePublished.slice(0, 19); entry.author = jsonLDObj.author.name ?? jsonLDObj.author[0].name; entry.title = jsonLDObj.headline; } if (jsonLDObj['@type'] == 'VideoObject') { // youtube, etc. entry.url = jsonLDObj['@id']; // url fix entry.publishedDate = jsonLDObj.uploadDate.slice(0, 19); entry.author = jsonLDObj.author; entry.title = jsonLDObj.name; } } // 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); } // forgejo meta elements const forgejoIdentElem = document.querySelector('meta[name="keywords"]'); if (forgejoIdentElem && forgejoIdentElem.content.includes('forgejo')) { entry.title = document.querySelector('meta[name="description"]').content; entry.author = document.querySelector('meta[name="author"]').content; } return entry; } // thanks stack overflow: https://stackoverflow.com/questions/12413243/javascript-date-format-like-iso-but-local function dateToISOLikeButLocal(date) { const offsetMs = date.getTimezoneOffset() * 60 * 1000; const msLocal = date.getTime() - offsetMs; const dateLocal = new Date(msLocal); const iso = dateLocal.toISOString(); const isoLocal = iso.slice(0, 19); return isoLocal; } function fillForm(entry) { const curDate = new Date(); document.getElementById('url').value = entry.url; document.getElementById('title').value = entry.title ?? ""; document.getElementById('author').value = entry.author ?? ""; document.getElementById('publishedDate').value = entry.publishedDate ?? ""; document.getElementById('accessedDate').value = dateToISOLikeButLocal(curDate); } function runContentScript(tabId) { chrome.scripting.executeScript({ target: { tabId: tabId}, func: parsePage, }).then((injectionResults) => { fillForm(injectionResults[0].result); }); } function generateJSON(event) { event.preventDefault(); const urlField = document.getElementById('url'); const titleField = document.getElementById('title'); const authorField = document.getElementById('author'); const publishedDateField = document.getElementById('publishedDate'); const accessedDateField = document.getElementById('accessedDate'); const noteField = document.getElementById('note'); const errorElem = document.getElementById('error'); const JSONElem = document.getElementById('generated'); const fixedNote = noteField.value.replace('"', '\\"'); // first, make sure all of our required fields are in place if ((urlField.value == null || urlField.value == "") || (titleField.value == null || titleField.value == "") || (accessedDateField.value == null || accessedDateField.value == "") || (noteField.value == null || noteField.value == "")) { errorElem.textContent = "ERROR: Missing required field." return; } errorElem.textContent = "" JSONElem.value = `{ "url": "${urlField.value}", "title": "${titleField.value}", "author": "${authorField.value}", "publishedDate": "${publishedDateField.value.replace('T', ' ')}", "accessedDate": "${accessedDateField.value.replace('T', ' ')}", "note": "${fixedNote}"\n},` } window.addEventListener('DOMContentLoaded', () => { chrome.tabs.query({ active: true, currentWindow: true }, tabs => { runContentScript(tabs[0].id); }); const form = document.getElementById('sharefeed') form.addEventListener('submit', generateJSON); });