Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wallet installation detection #1498

Merged
merged 6 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Loading