comparison js/background.js @ 46:586eebf8efb7

Work around race condition in feed reader subscription script execution browser.tabs.executeScript() should only run the script once the feed preview page is completely loaded but there seems to be a race condition where it is run too early resulting in disabled feed reader subscription.
author Guido Berhoerster <guido+feed-preview@berhoerster.name>
date Wed, 03 Jul 2019 15:51:36 +0200
parents 6bd8a649186d
children ede87e1004f9
comparison
equal deleted inserted replaced
45:cc328d4662dd 46:586eebf8efb7
190 sendResponse(tabsFeeds.get(request)); 190 sendResponse(tabsFeeds.get(request));
191 } 191 }
192 }); 192 });
193 193
194 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { 194 browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
195 if (typeof changeInfo.url === 'undefined') { 195 if (changeInfo.status !== 'complete') {
196 // filter out updates which do not change the URL
197 return; 196 return;
198 } 197 }
199 198
200 // hide the page action when the URL changes since it is no longer valid, 199 // hide the page action when the URL changes since it is no longer valid,
201 // it will be shown again if the content script detects a feed 200 // it will be shown again if the content script detects a feed
202 browser.pageAction.hide(tabId); 201 browser.pageAction.hide(tabId);
203 202
204 // inject content script once if the requested URL is a feed preview 203 // inject content script once if the requested URL is a feed preview
205 if (tabsFeedPreviews.get(tabId) === changeInfo.url) { 204 if (tabsFeedPreviews.get(tabId) === tab.url) {
206 browser.tabs.executeScript(tabId, { 205 browser.tabs.executeScript(tabId, {
207 file: 'content_scripts/feed-readers.js' 206 file: 'content_scripts/feed-readers.js'
208 }); 207 });
209 tabsFeedPreviews.delete(tabId); 208 tabsFeedPreviews.delete(tabId);
210 } 209 }
211 }); 210 }, {properties: ["status"]});
212 211
213 browser.tabs.onRemoved.addListener((tabId, removeInfo) => { 212 browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
214 tabsFeeds.delete(tabId); 213 tabsFeeds.delete(tabId);
215 tabsFeedPreviews.delete(tabId); 214 tabsFeedPreviews.delete(tabId);
216 }); 215 });