chrome.identity.launchwebauthflow alternative
Hey! I wanted to open up a discussion on an alternative for
chrome.identity.launchwebauthflow
because the firefox version is buggy (https://github.com/mozilla/webextension-polyfill/issues/248) and Safari doesn't support it at all.
I'm exploring a couple of options including just opening a new tab but wanted to see if anyone has experience here and can provide pointers. One of the pros of using a new tab/window is that if the user has Google Pay set up, it will work for stripe (unlike in launchwebauthflow
) and if they have a password manager it will autofill logins for the user.
Here is what I have so far (not much)
10 Replies
In
launchwebauthflow
, the page auto closes. Thankfully since this window was opened programmatically, the final redirect page can call window.close()
and it should work.
I still need to set up the success case (I think I need to use this: https://developer.chrome.com/docs/extensions/mv3/messaging/#external-webpage)
But open to feedback on this. I'll test on chrome, firefox (and safari eventually), if this works I'd suggest it be a plasmo library 🙂
Once I get the message passing working, I probably wont need any of the while loop stuff (just added that for testing)
Notes: window.close()
doesnt work
It doesnt work in firefox because of lack of externally_connectable
support
Other than that, this is probably better in the long run than the identityApi because of autofillCan you control the popup windows via background script on FF or is that also doesnt' work? xd
I was thinking if window.close() doesn't work
you can send a message to your bgsw (or bgpages for ff)
and tell it to close the popup
This works, just wont work on firefox until it supports
externally_connectable
or im sure there is a suitable alternative (but might require more permissions)
I'm probably going to generalize it more to my needs with some typescript generics BUT if plasmo wanted to release some sort of library to replace identity.launchWebAuthFlow
this would be a pretty good starting point
I think adding chrome.windows.remove(authWindow.id);
seems to work too!
Deleted my previous message, this is the final code I have that works for both stripe payment pages and todoist oauth. I'll test more but seems to cover the majority of my basic needs.
In the extension:
on my website:
Hope this helps
@louis Was thinking about using the @plasmohq/messaging
library, but it looks like the docs are out of date. relay
is deprecated. Do you also know if I can use sendToBackgroundViaRelay
in a newtab page?You can use sendToBackground from newtab directly
Would that allow
website <-> newtab
communication? Sounds like sendToBackground
would allow newtab <> background
communicationoh yeah, I thought you wanted to send to bgsw.
To comms between website <-> new tab, you will need to use relay
hmm
I wonder how that'd work - are you rendering this site inside an iframe?
or something like that?
Nope, see the code above, specifically the
launchWebAuthFlow
functionoh like with
Is that where you're opening this "webpage"? What's the host of
url
? Is it the auth service itself, a webpage you own, or a tab/newtab page?Yep, I'm opening the page from the new tab page (on button click). The URL is stripe, however the redirect url is to a domain that I own which calls
chrome.runtime.sendMessage
(see other code block). The two code blocks above work together to act exactly like launchWebAuthFlow
but works better because google pay and password managers work in the new window (it does not in launchWebAuthFlow
)I see, I wonder if content-script are injected in that windows, but as long as there is a CS that's being injected in that redirected url on that windows you created
then, the CS can hold the relay
and your page can safely use sendToBGSWWithRelay (whatever the function is called lol)
I'm not sure if
chrome.windows.create
will have CS injected to them like normal browser windows
so you will need to test that out a bit - simply add a CS matching that URL, and console log and see if there's any sign of life
Since the idea with relay is that
you have a CS injected in the page that act as the relay
so as long as a relay exists, message will flow