Skip to content

Commit

Permalink
Scope approvals to documentId
Browse files Browse the repository at this point in the history
  • Loading branch information
grod220 committed Jun 20, 2024
1 parent e9b0d0d commit de56e1d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
4 changes: 3 additions & 1 deletion apps/extension/src/listeners/message-prax-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ chrome.runtime.onMessage.addListener(
// user made a choice
if (status === UserChoice.Approved) {
respond(PraxConnection.Init);
void chrome.tabs.sendMessage(sender.tab!.id!, PraxConnection.Init);
void chrome.tabs.sendMessage(sender.tab!.id!, PraxConnection.Init, {
documentId: sender.documentId, // Ensures tab has not redirected to another url
});
} else {
respond(PraxConnection.Denied);
}
Expand Down
18 changes: 15 additions & 3 deletions apps/extension/src/listeners/tabs-updated-prax-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@ import { PraxConnection } from '../message/prax';

// trigger injected-connection-port to init when a known page is loaded.
chrome.tabs.onUpdated.addListener(
(tabId, { status, discarded }, { url }) =>
(tabId, { status, discarded, url: changeInfoUrl }, { url: tabUrl }) => {
// Unfortunately, no context for tab
// void chrome.runtime.getContexts({}).then(console.log);

const url = changeInfoUrl ?? tabUrl;
const documentUrls = [tabUrl, changeInfoUrl].filter(id => id) as string[];
void chrome.runtime.getContexts({}).then(console.log);
const getContext = chrome.runtime
.getContexts({ tabIds: [tabId], documentUrls })
.then(contexts => contexts.filter(({ frameId }) => !frameId)[0]);
void (async () => {
const documentId = (await getContext)?.documentId;
if (
documentId &&
status === 'complete' &&
!discarded &&
url?.startsWith('https://') &&
(await originAlreadyApproved(url))
)
void chrome.tabs.sendMessage(tabId, PraxConnection.Init);
})(),
void chrome.runtime.sendMessage(PraxConnection.Init, { tabId, documentId });
})();
},
);

0 comments on commit de56e1d

Please sign in to comment.