diff js/background.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 586eebf8efb7
children fc5fca9af05f
line wrap: on
line diff
--- 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;