[Advice needed] feat: Have injectScript return the return value of the script#1765
[Advice needed] feat: Have injectScript return the return value of the script#1765ion1 wants to merge 1 commit intowxt-dev:mainfrom
Conversation
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
1f9dab4 to
ca3ca14
Compare
5b335b1 to
d72f1ca
Compare
d72f1ca to
3aebc7c
Compare
|
@ion1 Are you have a plan to go on with that? If yes, let's tell us, what type of advice you need, what info we can provide to let you through. |
|
@ion1 One more poke :) |
|
Hi, Sorry I didn't manage to respond more quickly. The issue is that code executed via I'm not sure what would be the best way to handle the difference. Separate entry points? Some kind of reliable detection within a single entry point? I would appreciate your thoughts on this. The draft just makes it work for I am still interested in this feature, but unfortunately I cannot make any promises as to when I will have the energy to work on it next. |
|
@ion1 Thanks, i'll take a look on that, maybe today and try to give you my feedback. |
3aebc7c to
c0d95c6
Compare
✅ Deploy Preview for creative-fairy-df92c4 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Rebased on main. |
PatrykKuniczak
left a comment
There was a problem hiding this comment.
Overall, before those changes, you need advice from @aklinker1
Because i don't have that much knowledge about project and don't want to make that big decision.
If you'll have suggestion/approval from aklinker and make necessary changes(if needed), i'll review it more precisely 😄
| return ( | ||
| result != null && | ||
| typeof result === 'object' && | ||
| 'type' in result && | ||
| typeof result.type === 'string' && | ||
| ((result.type === 'success' && 'value' in result) || | ||
| (result.type === 'error' && | ||
| 'error' in result && | ||
| isDeconstructedError(result.error))) | ||
| ); | ||
| } |
There was a problem hiding this comment.
| return ( | |
| result != null && | |
| typeof result === 'object' && | |
| 'type' in result && | |
| typeof result.type === 'string' && | |
| ((result.type === 'success' && 'value' in result) || | |
| (result.type === 'error' && | |
| 'error' in result && | |
| isDeconstructedError(result.error))) | |
| ); | |
| } | |
| return ( | |
| ((result.?type === 'success' && 'value' in result) || | |
| (result.?type === 'error' && | |
| 'error' in result && | |
| isDeconstructedError(result.error))) | |
| ); | |
| } |
i hope this works the same way
| /** | ||
| * The return value of the script. | ||
| */ | ||
| result: unknown; |
There was a problem hiding this comment.
should be typed
A result object containing the created script element and the return value of the script.
That's mean, you know at least that will be script, and you can pass e.g generic type param here, from that script return type
| const script = document.currentScript; | ||
| if (script === null) { |
There was a problem hiding this comment.
| const script = document.currentScript; | |
| if (script === null) { | |
| if (document.?currentScript) { |
There was a problem hiding this comment.
This should be split to at least 3 files.
1 for dispatch helpers, 1 for errors and 1 "main"
There was a problem hiding this comment.
@aklinker1 OOOOOO right.
If this massive ifs, will be shortify this can be fine also for me.
There was a problem hiding this comment.
Alright, read through it. Makes sense, message passing is really the only way to do this.
Instead of stuffing this into injectScript, maybe we build a new util on top of it. If people want to use it, they can. If they don't, it's tree-stripped out.
import { evalScript } from 'eval-script/parent'
const res = await evalScript("/unlisted.js", ...args)
console.log(res) // "something"import { defineEvalScript } from 'eval-script/child'
import defineEvalScript((...args) => {
return "something";
})That seems like the best approach to me, but the bigger question is if I want to be responsible for maintaining this... I don't think I do. Given how long it took for me to respond to this PR lol. Gotta be picky.
If there's interest, I might be willing to make it an official WXT package, like @wxt-dev/storage. But at least in my use-cases, it just doesn't seem like a util most extensions would need. But I could be wrong.
So how about this... Create an issue for this featur. If it gets, let's say, 50 👍 or other positive reactions I'll be down to add the package for it, maybe even add it to the core wxt.
If you're willing to own the util and fix bugs or implement other features, that would help me out a ton too.
|
@ion1 Let's open https://github.com/wxt-dev/wxt/discussions and link this PR, it can be easier to gain 50 👍 |
Overview
A sketch for having
injectScriptreturn the result value from the script. The code works, but it currently has changes to virtual/unlisted-script-entrypoint.ts that only work withinjectScriptwhile being broken with thebrowser.scriptingAPI.I would like to get feedback on how you would like that to be resolved. Some options I can think of:
injectScriptorexecuteScript. I do not know how to achieve that reliably.document.currentScriptcan be overridden by the web page usingObject.defineProperty, so its value might not be a reliable way to determine that.injectScriptorexecuteScript.Manual Testing
Use
injectScripton a script which either returns a value or throws an exception; see thatinjectScriptreturns the value or rethrows the exception.Related Issue
#1754 proposes having a way to pass an
UnlistedScriptDefinitionrather than the filename toinjectScript. If something like that was implemented, the type of the return value ofinjectScriptcould be based on the type ofmainwithin theUnlistedScriptDefinition.