-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: duplicate instance of ContentScript causing black screen (#1550)
- Fixed duplicate instances of ContentScript after extension sleep/wakeup. Closes [#326](FuelLabs/fuel-connectors#326) - Ensure extension PopUp is closed on service resolve timeout/rejection. - Ensure extension PopUp is closed on class destroy
- Loading branch information
1 parent
b16cbf4
commit c38db1a
Showing
7 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@fuel-wallet/connections": patch | ||
"@fuel-wallet/types": patch | ||
"fuels-wallet": patch | ||
--- | ||
|
||
Fixed duplicate instances of ContentScript after extension sleep/wakeup. Closes [#326](https://github.com/FuelLabs/fuel-connectors/issues/326) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fuels-wallet": patch | ||
--- | ||
|
||
Ensure popup is closed on resolve timeout |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 33 additions & 11 deletions
44
packages/app/src/systems/CRX/scripts/executeContentScript.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
import { ContentScriptMessageTypes } from '@fuel-wallet/types'; | ||
import fileName from './contentScript?script'; | ||
|
||
/* | ||
* This function is called by the background script to register the content script | ||
* on all tabs. This is necessary because the content script is not automatically | ||
* registered on tabs that are already open when the extension is installed. | ||
*/ | ||
|
||
// Ping to check if the content script is already injected | ||
export async function executeContentScript() { | ||
chrome.tabs.query({ url: '<all_urls>' }, (tabs) => { | ||
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((err) => { | ||
if (process.env?.NODE_ENV === 'development') { | ||
console.warn(err); | ||
|
||
// Send a ping message to check if content script is already injected | ||
chrome.tabs.sendMessage( | ||
tab.id, | ||
{ | ||
type: ContentScriptMessageTypes.PING, | ||
}, | ||
(response) => { | ||
if ( | ||
response?.type !== ContentScriptMessageTypes.PONG || | ||
chrome.runtime.lastError | ||
) { | ||
// No response, content script is not injected | ||
injectContentScript(tab.id!); | ||
} | ||
}); | ||
} | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function injectContentScript(tabId: number) { | ||
chrome.scripting | ||
.executeScript({ | ||
target: { tabId: tabId, allFrames: true }, | ||
files: [fileName], | ||
injectImmediately: true, | ||
}) | ||
.catch((err) => { | ||
if (process.env?.NODE_ENV === 'development') { | ||
console.warn(err); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum ContentScriptMessageTypes { | ||
PING = 'ping', | ||
PONG = 'pong', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters