dylanpdx/BetterTwitFix

Feature Request: Ability to directly open a post in the mobile X app

Closed this issue · 9 comments

Users of a bot I've built have asked for the ability to open the vxtwitter links in the native application. I've figured out how to do this in iOS in a proof of concept flask app.

I also question if this should be the default behavior or an option (e.g. the current subdomain features). I'm also curious to know Android handles this, but I don't have access to an Android device.

The current redirect using the meta refresh doesn't work because the X app hasn't claimed vxtwitter like it does x.com. It does still support the twitter:// deeplink feature.

from user_agents import parse

@app.route("/xyeet/<string:user>/<string:what>/<string:id>", methods=['GET'])
def x_yeet(user, what, id):
    uas = request.headers.get("User-Agent")        
    ua_parsed = parse(uas)
    
    if ua_parsed.os.family == "iOS":        
        response = make_response( redirect(f"twitter://{what}?id={id}", code=302) )
        response.headers['Referrer-Policy'] = 'no-referrer'
        return response

    if ua_parsed.is_pc:
        response = make_response (redirect(f"https://x.com/{user}/{what}/{id}") )
        response.headers['Referrer-Policy'] = 'no-referrer'
        return response

    return f"{id}", 200

I've tried simply using the redirect directly to x.com instead of twitter:// but it seems a redirect doesn't cause mobile Safari to "see" that it's something that the X app has claimed.

It seems it's possible to add this logic to the render...Template functions, or an earlier location in the logic flow, and use the render_template if the user agent is not from an iOS device. I'm willing to put that together but I've never done a pull request on github so that might be interesting.

I've experimented with this in the past and encountered issues with it, mainly general weirdness with iOS and Android not loading the app properly.
Also, if the user doesn't have the app installed, Safari displays a blank screen with minimal feedback on what happened, even if the user is logged in on the browser, which I don't think is a good user experience.
My implementation used JS on the templates to try and work around these issues but it didn't end up working out.

Loading the app on iOS worked for me in my limited test. You do raise a very valid point about handling if the app is not installed. I doubt there's an API to access the installed app list in JS in either platform. The thought I just had now was to have a page with a delay before the auto redirect with a link to the same x.com address it would redirect to. Maybe having a user tap on the link instead of a meta redirect, would trigger the app's watcher for handing x.com links over to the installed app. I'm only spitballing with what I know about how iOS deals with it so I don't know if that could work for Android, although it looks similar from a cursory search. The existing redirect could still happen automatically.

I used this for how the twitter:// scheme worked https://github.com/takumi3y/URLScheme/wiki/Twitter

I think the link click option would work, but I'm not sure about having an extra click added to the whole flow (although this is just me being picky)

Maybe setting window.location to the twitter:// schema using JS would work? And if nothing happens for around 1-2 seconds, perform the redirect to the full x.com url as a fallback.

I also found this answer, which mentions using an iframe: https://stackoverflow.com/a/22861457

I'll look into this soon and keep you updated on what I find

Or perhaps using window.location to redirect to the x.com link, instead of the meta redirect, would trigger the app handoff as if it was clicked. Or an onclick event with a click(). Just throwing out ideas of how to make the app handoff happen without user interaction.

Hey, I've been trying various methods and I haven't been able to get a result I'm happy with.

Basically, what I was hoping for is, for both iOS and Android, if the app is installed, just open the app. If it isn't installed, it should redirect to the x.com URL.

On both platforms, setting window.location to twitter://status?id=TWEET_ID works perfectly if the app is installed.
If the app isn't installed, iOS shows an error message, but still redirects to the x.com URL a little later, due to the meta tag.
Android, however, just gets stuck and does not redirect, even via JS.

Using an iframe did not work on both platforms. Apparently, it might work on older iOS versions, but not on newer ones.

So, my current ideas from here:

-Just add a "open in app" button, maybe set a cookie so it does not need to be pressed in the future. Might be tricky to press before the automatic redirect kicks in though

-Stick the x.com link in an iframe, and then add a "open in app" button at the bottom or top of that. Redirect normally for desktop

-Leave everything as is, but It would suck

I'm liking the second option. With a button to hide the iframe from view, combined with a way to remember if the user wants to open in app or just go directly to the x.com page.

Maybe use localStorage instead of cookies to save the preference to open in app.

So if nothing is saved show the iframe pair. If they saved to use the app use the redirect. If they saved to use the website (or for desktop) go directly to the site.

Maybe the JS can try to catch errors so if they try to open in app but the app isn't installed it doesn't save the open in app preference in localStorage. And then a little preference page somewhere on the site that lets someone change the setting after the fact would be cool.

Hey, just letting you know this is half-implemented on the main vx website. If you head over to https://vxtwitter.com/preferences on your mobile device and enable the redirect option, any future links should open the app.

If this initial version doesn't have any issues, I might add a one-time popup on vx links for mobile users asking if they want to enable this, but I'm still unsure about it.

@dylanpdx it is working for me and my folks so far on both iOS and Android.

Nama commented

Disabled

Firefox asks to open the app and doesn't load the twitter website

Enabled

Firefox asks to open the app and loads the twitter site

just fyi^^ I'll keep it disabled.