comparison 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
comparison
equal deleted inserted replaced
9:fcd65cf3f634 10:ff5e5e3eba32
1 /*
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name>
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9 'use strict';
10
11 function updateFeedReaders(feedReaders) {
12 let feedReaderSelectionElement = document.forms['feed-subscription']
13 .elements['feed-reader-selection'];
14 for (let optionElement of
15 feedReaderSelectionElement.querySelectorAll('option')) {
16 optionElement.remove();
17 }
18
19 for (let feedReader of feedReaders) {
20 let optionElement = document.createElement('option');
21 optionElement.value = feedReader.urlTemplate;
22 optionElement.textContent = feedReader.title;
23 feedReaderSelectionElement.append(optionElement);
24 }
25
26 document.forms['feed-subscription'].elements['main'].disabled =
27 feedReaders.length === 0;
28 }
29
30 document.addEventListener('submit', ev => {
31 if (ev.target.id !== 'feed-subscription') {
32 return;
33 }
34
35 ev.preventDefault();
36 let subscribeUrl = ev.target.elements['feed-reader-selection'].value
37 .replace('%s', encodeURIComponent(document.documentURI));
38 console.log(`subscribing to feed using ${subscribeUrl}`);
39 window.location.href = subscribeUrl;
40 });
41
42 function onStorageChanged(changes, areaName) {
43 if (areaName !== 'sync' || changes.feedReaders === 'undefined') {
44 return;
45 }
46
47 // stored feed readers have been changed or deleted
48 let feedReaders = typeof changes.feedReaders.newValue !== 'undefined' ?
49 changes.feedReaders.newValue : [];
50 console.log('feedReaders changed to', feedReaders);
51 updateFeedReaders(feedReaders);
52 }
53
54 (async () => {
55 // initialize subscription form
56 let {feedReaders = []} = await browser.storage.sync.get('feedReaders');
57 updateFeedReaders(feedReaders);
58
59 browser.storage.onChanged.addListener(onStorageChanged);
60 })();