Skip to content

Commit

Permalink
Add additional listener for inject script
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjort committed Mar 12, 2024
1 parent dd150b8 commit cd461fa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
6 changes: 6 additions & 0 deletions packages/browser-wallet/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixed

- Inject script not loading on the first page of a new tab, causing API to not be available for dApps.

## 1.4.2

### Fixed
Expand Down
23 changes: 14 additions & 9 deletions packages/browser-wallet/src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,24 @@ async function exportGRPCLocation(
return onSuccess(`${network.grpcUrl}:${network.grpcPort}`);
}

async function executeInjectScript(tabId: number): Promise<void> {
await chrome.scripting.executeScript({
target: { tabId },
// TODO this is a reference to the output file, expecting to be placed in the root with manifest.json.
// Would be nice if the relative output path could be built from a reference to the entrypoint file instead.
files: ['inject.js'],
world: 'MAIN',
});
}

/**
* Callback method which installs Injected script into Main world of Dapp
*/
const injectScript: ExtensionMessageHandler = (_msg, sender, respond) => {
if (sender.tab?.id === undefined) {
throw new Error('No ID for tab.');
}

chrome.scripting
.executeScript({
target: { tabId: sender.tab.id },
// TODO this is a reference to the output file, expecting to be placed in the root with manifest.json.
// Would be nice if the relative output path could be built from a reference to the entrypoint file instead.
files: ['inject.js'],
world: 'MAIN',
})
executeInjectScript(sender.tab.id)
.then(() => respond(true))
.catch(() => respond(false));

Expand Down Expand Up @@ -251,11 +253,14 @@ bgMessageHandler.handleMessage(
sendCredentialHandler
);

chrome.tabs.onUpdated.addListener((tabId, _, tab) => tab.url?.startsWith('http') && executeInjectScript(tabId));

bgMessageHandler.handleMessage(
createMessageTypeFilter(InternalMessageType.StartIdentityIssuance),
identityIssuanceHandler
);
bgMessageHandler.handleMessage(createMessageTypeFilter(InternalMessageType.Init), injectScript);

bgMessageHandler.handleMessage(createMessageTypeFilter(InternalMessageType.SetViewSize), ({ payload }) => {
setPopupSize(payload);
});
Expand Down

0 comments on commit cd461fa

Please sign in to comment.