Mercurial > addons > firefox-addons > feed-preview
diff 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 |
line wrap: on
line diff
--- a/content_scripts/feed-probe.js Wed Sep 18 13:07:06 2019 +0200 +++ b/content_scripts/feed-probe.js Thu Sep 26 23:11:18 2019 +0200 @@ -8,8 +8,6 @@ 'use strict'; -const TIMEOUT_MAX = 800; // ms - function getFeeds() { let urlsFeeds = new Map(); let elements = document.querySelectorAll(':-moz-any(link, a)[href]' + @@ -31,38 +29,13 @@ href: element.href, title: element.title || browser.i18n.getMessage('defaultFeedTitle'), type: element.type - }) + }); } return Array.from(urlsFeeds.values()); } -function probeLinkedFeeds() { - if (document.documentElement.nodeName.toUpperCase() !== 'HTML') { - return; - } - - let feeds = getFeeds(); - if (feeds.length === 0) { - return; - } - - // the listener on the background page might not be ready, keep trying to - // send the message with an exponential backoff until TIMEOUT_MAX is - // reached - let timeout = 0; - let sendFeeds = () => { - browser.runtime.sendMessage(feeds).catch(e => { - timeout = (timeout > 0) ? timeout * 2 : 100; - if (timeout > TIMEOUT_MAX) { - console.log(`Error: failed to message the background page: ` + - ` ${e.message}`); - return; - } - setTimeout(sendFeeds, timeout); - }); - }; - setTimeout(sendFeeds, timeout); -} - -probeLinkedFeeds(); +browser.runtime.onMessage.addListener((request, sender, sendResponse) => { + // background page querying available feeds + sendResponse(getFeeds()); +});