Repository navigation
feat: offer to open the page in the phone's browser from an app's browser #1218
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
Open
mnapoli
wants to merge
2
commits into
production
Choose a base branch
from
feat/in-app-browser-hint
base: production
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−9
Open
Changes from all commits
Commits
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 hidden or 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 hidden or 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,55 @@ | ||
| export type InAppBrowser = { | ||
| /** The app showing the page, when its user agent tells. */ | ||
| app: string | null; | ||
| platform: 'ios' | 'android'; | ||
| }; | ||
|
|
||
| const APPS: [RegExp, string][] = [ | ||
| [/Twitter/i, 'X'], | ||
| [/Instagram/, 'Instagram'], | ||
| [/FBAN|FBAV|FB_IAB/, 'Facebook'], | ||
| [/LinkedInApp/, 'LinkedIn'], | ||
| [/Barcelona/, 'Threads'], | ||
| [/BytedanceWebview|musical_ly/, 'TikTok'], | ||
| [/Snapchat/, 'Snapchat'], | ||
| ]; | ||
|
|
||
| /** | ||
| * The browser an app opens links in on a phone, rather than the phone's own browser. It is signed in to nothing, so | ||
| * signing in with GitHub there means typing a password and a 2FA code, without the passkey or saved password Safari or | ||
| * Chrome would offer. Known apps name themselves in the user agent; other web views drop Safari's token on iOS, or | ||
| * add `wv` on Android. Apps that open links in Safari's own view look like Safari and aren't told apart. | ||
| */ | ||
| export function inAppBrowser(userAgent: string): InAppBrowser | null { | ||
| const platform = /iPhone|iPad|iPod/.test(userAgent) | ||
| ? 'ios' | ||
| : /Android/.test(userAgent) | ||
| ? 'android' | ||
| : null; | ||
|
|
||
| if (platform === null) { | ||
| return null; | ||
| } | ||
|
|
||
| const app = APPS.find(([pattern]) => pattern.test(userAgent))?.[1] ?? null; | ||
| const isWebView = | ||
| app !== null || | ||
| (platform === 'ios' | ||
| ? !/Safari\//.test(userAgent) | ||
| : /; wv\)/.test(userAgent)); | ||
|
|
||
| return isWebView ? { app, platform } : null; | ||
| } | ||
|
|
||
| /** | ||
| * A link that opens the page in the phone's browser: Safari's own scheme on iOS, which Apple doesn't document but | ||
| * works from web views since iOS 17, or an intent for the default browser on Android. | ||
| */ | ||
| export function phoneBrowserUrl( | ||
| url: URL, | ||
| platform: InAppBrowser['platform'], | ||
| ): string { | ||
| return platform === 'ios' | ||
| ? `x-safari-${url.href}` | ||
| : `intent://${url.host}${url.pathname}${url.search}${url.hash}#Intent;scheme=https;end`; | ||
| } | ||
This file contains hidden or 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,46 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { test } from 'node:test'; | ||
| import { inAppBrowser, phoneBrowserUrl } from '../../resources/js/lib/in-app-browser.ts'; | ||
|
|
||
| const SAFARI = | ||
| 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Mobile/15E148 Safari/604.1'; | ||
| const IOS_WEB_VIEW = | ||
| 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148'; | ||
| const CHROME_ANDROID = | ||
| 'Mozilla/5.0 (Linux; Android 15; Pixel 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36'; | ||
| const ANDROID_WEB_VIEW = | ||
| 'Mozilla/5.0 (Linux; Android 15; Pixel 9; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/141.0.0.0 Mobile Safari/537.36'; | ||
|
|
||
| await test('tells the app whose browser shows the page, when it names itself', () => { | ||
| assert.deepEqual(inAppBrowser(`${IOS_WEB_VIEW} Twitter for iPhone`), { app: 'X', platform: 'ios' }); | ||
| assert.deepEqual(inAppBrowser(`${IOS_WEB_VIEW} Instagram 400.0.0.0`), { app: 'Instagram', platform: 'ios' }); | ||
| assert.deepEqual(inAppBrowser(`${ANDROID_WEB_VIEW} [FB_IAB/FB4A;FBAV/500.0.0.0;]`), { app: 'Facebook', platform: 'android' }); | ||
| assert.deepEqual(inAppBrowser(`${SAFARI} LinkedInApp/9.30`), { app: 'LinkedIn', platform: 'ios' }); | ||
| }); | ||
|
|
||
| await test('tells web views of apps that do not name themselves', () => { | ||
| assert.deepEqual(inAppBrowser(IOS_WEB_VIEW), { app: null, platform: 'ios' }); | ||
| assert.deepEqual(inAppBrowser(ANDROID_WEB_VIEW), { app: null, platform: 'android' }); | ||
| }); | ||
|
|
||
| await test('leaves the phone browsers and computers alone', () => { | ||
| for (const userAgent of [ | ||
| SAFARI, | ||
| CHROME_ANDROID, | ||
| 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/141.0.0.0 Mobile/15E148 Safari/604.1', | ||
| 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) FxiOS/144.0 Mobile/15E148 Safari/605.1.15', | ||
| 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0 Safari/605.1.15', | ||
| ]) { | ||
| assert.equal(inAppBrowser(userAgent), null); | ||
| } | ||
| }); | ||
|
|
||
| await test('opens the page in Safari on iOS and in the default browser on Android', () => { | ||
| const url = new URL('https://dev-null.sh/@mnapoli/log/abc?tab=replies#reply-1'); | ||
|
|
||
| assert.equal(phoneBrowserUrl(url, 'ios'), 'x-safari-https://dev-null.sh/@mnapoli/log/abc?tab=replies#reply-1'); | ||
| assert.equal( | ||
| phoneBrowserUrl(url, 'android'), | ||
| 'intent://dev-null.sh/@mnapoli/log/abc?tab=replies#reply-1#Intent;scheme=https;end', | ||
| ); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.