-
-
Notifications
You must be signed in to change notification settings - Fork 245
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
feat: Support returning values from scripts executed with the scripting API #624
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b1ec6c0
feat: Support returning values from scripts executed with the scripti…
aklinker1 cb7797b
get it working
aklinker1 882bfd8
update test snapshot
aklinker1 b4f4c5a
bump version for early release
aklinker1 d91277f
Add unit tests for string util
aklinker1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
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
11 changes: 9 additions & 2 deletions
11
packages/wxt/src/virtual/content-script-main-world-entrypoint.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,14 +1,21 @@ | ||
import definition from 'virtual:user-content-script-main-world-entrypoint'; | ||
import { logger } from '../sandbox/utils/logger'; | ||
|
||
(async () => { | ||
const result = (async () => { | ||
try { | ||
const { main } = definition; | ||
await main(); | ||
return await main(); | ||
} catch (err) { | ||
logger.error( | ||
`The content script "${import.meta.env.ENTRYPOINT}" crashed on startup!`, | ||
err, | ||
); | ||
throw err; | ||
} | ||
})(); | ||
|
||
// Return the main function's result to the background when executed via the | ||
// scripting API. Default export causes the IIFE to return a value. | ||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value | ||
// Tested on both Chrome and Firefox | ||
export default result; | ||
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,13 +1,20 @@ | ||
import definition from 'virtual:user-unlisted-script-entrypoint'; | ||
import { logger } from '../sandbox/utils/logger'; | ||
|
||
(async () => { | ||
const result = (async () => { | ||
try { | ||
await definition.main(); | ||
return await definition.main(); | ||
} catch (err) { | ||
logger.error( | ||
`The unlisted script "${import.meta.env.ENTRYPOINT}" crashed on startup!`, | ||
err, | ||
); | ||
throw err; | ||
} | ||
})(); | ||
|
||
// Return the main function's result to the background when executed via the | ||
// scripting API. Default export causes the IIFE to return a value. | ||
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/scripting/executeScript#return_value | ||
// Tested on both Chrome and Firefox | ||
export default result; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a possibility that the generated variable name might conflict with an existing variable on the page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're running the code in an isolated world, no. Variables are stored in the isolated context.
If you're running the code in the main world... I'm not sure. We could add a prefix or something that makes that less likely. But we should verify that first before making the change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have two main use cases:
For 1, I can see there might be a chance of naming collisions and for 2 it would just produce syntax errors with evaluating undeclared variables generated here by WXT.
Let me see if I can put up a minimal reproduction for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It’s a bit late, but I finally had time to look into this (not for name collisions, but for errors occurring with Web Workers):
#1424