diff content_scripts/feed-readers.js @ 10:ff5e5e3eba32

Implement feed subscription for web-based feed readers Add options page for configuring web-based feed readers which allow for subscribing to feeds via GET requests. Track tabs containing feed previews and inject a content script which retrieves the configured feed readers and keeps them in sync.
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Fri, 07 Dec 2018 23:00:41 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/content_scripts/feed-readers.js	Fri Dec 07 23:00:41 2018 +0100
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name>
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+'use strict';
+
+function updateFeedReaders(feedReaders) {
+    let feedReaderSelectionElement = document.forms['feed-subscription']
+            .elements['feed-reader-selection'];
+    for (let optionElement of
+            feedReaderSelectionElement.querySelectorAll('option')) {
+        optionElement.remove();
+    }
+
+    for (let feedReader of feedReaders) {
+        let optionElement = document.createElement('option');
+        optionElement.value = feedReader.urlTemplate;
+        optionElement.textContent = feedReader.title;
+        feedReaderSelectionElement.append(optionElement);
+    }
+
+    document.forms['feed-subscription'].elements['main'].disabled =
+            feedReaders.length === 0;
+}
+
+document.addEventListener('submit', ev => {
+    if (ev.target.id !== 'feed-subscription') {
+        return;
+    }
+
+    ev.preventDefault();
+    let subscribeUrl = ev.target.elements['feed-reader-selection'].value
+            .replace('%s', encodeURIComponent(document.documentURI));
+    console.log(`subscribing to feed using ${subscribeUrl}`);
+    window.location.href = subscribeUrl;
+});
+
+function onStorageChanged(changes, areaName) {
+    if (areaName !== 'sync' || changes.feedReaders === 'undefined') {
+        return;
+    }
+
+    // stored feed readers have been changed or deleted
+    let feedReaders = typeof changes.feedReaders.newValue !== 'undefined' ?
+            changes.feedReaders.newValue : [];
+    console.log('feedReaders changed to', feedReaders);
+    updateFeedReaders(feedReaders);
+}
+
+(async () => {
+    // initialize subscription form
+    let {feedReaders = []} = await browser.storage.sync.get('feedReaders');
+    updateFeedReaders(feedReaders);
+
+    browser.storage.onChanged.addListener(onStorageChanged);
+})();