comparison content_scripts/feed-probe.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 76e23b361e92
children
comparison
equal deleted inserted replaced
53:7e1a2357b8c5 54:ede87e1004f9
5 * License, v. 2.0. If a copy of the MPL was not distributed with this 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/. 6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */ 7 */
8 8
9 'use strict'; 9 'use strict';
10
11 const TIMEOUT_MAX = 800; // ms
12 10
13 function getFeeds() { 11 function getFeeds() {
14 let urlsFeeds = new Map(); 12 let urlsFeeds = new Map();
15 let elements = document.querySelectorAll(':-moz-any(link, a)[href]' + 13 let elements = document.querySelectorAll(':-moz-any(link, a)[href]' +
16 '[rel~=alternate]:-moz-any([type="application/atom+xml"], ' + 14 '[rel~=alternate]:-moz-any([type="application/atom+xml"], ' +
29 27
30 urlsFeeds.set(element.href, { 28 urlsFeeds.set(element.href, {
31 href: element.href, 29 href: element.href,
32 title: element.title || browser.i18n.getMessage('defaultFeedTitle'), 30 title: element.title || browser.i18n.getMessage('defaultFeedTitle'),
33 type: element.type 31 type: element.type
34 }) 32 });
35 } 33 }
36 34
37 return Array.from(urlsFeeds.values()); 35 return Array.from(urlsFeeds.values());
38 } 36 }
39 37
40 function probeLinkedFeeds() { 38 browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
41 if (document.documentElement.nodeName.toUpperCase() !== 'HTML') { 39 // background page querying available feeds
42 return; 40 sendResponse(getFeeds());
43 } 41 });
44
45 let feeds = getFeeds();
46 if (feeds.length === 0) {
47 return;
48 }
49
50 // the listener on the background page might not be ready, keep trying to
51 // send the message with an exponential backoff until TIMEOUT_MAX is
52 // reached
53 let timeout = 0;
54 let sendFeeds = () => {
55 browser.runtime.sendMessage(feeds).catch(e => {
56 timeout = (timeout > 0) ? timeout * 2 : 100;
57 if (timeout > TIMEOUT_MAX) {
58 console.log(`Error: failed to message the background page: ` +
59 ` ${e.message}`);
60 return;
61 }
62 setTimeout(sendFeeds, timeout);
63 });
64 };
65 setTimeout(sendFeeds, timeout);
66 }
67
68 probeLinkedFeeds();