comparison 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
comparison
equal deleted inserted replaced
53:7e1a2357b8c5 54:ede87e1004f9
178 }, 178 },
179 {urls: ['http://*/*', 'https://*/*'], types: ['main_frame']}, 179 {urls: ['http://*/*', 'https://*/*'], types: ['main_frame']},
180 ['blocking', 'responseHeaders']); 180 ['blocking', 'responseHeaders']);
181 181
182 browser.runtime.onMessage.addListener((request, sender, sendResponse) => { 182 browser.runtime.onMessage.addListener((request, sender, sendResponse) => {
183 let tab = sender.tab; 183 // popup querying feeds
184 if (typeof tab !== 'undefined') { 184 sendResponse(tabsFeeds.get(request));
185 // content script sending feeds 185 });
186 tabsFeeds.set(tab.id, request); 186
187 browser.pageAction.show(tab.id); 187 browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
188 } else {
189 // popup querying feeds
190 sendResponse(tabsFeeds.get(request));
191 }
192 });
193
194 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
195 if (changeInfo.status !== 'complete') { 188 if (changeInfo.status !== 'complete') {
189 // hide the page action when the URL changes since it is no longer
190 // valid, it will be shown again if the content script detects a feed
191 browser.pageAction.hide(tabId);
196 return; 192 return;
197 } 193 }
198 194
199 // hide the page action when the URL changes since it is no longer valid,
200 // it will be shown again if the content script detects a feed
201 browser.pageAction.hide(tabId);
202
203 // inject content script once if the requested URL is a feed preview
204 if (tabsFeedPreviews.get(tabId) === tab.url) { 195 if (tabsFeedPreviews.get(tabId) === tab.url) {
196 // inject content script once if the requested URL is a feed preview
205 browser.tabs.executeScript(tabId, { 197 browser.tabs.executeScript(tabId, {
206 file: 'content_scripts/feed-readers.js' 198 file: 'content_scripts/feed-readers.js'
207 }); 199 });
208 tabsFeedPreviews.delete(tabId); 200 tabsFeedPreviews.delete(tabId);
201 } else {
202 // query available feeds
203 let feeds = await browser.tabs.sendMessage(tabId, {});
204 if (feeds.length > 0) {
205 tabsFeeds.set(tabId, feeds);
206 browser.pageAction.show(tabId);
207 console.log(`detected feeds in tab ${tabId} for ${tab.url}:\n`,
208 feeds);
209 } else {
210 console.log(`no feeds detected in tab ${tabId} for ${tab.url}\n`);
211 }
209 } 212 }
210 }, {properties: ["status"]}); 213 }, {properties: ["status"]});
211 214
212 browser.tabs.onRemoved.addListener((tabId, removeInfo) => { 215 browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
213 tabsFeeds.delete(tabId); 216 tabsFeeds.delete(tabId);
234 if (typeof feedPreview !== 'undefined' && 237 if (typeof feedPreview !== 'undefined' &&
235 feedPreview === Object(feedPreview)) { 238 feedPreview === Object(feedPreview)) {
236 feedPreviewOptions.expandEntries = !!feedPreview.expandEntries; 239 feedPreviewOptions.expandEntries = !!feedPreview.expandEntries;
237 } 240 }
238 }); 241 });
242
239 browser.storage.onChanged.addListener((changes, areaName) => { 243 browser.storage.onChanged.addListener((changes, areaName) => {
240 if (areaName !== 'sync' || typeof changes.feedPreview === 'undefined') { 244 if (areaName !== 'sync' || typeof changes.feedPreview === 'undefined') {
241 return; 245 return;
242 } 246 }
243 247