changeset 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 7e1a2357b8c5
children ca6140f82dc8
files content_scripts/feed-probe.js js/background.js manifest.json.in
diffstat 3 files changed, 26 insertions(+), 48 deletions(-) [+]
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());
+});
--- a/js/background.js	Wed Sep 18 13:07:06 2019 +0200
+++ b/js/background.js	Thu Sep 26 23:11:18 2019 +0200
@@ -180,32 +180,35 @@
         ['blocking', 'responseHeaders']);
 
 browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
-    let tab = sender.tab;
-    if (typeof tab !== 'undefined') {
-        // content script sending feeds
-        tabsFeeds.set(tab.id, request);
-        browser.pageAction.show(tab.id);
-    } else {
-        // popup querying feeds
-        sendResponse(tabsFeeds.get(request));
-    }
+    // popup querying feeds
+    sendResponse(tabsFeeds.get(request));
 });
 
-browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
+browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
     if (changeInfo.status !== 'complete') {
+        // hide the page action when the URL changes since it is no longer
+        // valid, it will be shown again if the content script detects a feed
+        browser.pageAction.hide(tabId);
         return;
     }
 
-    // hide the page action when the URL changes since it is no longer valid,
-    // it will be shown again if the content script detects a feed
-    browser.pageAction.hide(tabId);
-
-    // inject content script once if the requested URL is a feed preview
     if (tabsFeedPreviews.get(tabId) === tab.url) {
+        // inject content script once if the requested URL is a feed preview
         browser.tabs.executeScript(tabId, {
             file: 'content_scripts/feed-readers.js'
         });
         tabsFeedPreviews.delete(tabId);
+    } else {
+        // query available feeds
+        let feeds = await browser.tabs.sendMessage(tabId, {});
+        if (feeds.length > 0) {
+            tabsFeeds.set(tabId, feeds);
+            browser.pageAction.show(tabId);
+            console.log(`detected feeds in tab ${tabId} for ${tab.url}:\n`,
+                    feeds);
+        } else {
+            console.log(`no feeds detected in tab ${tabId} for ${tab.url}\n`);
+        }
     }
 }, {properties: ["status"]});
 
@@ -236,6 +239,7 @@
         feedPreviewOptions.expandEntries = !!feedPreview.expandEntries;
     }
 });
+
 browser.storage.onChanged.addListener((changes, areaName) => {
     if (areaName !== 'sync' || typeof changes.feedPreview === 'undefined') {
         return;
--- a/manifest.json.in	Wed Sep 18 13:07:06 2019 +0200
+++ b/manifest.json.in	Thu Sep 26 23:11:18 2019 +0200
@@ -30,7 +30,8 @@
     "content_scripts": [
         {
             "matches": [ "http://*/*", "https://*/*", "file:///*" ],
-            "js": [ "content_scripts/feed-probe.js" ]
+            "js": [ "content_scripts/feed-probe.js" ],
+            "run_at": "document_end"
         }
     ],
     "web_accessible_resources": [