1. /* global chrome */
    
  2. 
    
  3. 'use strict';
    
  4. 
    
  5. import setExtensionIconAndPopup from './setExtensionIconAndPopup';
    
  6. 
    
  7. function isRestrictedBrowserPage(url) {
    
  8.   return !url || new URL(url).protocol === 'chrome:';
    
  9. }
    
  10. 
    
  11. function checkAndHandleRestrictedPageIfSo(tab) {
    
  12.   if (tab && isRestrictedBrowserPage(tab.url)) {
    
  13.     setExtensionIconAndPopup('restricted', tab.id);
    
  14.   }
    
  15. }
    
  16. 
    
  17. // update popup page of any existing open tabs, if they are restricted browser pages.
    
  18. // we can't update for any other types (prod,dev,outdated etc)
    
  19. // as the content script needs to be injected at document_start itself for those kinds of detection
    
  20. // TODO: Show a different popup page(to reload current page probably) for old tabs, opened before the extension is installed
    
  21. if (__IS_CHROME__ || __IS_EDGE__) {
    
  22.   chrome.tabs.query({}, tabs => tabs.forEach(checkAndHandleRestrictedPageIfSo));
    
  23.   chrome.tabs.onCreated.addListener((tabId, changeInfo, tab) =>
    
  24.     checkAndHandleRestrictedPageIfSo(tab),
    
  25.   );
    
  26. }
    
  27. 
    
  28. // Listen to URL changes on the active tab and update the DevTools icon.
    
  29. chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
    
  30.   if (__IS_FIREFOX__) {
    
  31.     // We don't properly detect protected URLs in Firefox at the moment.
    
  32.     // However, we can reset the DevTools icon to its loading state when the URL changes.
    
  33.     // It will be updated to the correct icon by the onMessage callback below.
    
  34.     if (tab.active && changeInfo.status === 'loading') {
    
  35.       setExtensionIconAndPopup('disabled', tabId);
    
  36.     }
    
  37.   } else {
    
  38.     // Don't reset the icon to the loading state for Chrome or Edge.
    
  39.     // The onUpdated callback fires more frequently for these browsers,
    
  40.     // often after onMessage has been called.
    
  41.     checkAndHandleRestrictedPageIfSo(tab);
    
  42.   }
    
  43. });