Skip to content

Commit

Permalink
fix: wallet installation detection (#1498)
Browse files Browse the repository at this point in the history
Closes #1464 

# Changes

## @fuel-wallet/connections
- Fixed extension not being detected as installed after inactivity
period.
- Fixed not being detected after fresh install on currently open tabs

## Wallet
- Avoid duplicating instances of injected Content Script
- Avoid attempting to inject into restricted urls (e.g. `chrome://...`)

# Evidence

## Before

[Fresh
Install](https://github.com/user-attachments/assets/a22507b2-833f-4013-97b5-64685c53d259)

[Inactivity/Suspension](https://github.com/user-attachments/assets/4c88db00-88bd-43d6-8834-70f7b5853f61)

## After

[Fresh
Install](https://github.com/user-attachments/assets/20b5c026-d523-4864-a391-b9c6385aeb85)

[Inactivity/Suspension](https://github.com/user-attachments/assets/92c22da0-db65-4a0b-8538-b40a3104a00e)
  • Loading branch information
arthurgeron committed Sep 23, 2024
1 parent 4bd3f8f commit 698c272
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/funny-melons-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-wallet/connections": patch
---

Fixed extension not being detected as installed after inactivity period.
5 changes: 5 additions & 0 deletions .changeset/happy-clouds-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Avoid duplicating instances of injected Content Script
2 changes: 0 additions & 2 deletions packages/app/src/systems/CRX/background/actions/onInstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,4 @@ chrome.runtime.onInstalled.addListener(async (object) => {
) {
chrome.tabs.create({ url: welcomeLink() });
}

executeContentScript();
});
13 changes: 8 additions & 5 deletions packages/app/src/systems/CRX/scripts/executeContentScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ import fileName from './contentScript?script';
*/
export async function executeContentScript() {
chrome.tabs.query({ url: '<all_urls>' }, (tabs) => {
// biome-ignore lint/complexity/noForEach: <explanation>
tabs.forEach((tab) => {
if (!tab.id) return;
for (const tab of tabs) {
if (!tab.id || tab.url?.startsWith('chrome://')) continue;
chrome.scripting
.executeScript({
target: { tabId: tab.id, allFrames: true },
files: [fileName],
injectImmediately: true,
})
// Ignore errors on tabs when executing script
.catch(() => {});
});
.catch((err) => {
if (process.env?.NODE_ENV === 'development') {
console.warn(err);
}
});
}
});
}
10 changes: 7 additions & 3 deletions packages/connections/src/ContentProxyConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type { JSONRPCID } from 'json-rpc-2.0';
export class ContentProxyConnection {
connection: chrome.runtime.Port | undefined = undefined;
readonly connectorName: string;

constructor(connectorName: string) {
this.connectorName = connectorName;
this.onMessageFromWindow = this.onMessageFromWindow.bind(this);
Expand Down Expand Up @@ -54,12 +53,14 @@ export class ContentProxyConnection {
this.onStartEvent();
}

destroy() {
destroy(keepWindowListener = true) {
this.connection?.onMessage.removeListener(this.onMessageFromExtension);
this.connection?.onDisconnect.removeListener(this.onDisconnect);
this.connection?.disconnect();
this.connection = undefined;
window.removeEventListener(EVENT_MESSAGE, this.onMessageFromWindow);
if (!keepWindowListener) {
window.removeEventListener(EVENT_MESSAGE, this.onMessageFromWindow);
}
}

onDisconnect = () => {
Expand Down Expand Up @@ -96,6 +97,9 @@ export class ContentProxyConnection {
onMessageFromWindow = (message: MessageEvent<CommunicationMessage>) => {
const { data: event, origin } = Object.freeze(message);
if (this.shouldAcceptMessage(event, origin)) {
if (!this.connection) {
this.connectAndAttachListeners();
}
if (
event.type === MessageTypes.request &&
event.request.method === 'connectorName'
Expand Down

0 comments on commit 698c272

Please sign in to comment.