Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add browser extension feature to subscribe to feed #3305

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/extension/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# make artifacts
*.zip

35 changes: 35 additions & 0 deletions pkg/extension/src/scripts/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 16 additions & 0 deletions pkg/extension/src/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -495,6 +507,10 @@ async function saveArticle(tab, createHighlight) {
}
break
}
case 'feed': {
await createFeedSubscription(encodeURI(tab.url))
break
}
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)')
Expand Down
33 changes: 33 additions & 0 deletions pkg/extension/src/scripts/content/prepare-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we have a few other types we handle here /cc @sywhb

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, i think .rss and .html could belong to here too

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah the problem with HTML though is then it wont work for nomal pages, so maybe we have to just be conservative and handle XML here, even though a lot of sites return it as HTML.

'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()

Expand Down Expand Up @@ -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)) {
Expand Down