Skip to content

Commit

Permalink
fix[react-devtools/extensions]: fixed tabs API calls and displaying r…
Browse files Browse the repository at this point in the history
…estricted access popup (#30825)

Stacked on #30824. See [this
commit](c9830d6).

Turns out we should be listing `tabs` in our permissions, if we want to
be able to receive tab url, once its updated.
This also fixes `chrome.tabs.onCreated` event subscription, because [it
should receive only tab
object](https://developer.chrome.com/docs/extensions/reference/api/tabs#event-onCreated),
and not 3 arguments, as expected in the previous implementation.
  • Loading branch information
hoxyq authored Aug 29, 2024
1 parent 537c74e commit 795b320
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
},
"permissions": [
"storage",
"scripting"
"scripting",
"tabs"
],
"host_permissions": [
"<all_urls>"
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/edge/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
},
"permissions": [
"storage",
"scripting"
"scripting",
"tabs"
],
"host_permissions": [
"<all_urls>"
Expand Down
3 changes: 2 additions & 1 deletion packages/react-devtools-extensions/firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
]
},
"permissions": [
"scripting"
"scripting",
"tabs"
],
"host_permissions": [
"<all_urls>"
Expand Down
20 changes: 11 additions & 9 deletions packages/react-devtools-extensions/src/background/tabsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
import setExtensionIconAndPopup from './setExtensionIconAndPopup';

function isRestrictedBrowserPage(url) {
return !url || new URL(url).protocol === 'chrome:';
if (!url) {
return true;
}

const urlProtocol = new URL(url).protocol;
return urlProtocol === 'chrome:' || urlProtocol === 'about:';
}

function checkAndHandleRestrictedPageIfSo(tab) {
Expand All @@ -14,16 +19,13 @@ function checkAndHandleRestrictedPageIfSo(tab) {
}
}

// update popup page of any existing open tabs, if they are restricted browser pages.
// we can't update for any other types (prod,dev,outdated etc)
// as the content script needs to be injected at document_start itself for those kinds of detection
// TODO: Show a different popup page(to reload current page probably) for old tabs, opened before the extension is installed
// Update popup page of any existing open tabs, if they are restricted browser pages
chrome.tabs.query({}, tabs => tabs.forEach(checkAndHandleRestrictedPageIfSo));
chrome.tabs.onCreated.addListener((tabId, changeInfo, tab) =>
checkAndHandleRestrictedPageIfSo(tab),
);
chrome.tabs.onCreated.addListener(tab => checkAndHandleRestrictedPageIfSo(tab));

// Listen to URL changes on the active tab and update the DevTools icon.
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
checkAndHandleRestrictedPageIfSo(tab);
if (changeInfo.url && isRestrictedBrowserPage(changeInfo.url)) {
setExtensionIconAndPopup('restricted', tabId);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,8 @@ import {registerDevToolsEventLogger} from 'react-devtools-shared/src/registerDev

function registerEventsLogger() {
registerDevToolsEventLogger('extension', async () => {
// TODO: after we upgrade to Firefox Manifest V3, chrome.tabs.query returns a Promise without the callback.
return new Promise(resolve => {
chrome.tabs.query({active: true}, tabs => {
resolve({
page_url: tabs[0]?.url,
});
});
});
const tabs = await chrome.tabs.query({active: true});
return {page_url: tabs[0]?.url};
});
}

Expand Down

0 comments on commit 795b320

Please sign in to comment.