From 3bb1b3f186fb8d03cb38d9c76327b0371f92c487 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 3 Jan 2024 01:21:03 -0500 Subject: [PATCH] add feature to subscribe to feed --- pkg/extension/.gitignore | 3 ++ pkg/extension/src/scripts/api.js | 35 +++++++++++++++++++ pkg/extension/src/scripts/background.js | 16 +++++++++ .../content/content-listener-script.js | 1 + .../src/scripts/content/prepare-content.js | 33 +++++++++++++++++ 5 files changed, 88 insertions(+) create mode 100644 pkg/extension/.gitignore diff --git a/pkg/extension/.gitignore b/pkg/extension/.gitignore new file mode 100644 index 0000000000..fb5863cb53 --- /dev/null +++ b/pkg/extension/.gitignore @@ -0,0 +1,3 @@ +# make artifacts +*.zip + diff --git a/pkg/extension/src/scripts/api.js b/pkg/extension/src/scripts/api.js index 48465ca9de..898d102714 100644 --- a/pkg/extension/src/scripts/api.js +++ b/pkg/extension/src/scripts/api.js @@ -346,3 +346,38 @@ async function deleteItem(apiUrl, pageId) { } return data.setBookmarkArticle.bookmarkedArticle } + +async function subscribeFeed(apiUrl, url) { + const mutation = JSON.stringify({ + query: `mutation Subscribe($input: SubscribeInput!) { + subscribe(input: $input) { + ... on SubscribeSuccess { + subscriptions { + id + } + } + ... on SubscribeError{ + errorCodes + } + } + } + `, + variables: { + input: { + subscriptionType: 'RSS', + url: url, + }, + }, + }) + + const data = await gqlRequest(apiUrl, mutation) + if ( + !data.subscribe || + data.subscribe['errorCodes'] || + !data.subscribe.subscriptions + ) { + console.log('GQL Error subscribing to Feed:', data) + throw new Error('Error subscribing to Feed.') + } + return data.subscribe.subscriptions +} diff --git a/pkg/extension/src/scripts/background.js b/pkg/extension/src/scripts/background.js index ebc26ac27d..bfdbae3efc 100644 --- a/pkg/extension/src/scripts/background.js +++ b/pkg/extension/src/scripts/background.js @@ -14,6 +14,7 @@ import { v4 as uuidv4 } from 'uuid' import { nanoid } from 'nanoid' +// Background Script: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Background_scripts class TaskQueue { constructor() { this.queue = [] @@ -199,6 +200,17 @@ async function savePdfFile( return uploadFileResult } +async function createFeedSubscription(url) { + subscribeFeed( + omnivoreGraphqlURL + 'graphql', + url + ).then((subscriptions) => { + console.log('create feed subscription succeeded', subscriptions) + }).catch((err) => { + console.log('create feed subscription failed', err) + }) +} + function clearClickCompleteState() { getStorageItem('postInstallClickComplete').then( (postInstallClickComplete) => { @@ -495,6 +507,10 @@ async function saveArticle(tab, createHighlight) { } break } + case 'feed': { + await createFeedSubscription(encodeURI(tab.url)) + break + } } } ) diff --git a/pkg/extension/src/scripts/content/content-listener-script.js b/pkg/extension/src/scripts/content/content-listener-script.js index fe0a377623..a9b7418f62 100644 --- a/pkg/extension/src/scripts/content/content-listener-script.js +++ b/pkg/extension/src/scripts/content/content-listener-script.js @@ -7,6 +7,7 @@ ACTIONS */ +// Content Script: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts 'use strict' ;(function () { const darkModeQuery = window.matchMedia('(prefers-color-scheme: dark)') diff --git a/pkg/extension/src/scripts/content/prepare-content.js b/pkg/extension/src/scripts/content/prepare-content.js index 01dc089fa4..1cdd0d3421 100644 --- a/pkg/extension/src/scripts/content/prepare-content.js +++ b/pkg/extension/src/scripts/content/prepare-content.js @@ -62,6 +62,28 @@ }) } + /** + * Determines whether a page is a feed + * Currently only feed pages of the XML family and selected feed format in it are recognized (i.e. RSS or Atom) + * + * @returns true if it's feed page, false otherwise + */ + function isFeedPage() { + const fileExtension = window.location.pathname.slice(-4).toLowerCase() + const hasXMLExtension = fileExtension === '.xml' + const xmlContentTypes = [ + 'application/xml', + 'text/xml' + ] + const isXMLContent = hasXMLExtension || xmlContentTypes.indexOf(document.contentType) !== -1 + const feedElementNames = [ + 'rss', + 'feed' + ] + const isSupportedFeed = feedElementNames.indexOf(document.documentElement.nodeName) !== -1 + return isXMLContent && isSupportedFeed + } + function prepareContentPostItem(itemEl) { const lowerTagName = itemEl.tagName.toLowerCase() @@ -271,11 +293,22 @@ }, 0.5e3) } + /** + * Return the type and content of given page + * + * @param {*} createHighlight + * @returns type and content + */ async function prepareContent(createHighlight) { const pdfContent = await grabPdfContent() if (pdfContent) { return pdfContent } + + if (isFeedPage()) { + return { type: 'feed' } + } + const url = window.location.href try { if (!createHighlight && handleBackendUrl(url)) {