sharefeed-chrome/popup.js

99 lines
3.5 KiB
JavaScript

function parsePage() {
let entry = {
url: window.location.href,
title: "",
publishedDate: ""
};
// 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') {
entry.publishedDate = jsonLDObj.datePublished?.slice(0, 19) ?? "";
entry.author = jsonLDObj.author?.name ?? ""
entry.title = jsonLDObj.headline ?? ""
}
}
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');
// 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": "${noteField.value}",\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);
});