annotate content_scripts/feed-readers.js @ 54:ede87e1004f9

Fix issues with feed detection Query the feed probe content script for available feeds from the background script instead of making the content script message the background script. This solves a race condition between the message from the content script sending any feeds associated with the current document and the tab's status "complete" event signaling that a new document has been loaded and hiding the page action. Sometimes that event would be triggered after the message from the content script and thus hide the page action again. In addition, navigating back to a previously visited page might not cause a reload which means that the content script would not send a message if there were feeds associated with the current document.
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Thu, 26 Sep 2019 23:11:18 +0200
parents ff5e5e3eba32
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
1 /*
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
2 * Copyright (C) 2018 Guido Berhoerster <guido+feed-preview@berhoerster.name>
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
3 *
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
4 * This Source Code Form is subject to the terms of the Mozilla Public
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
7 */
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
8
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
9 'use strict';
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
10
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
11 function updateFeedReaders(feedReaders) {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
12 let feedReaderSelectionElement = document.forms['feed-subscription']
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
13 .elements['feed-reader-selection'];
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
14 for (let optionElement of
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
15 feedReaderSelectionElement.querySelectorAll('option')) {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
16 optionElement.remove();
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
17 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
18
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
19 for (let feedReader of feedReaders) {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
20 let optionElement = document.createElement('option');
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
21 optionElement.value = feedReader.urlTemplate;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
22 optionElement.textContent = feedReader.title;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
23 feedReaderSelectionElement.append(optionElement);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
24 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
25
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
26 document.forms['feed-subscription'].elements['main'].disabled =
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
27 feedReaders.length === 0;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
28 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
29
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
30 document.addEventListener('submit', ev => {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
31 if (ev.target.id !== 'feed-subscription') {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
32 return;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
33 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
34
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
35 ev.preventDefault();
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
36 let subscribeUrl = ev.target.elements['feed-reader-selection'].value
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
37 .replace('%s', encodeURIComponent(document.documentURI));
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
38 console.log(`subscribing to feed using ${subscribeUrl}`);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
39 window.location.href = subscribeUrl;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
40 });
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
41
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
42 function onStorageChanged(changes, areaName) {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
43 if (areaName !== 'sync' || changes.feedReaders === 'undefined') {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
44 return;
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
45 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
46
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
47 // stored feed readers have been changed or deleted
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
48 let feedReaders = typeof changes.feedReaders.newValue !== 'undefined' ?
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
49 changes.feedReaders.newValue : [];
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
50 console.log('feedReaders changed to', feedReaders);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
51 updateFeedReaders(feedReaders);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
52 }
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
53
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
54 (async () => {
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
55 // initialize subscription form
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
56 let {feedReaders = []} = await browser.storage.sync.get('feedReaders');
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
57 updateFeedReaders(feedReaders);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
58
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
59 browser.storage.onChanged.addListener(onStorageChanged);
ff5e5e3eba32 Implement feed subscription for web-based feed readers
Guido Berhoerster <guido+feed-preview@berhoerster.name>
parents:
diff changeset
60 })();