Skip to content
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

Using @greymass/return-path library. #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"src/*"
],
"dependencies": {
"@greymass/return-path": "^0.0.2",
"tslib": "^2.0.3"
},
"peerDependencies": {
Expand Down
99 changes: 5 additions & 94 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
SigningRequest,
} from 'anchor-link'

import { detectReturnPath, isAppleHandheld, isFirefox, isBrave } from "@greymass/return-path";

import styleText from './styles'
import generateQr from './qrcode'

Expand Down Expand Up @@ -268,9 +270,9 @@ export default class BrowserTransport implements LinkTransport {
showFooter = true
) {
const sameDeviceRequest = request.clone()
const returnUrl = generateReturnUrl()
const returnPath = detectReturnPath()
sameDeviceRequest.setInfoKey('same_device', true)
sameDeviceRequest.setInfoKey('return_path', returnUrl)
sameDeviceRequest.setInfoKey('return_path', returnPath)

const sameDeviceUri = sameDeviceRequest.encode(true, false)
const crossDeviceUri = request.encode(true, false)
Expand Down Expand Up @@ -387,7 +389,7 @@ export default class BrowserTransport implements LinkTransport {
cancel: (reason: string | Error) => void
) {
if (session.metadata.sameDevice) {
request.setInfoKey('return_path', generateReturnUrl())
request.setInfoKey('return_path', detectReturnPath())
}

if (session.type === 'fallback') {
Expand Down Expand Up @@ -763,97 +765,6 @@ function emptyElement(el: HTMLElement) {
}
}

/** Generate a return url that Anchor will redirect back to w/o reload. */
function generateReturnUrl() {
if (isChromeiOS()) {
// google chrome on iOS will always open new tab so we just ask it to open again as a workaround
return 'googlechrome://'
}
if (isFirefoxiOS()) {
// same for firefox
return 'firefox:://'
}
if (isAppleHandheld() && isBrave()) {
// and brave ios
return 'brave://'
}
if (isAppleHandheld()) {
// return url with unique fragment required for iOS safari to trigger the return url
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
let rv = window.location.href.split('#')[0] + '#'
for (let i = 0; i < 8; i++) {
rv += alphabet.charAt(Math.floor(Math.random() * alphabet.length))
}
return rv
}

if (isAndroid() && isFirefox()) {
return 'android-intent://org.mozilla.firefox'
}

if (isAndroid() && isEdge()) {
return 'android-intent://com.microsoft.emmx'
}

if (isAndroid() && isOpera()) {
return 'android-intent://com.opera.browser'
}

if (isAndroid() && isBrave()) {
return 'android-intent://com.brave.browser'
}

if (isAndroid() && isAndroidWebView()) {
return 'android-intent://webview'
}

if (isAndroid() && isChromeMobile()) {
return 'android-intent://com.android.chrome'
}

return window.location.href
}

function isAppleHandheld() {
return /iP(ad|od|hone)/i.test(navigator.userAgent)
}

function isChromeiOS() {
return /CriOS/.test(navigator.userAgent)
}

function isChromeMobile() {
return /Chrome\/[.0-9]* Mobile/i.test(navigator.userAgent)
}

function isFirefox() {
return /Firefox/i.test(navigator.userAgent)
}

function isFirefoxiOS() {
return /FxiOS/.test(navigator.userAgent)
}

function isOpera() {
return /OPR/.test(navigator.userAgent) || /Opera/.test(navigator.userAgent)
}

function isEdge() {
return /Edg/.test(navigator.userAgent)
}

function isBrave() {
return navigator['brave'] && typeof navigator['brave'].isBrave === 'function'
}

function isAndroid() {
return /Android/.test(navigator.userAgent)
}

function isAndroidWebView() {
return /wv/.test(navigator.userAgent) || /Android.*AppleWebKit/.test(navigator.userAgent);
}

function copyToClipboard(text: string) {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text)
Expand Down
1 change: 1 addition & 0 deletions test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"build": "snowpack build"
},
"dependencies": {
"@greymass/return-path": "^0.0.2",
"@snowpack/plugin-typescript": "^1.1.1",
"anchor-link": "^3.0.0",
"snowpack": "3.0.x",
Expand Down
7 changes: 7 additions & 0 deletions test/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
hash.js "^1.0.0"
tslib "^2.0.3"

"@greymass/return-path@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@greymass/return-path/-/return-path-0.0.2.tgz#053ca57eeeea5da970081ef3c818718a7a502f53"
integrity sha512-K1/AMGnLi3J3yhOfZPAwU4pxkN12dHt9NiL15fssZ7Kk1vO+m7k3c/T6kR5l8XnfPepeAdVlewRrsBE4gY6QIQ==
dependencies:
tslib "^2.1.0"

"@snowpack/plugin-typescript@^1.1.1":
version "1.2.1"
resolved "https://registry.yarnpkg.com/@snowpack/plugin-typescript/-/plugin-typescript-1.2.1.tgz#7170b039d16d41963cc61a714fe7c37f3fdd9d51"
Expand Down
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,13 @@
dependencies:
tslib "^2.1.0"

"@greymass/return-path@^0.0.2":
version "0.0.2"
resolved "https://registry.yarnpkg.com/@greymass/return-path/-/return-path-0.0.2.tgz#053ca57eeeea5da970081ef3c818718a7a502f53"
integrity sha512-K1/AMGnLi3J3yhOfZPAwU4pxkN12dHt9NiL15fssZ7Kk1vO+m7k3c/T6kR5l8XnfPepeAdVlewRrsBE4gY6QIQ==
dependencies:
tslib "^2.1.0"

"@humanwhocodes/config-array@^0.9.2":
version "0.9.5"
resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"
Expand Down