universal-app-opener 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +43 -53
- package/dist/index.d.mts +14 -2
- package/dist/index.d.ts +14 -2
- package/dist/index.js +92 -18
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -18
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 universal-app-opener
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
CHANGED
|
@@ -18,86 +18,76 @@ yarn add universal-app-opener
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
-
###
|
|
21
|
+
### One-Click Open (Recommended)
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
import { generateDeepLink, detectOS } from 'universal-app-opener';
|
|
23
|
+
The simplest way to open a link - automatically detects platform and opens the appropriate app or web URL:
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
```typescript
|
|
26
|
+
import { openLink } from 'universal-app-opener';
|
|
28
27
|
|
|
29
|
-
|
|
30
|
-
// {
|
|
31
|
-
// webUrl: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
|
|
32
|
-
// ios: 'vnd.youtube://watch?v=dQw4w9WgXcQ',
|
|
33
|
-
// android: 'intent://watch?v=dQw4w9WgXcQ#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end',
|
|
34
|
-
// platform: 'youtube'
|
|
35
|
-
// }
|
|
28
|
+
openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
36
29
|
```
|
|
37
30
|
|
|
38
|
-
|
|
31
|
+
**With options:**
|
|
39
32
|
|
|
40
33
|
```typescript
|
|
41
|
-
import {
|
|
34
|
+
import { openLink } from 'universal-app-opener';
|
|
42
35
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
window.location.href = result.ios;
|
|
49
|
-
} else if (os === 'android' && result.android) {
|
|
50
|
-
window.location.href = result.android;
|
|
51
|
-
} else {
|
|
52
|
-
window.open(result.webUrl, '_blank');
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
openLink('https://www.linkedin.com/in/iamsaban/');
|
|
36
|
+
openLink('https://www.linkedin.com/in/iamsaban/', {
|
|
37
|
+
fallbackToWeb: true, // Fallback to web if app not installed (default: true)
|
|
38
|
+
fallbackDelay: 2500, // Delay before fallback in ms (default: 2500)
|
|
39
|
+
openInNewTab: false // Open web URL in new tab (default: false)
|
|
40
|
+
});
|
|
57
41
|
```
|
|
58
42
|
|
|
59
|
-
###
|
|
43
|
+
### Advanced Usage
|
|
44
|
+
|
|
45
|
+
If you need more control, you can use the lower-level APIs:
|
|
60
46
|
|
|
61
47
|
```typescript
|
|
62
48
|
import { generateDeepLink, detectOS } from 'universal-app-opener';
|
|
63
49
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
deepLink = result.android;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (deepLink) {
|
|
77
|
-
window.location.href = deepLink;
|
|
78
|
-
setTimeout(() => {
|
|
79
|
-
window.location.href = result.webUrl;
|
|
80
|
-
}, 2500);
|
|
81
|
-
} else {
|
|
82
|
-
window.open(result.webUrl, '_blank');
|
|
83
|
-
}
|
|
50
|
+
const result = generateDeepLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
51
|
+
const os = detectOS();
|
|
52
|
+
|
|
53
|
+
if (os === 'ios' && result.ios) {
|
|
54
|
+
window.location.href = result.ios;
|
|
55
|
+
} else if (os === 'android' && result.android) {
|
|
56
|
+
window.location.href = result.android;
|
|
57
|
+
} else {
|
|
58
|
+
window.open(result.webUrl, '_blank');
|
|
84
59
|
}
|
|
85
60
|
```
|
|
86
61
|
|
|
87
62
|
### CommonJS Usage
|
|
88
63
|
|
|
89
64
|
```javascript
|
|
90
|
-
const {
|
|
65
|
+
const { openLink, generateDeepLink } = require('universal-app-opener');
|
|
91
66
|
|
|
92
|
-
|
|
93
|
-
console.log(result.ios);
|
|
67
|
+
openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
94
68
|
```
|
|
95
69
|
|
|
96
70
|
## API Reference
|
|
97
71
|
|
|
72
|
+
### `openLink(url: string, options?: OpenLinkOptions): void`
|
|
73
|
+
|
|
74
|
+
Opens a URL in the appropriate app or browser. Automatically detects platform and handles deep linking.
|
|
75
|
+
|
|
76
|
+
**Parameters:**
|
|
77
|
+
- `url` (string): The web URL to open (YouTube or LinkedIn)
|
|
78
|
+
- `options` (optional): Configuration object
|
|
79
|
+
- `fallbackToWeb` (boolean): Fallback to web URL if app not installed (default: `true`)
|
|
80
|
+
- `fallbackDelay` (number): Delay in milliseconds before fallback (default: `2500`)
|
|
81
|
+
- `openInNewTab` (boolean): Open web URL in new tab (default: `false`)
|
|
82
|
+
|
|
83
|
+
**Example:**
|
|
84
|
+
```typescript
|
|
85
|
+
openLink('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
|
|
86
|
+
```
|
|
87
|
+
|
|
98
88
|
### `generateDeepLink(url: string): DeepLinkResult`
|
|
99
89
|
|
|
100
|
-
Converts a web URL into platform-specific deep links.
|
|
90
|
+
Converts a web URL into platform-specific deep links. Returns the deep link data without opening it.
|
|
101
91
|
|
|
102
92
|
**Parameters:**
|
|
103
93
|
- `url` (string): The web URL to convert (YouTube or LinkedIn)
|
package/dist/index.d.mts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
|
+
type Platform = 'youtube' | 'linkedin' | 'instagram' | 'unknown';
|
|
1
2
|
interface DeepLinkResult {
|
|
2
3
|
webUrl: string;
|
|
3
4
|
ios: string | null;
|
|
4
5
|
android: string | null;
|
|
5
|
-
platform:
|
|
6
|
+
platform: Platform;
|
|
6
7
|
}
|
|
8
|
+
interface DeepLinkHandler {
|
|
9
|
+
match(url: string): RegExpMatchArray | null;
|
|
10
|
+
build(url: string, match: RegExpMatchArray): DeepLinkResult;
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
declare function generateDeepLink(url: string): DeepLinkResult;
|
|
8
14
|
declare function detectOS(): 'ios' | 'android' | 'desktop';
|
|
15
|
+
interface OpenLinkOptions {
|
|
16
|
+
fallbackToWeb?: boolean;
|
|
17
|
+
fallbackDelay?: number;
|
|
18
|
+
openInNewTab?: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function openLink(url: string, options?: OpenLinkOptions): void;
|
|
9
21
|
|
|
10
|
-
export { type DeepLinkResult, detectOS, generateDeepLink };
|
|
22
|
+
export { type DeepLinkHandler, type DeepLinkResult, type OpenLinkOptions, type Platform, detectOS, generateDeepLink, openLink };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,22 @@
|
|
|
1
|
+
type Platform = 'youtube' | 'linkedin' | 'instagram' | 'unknown';
|
|
1
2
|
interface DeepLinkResult {
|
|
2
3
|
webUrl: string;
|
|
3
4
|
ios: string | null;
|
|
4
5
|
android: string | null;
|
|
5
|
-
platform:
|
|
6
|
+
platform: Platform;
|
|
6
7
|
}
|
|
8
|
+
interface DeepLinkHandler {
|
|
9
|
+
match(url: string): RegExpMatchArray | null;
|
|
10
|
+
build(url: string, match: RegExpMatchArray): DeepLinkResult;
|
|
11
|
+
}
|
|
12
|
+
|
|
7
13
|
declare function generateDeepLink(url: string): DeepLinkResult;
|
|
8
14
|
declare function detectOS(): 'ios' | 'android' | 'desktop';
|
|
15
|
+
interface OpenLinkOptions {
|
|
16
|
+
fallbackToWeb?: boolean;
|
|
17
|
+
fallbackDelay?: number;
|
|
18
|
+
openInNewTab?: boolean;
|
|
19
|
+
}
|
|
20
|
+
declare function openLink(url: string, options?: OpenLinkOptions): void;
|
|
9
21
|
|
|
10
|
-
export { type DeepLinkResult, detectOS, generateDeepLink };
|
|
22
|
+
export { type DeepLinkHandler, type DeepLinkResult, type OpenLinkOptions, type Platform, detectOS, generateDeepLink, openLink };
|
package/dist/index.js
CHANGED
|
@@ -21,25 +21,16 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
23
|
detectOS: () => detectOS,
|
|
24
|
-
generateDeepLink: () => generateDeepLink
|
|
24
|
+
generateDeepLink: () => generateDeepLink,
|
|
25
|
+
openLink: () => openLink
|
|
25
26
|
});
|
|
26
27
|
module.exports = __toCommonJS(index_exports);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const
|
|
33
|
-
return {
|
|
34
|
-
webUrl,
|
|
35
|
-
ios: `vnd.youtube://watch?v=${videoId}`,
|
|
36
|
-
android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,
|
|
37
|
-
platform: "youtube"
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
const linkedinMatch = webUrl.match(/linkedin\.com\/in\/([^/?]+)/);
|
|
41
|
-
if (linkedinMatch) {
|
|
42
|
-
const profileId = linkedinMatch[1];
|
|
28
|
+
|
|
29
|
+
// src/platforms/linkedin.ts
|
|
30
|
+
var linkedinHandler = {
|
|
31
|
+
match: (url) => url.match(/linkedin\.com\/in\/([^/?]+)/),
|
|
32
|
+
build: (webUrl, match) => {
|
|
33
|
+
const profileId = match[1];
|
|
43
34
|
return {
|
|
44
35
|
webUrl,
|
|
45
36
|
ios: `linkedin://in/${profileId}`,
|
|
@@ -47,6 +38,10 @@ function generateDeepLink(url) {
|
|
|
47
38
|
platform: "linkedin"
|
|
48
39
|
};
|
|
49
40
|
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// src/platforms/unknown.ts
|
|
44
|
+
function unknownHandler(webUrl) {
|
|
50
45
|
return {
|
|
51
46
|
webUrl,
|
|
52
47
|
ios: null,
|
|
@@ -54,6 +49,51 @@ function generateDeepLink(url) {
|
|
|
54
49
|
platform: "unknown"
|
|
55
50
|
};
|
|
56
51
|
}
|
|
52
|
+
|
|
53
|
+
// src/platforms/youtube.ts
|
|
54
|
+
var youtubeHandler = {
|
|
55
|
+
match: (url) => url.match(/youtube\.com\/watch\?v=([^&]+)/) ?? url.match(/youtu\.be\/([^?]+)/),
|
|
56
|
+
build: (webUrl, match) => {
|
|
57
|
+
const videoId = match[1];
|
|
58
|
+
return {
|
|
59
|
+
webUrl,
|
|
60
|
+
ios: `vnd.youtube://watch?v=${videoId}`,
|
|
61
|
+
android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,
|
|
62
|
+
platform: "youtube"
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// src/platforms/instagram.ts
|
|
68
|
+
var instagramHandler = {
|
|
69
|
+
match: (url) => url.match(/instagram\.com\/([^/?]+)/),
|
|
70
|
+
build: (webUrl, match) => {
|
|
71
|
+
const username = match[1];
|
|
72
|
+
return {
|
|
73
|
+
webUrl,
|
|
74
|
+
ios: `instagram://user?username=${username}`,
|
|
75
|
+
android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,
|
|
76
|
+
platform: "instagram"
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/index.ts
|
|
82
|
+
var handlers = [
|
|
83
|
+
youtubeHandler,
|
|
84
|
+
linkedinHandler,
|
|
85
|
+
instagramHandler
|
|
86
|
+
];
|
|
87
|
+
function generateDeepLink(url) {
|
|
88
|
+
const webUrl = url.trim();
|
|
89
|
+
for (const handler of handlers) {
|
|
90
|
+
const match = handler.match(webUrl);
|
|
91
|
+
if (match) {
|
|
92
|
+
return handler.build(webUrl, match);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
return unknownHandler(webUrl);
|
|
96
|
+
}
|
|
57
97
|
function detectOS() {
|
|
58
98
|
if (typeof window === "undefined") {
|
|
59
99
|
return "desktop";
|
|
@@ -67,9 +107,43 @@ function detectOS() {
|
|
|
67
107
|
}
|
|
68
108
|
return "desktop";
|
|
69
109
|
}
|
|
110
|
+
function openLink(url, options = {}) {
|
|
111
|
+
const {
|
|
112
|
+
fallbackToWeb = true,
|
|
113
|
+
fallbackDelay = 2500,
|
|
114
|
+
openInNewTab = false
|
|
115
|
+
} = options;
|
|
116
|
+
const os = detectOS();
|
|
117
|
+
const result = generateDeepLink(url);
|
|
118
|
+
let deepLink = null;
|
|
119
|
+
if (os === "ios" && result.ios) {
|
|
120
|
+
deepLink = result.ios;
|
|
121
|
+
} else if (os === "android" && result.android) {
|
|
122
|
+
deepLink = result.android;
|
|
123
|
+
}
|
|
124
|
+
if (deepLink && (os === "ios" || os === "android")) {
|
|
125
|
+
window.location.href = deepLink;
|
|
126
|
+
if (fallbackToWeb) {
|
|
127
|
+
setTimeout(() => {
|
|
128
|
+
if (openInNewTab) {
|
|
129
|
+
window.open(result.webUrl, "_blank");
|
|
130
|
+
} else {
|
|
131
|
+
window.location.href = result.webUrl;
|
|
132
|
+
}
|
|
133
|
+
}, fallbackDelay);
|
|
134
|
+
}
|
|
135
|
+
} else {
|
|
136
|
+
if (openInNewTab) {
|
|
137
|
+
window.open(result.webUrl, "_blank");
|
|
138
|
+
} else {
|
|
139
|
+
window.location.href = result.webUrl;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
70
143
|
// Annotate the CommonJS export names for ESM import in node:
|
|
71
144
|
0 && (module.exports = {
|
|
72
145
|
detectOS,
|
|
73
|
-
generateDeepLink
|
|
146
|
+
generateDeepLink,
|
|
147
|
+
openLink
|
|
74
148
|
});
|
|
75
149
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts"],"sourcesContent":["import { instagramHandler, linkedinHandler, unknownHandler, youtubeHandler } from \"./platforms\";\nimport { DeepLinkResult } from \"./types\";\n\nexport * from './types';\n\nconst handlers = [\n youtubeHandler,\n linkedinHandler,\n instagramHandler\n];\nexport function generateDeepLink(url: string): DeepLinkResult {\n const webUrl = url.trim();\n\n for (const handler of handlers) {\n const match = handler.match(webUrl);\n if (match) {\n return handler.build(webUrl, match);\n }\n }\n\n return unknownHandler(webUrl);\n}\n\nexport function detectOS(): 'ios' | 'android' | 'desktop' {\n if (typeof window === 'undefined') {\n return 'desktop';\n }\n\n const userAgent = window.navigator.userAgent.toLowerCase();\n\n if (/iphone|ipad|ipod/.test(userAgent)) {\n return 'ios';\n }\n\n if (/android/.test(userAgent)) {\n return 'android';\n }\n\n return 'desktop';\n}\n\nexport interface OpenLinkOptions {\n fallbackToWeb?: boolean;\n fallbackDelay?: number;\n openInNewTab?: boolean;\n}\n\nexport function openLink(url: string, options: OpenLinkOptions = {}): void {\n const {\n fallbackToWeb = true,\n fallbackDelay = 2500,\n openInNewTab = false\n } = options;\n\n const os = detectOS();\n const result = generateDeepLink(url);\n\n let deepLink: string | null = null;\n\n if (os === 'ios' && result.ios) {\n deepLink = result.ios;\n } else if (os === 'android' && result.android) {\n deepLink = result.android;\n }\n\n if (deepLink && (os === 'ios' || os === 'android')) {\n window.location.href = deepLink;\n\n if (fallbackToWeb) {\n setTimeout(() => {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }, fallbackDelay);\n }\n } else {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }\n}\n\n","import { DeepLinkHandler } from '../types';\n\nexport const linkedinHandler: DeepLinkHandler = {\n match: (url) => url.match(/linkedin\\.com\\/in\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const profileId = match[1];\n\n return {\n webUrl,\n ios: `linkedin://in/${profileId}`,\n android: `intent://in/${profileId}#Intent;scheme=linkedin;package=com.linkedin.android;end`,\n platform: 'linkedin',\n };\n },\n};\n","import { DeepLinkResult } from '../types';\n\nexport function unknownHandler(webUrl: string): DeepLinkResult {\n return {\n webUrl,\n ios: null,\n android: null,\n platform: 'unknown',\n };\n}\n","import { DeepLinkHandler } from '../types';\n\nexport const youtubeHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(/youtube\\.com\\/watch\\?v=([^&]+)/) ??\n url.match(/youtu\\.be\\/([^?]+)/),\n\n build: (webUrl, match) => {\n const videoId = match[1];\n\n return {\n webUrl,\n ios: `vnd.youtube://watch?v=${videoId}`,\n android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,\n platform: 'youtube',\n };\n },\n};\n","import { DeepLinkHandler } from '../types';\n\nexport const instagramHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(/instagram\\.com\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const username = match[1];\n\n return {\n webUrl,\n ios: `instagram://user?username=${username}`,\n android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,\n platform: 'instagram',\n };\n },\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,kBAAmC;AAAA,EAC9C,OAAO,CAAC,QAAQ,IAAI,MAAM,6BAA6B;AAAA,EAEvD,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,YAAY,MAAM,CAAC;AAEzB,WAAO;AAAA,MACL;AAAA,MACA,KAAK,iBAAiB,SAAS;AAAA,MAC/B,SAAS,eAAe,SAAS;AAAA,MACjC,UAAU;AAAA,IACZ;AAAA,EACF;AACF;;;ACbO,SAAS,eAAe,QAAgC;AAC7D,SAAO;AAAA,IACL;AAAA,IACA,KAAK;AAAA,IACL,SAAS;AAAA,IACT,UAAU;AAAA,EACZ;AACF;;;ACPO,IAAM,iBAAkC;AAAA,EAC7C,OAAO,CAAC,QACN,IAAI,MAAM,gCAAgC,KAC1C,IAAI,MAAM,oBAAoB;AAAA,EAEhC,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,UAAU,MAAM,CAAC;AAEvB,WAAO;AAAA,MACL;AAAA,MACA,KAAK,yBAAyB,OAAO;AAAA,MACrC,SAAS,oBAAoB,OAAO;AAAA,MACpC,UAAU;AAAA,IACZ;AAAA,EACF;AACF;;;ACfO,IAAM,mBAAo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,iBAAW,MAAM;AACf,YAAI,cAAc;AAChB,iBAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,QACrC,OAAO;AACL,iBAAO,SAAS,OAAO,OAAO;AAAA,QAChC;AAAA,MACF,GAAG,aAAa;AAAA,IAClB;AAAA,EACF,OAAO;AACL,QAAI,cAAc;AAChB,aAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,IACrC,OAAO;AACL,aAAO,SAAS,OAAO,OAAO;AAAA,IAChC;AAAA,EACF;AACF;","names":[]}
|
package/dist/index.mjs
CHANGED
|
@@ -1,20 +1,8 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
if (youtubeWatchMatch || youtubeShortMatch) {
|
|
7
|
-
const videoId = youtubeWatchMatch ? youtubeWatchMatch[1] : youtubeShortMatch[1];
|
|
8
|
-
return {
|
|
9
|
-
webUrl,
|
|
10
|
-
ios: `vnd.youtube://watch?v=${videoId}`,
|
|
11
|
-
android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,
|
|
12
|
-
platform: "youtube"
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
const linkedinMatch = webUrl.match(/linkedin\.com\/in\/([^/?]+)/);
|
|
16
|
-
if (linkedinMatch) {
|
|
17
|
-
const profileId = linkedinMatch[1];
|
|
1
|
+
// src/platforms/linkedin.ts
|
|
2
|
+
var linkedinHandler = {
|
|
3
|
+
match: (url) => url.match(/linkedin\.com\/in\/([^/?]+)/),
|
|
4
|
+
build: (webUrl, match) => {
|
|
5
|
+
const profileId = match[1];
|
|
18
6
|
return {
|
|
19
7
|
webUrl,
|
|
20
8
|
ios: `linkedin://in/${profileId}`,
|
|
@@ -22,6 +10,10 @@ function generateDeepLink(url) {
|
|
|
22
10
|
platform: "linkedin"
|
|
23
11
|
};
|
|
24
12
|
}
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
// src/platforms/unknown.ts
|
|
16
|
+
function unknownHandler(webUrl) {
|
|
25
17
|
return {
|
|
26
18
|
webUrl,
|
|
27
19
|
ios: null,
|
|
@@ -29,6 +21,51 @@ function generateDeepLink(url) {
|
|
|
29
21
|
platform: "unknown"
|
|
30
22
|
};
|
|
31
23
|
}
|
|
24
|
+
|
|
25
|
+
// src/platforms/youtube.ts
|
|
26
|
+
var youtubeHandler = {
|
|
27
|
+
match: (url) => url.match(/youtube\.com\/watch\?v=([^&]+)/) ?? url.match(/youtu\.be\/([^?]+)/),
|
|
28
|
+
build: (webUrl, match) => {
|
|
29
|
+
const videoId = match[1];
|
|
30
|
+
return {
|
|
31
|
+
webUrl,
|
|
32
|
+
ios: `vnd.youtube://watch?v=${videoId}`,
|
|
33
|
+
android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,
|
|
34
|
+
platform: "youtube"
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
// src/platforms/instagram.ts
|
|
40
|
+
var instagramHandler = {
|
|
41
|
+
match: (url) => url.match(/instagram\.com\/([^/?]+)/),
|
|
42
|
+
build: (webUrl, match) => {
|
|
43
|
+
const username = match[1];
|
|
44
|
+
return {
|
|
45
|
+
webUrl,
|
|
46
|
+
ios: `instagram://user?username=${username}`,
|
|
47
|
+
android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,
|
|
48
|
+
platform: "instagram"
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// src/index.ts
|
|
54
|
+
var handlers = [
|
|
55
|
+
youtubeHandler,
|
|
56
|
+
linkedinHandler,
|
|
57
|
+
instagramHandler
|
|
58
|
+
];
|
|
59
|
+
function generateDeepLink(url) {
|
|
60
|
+
const webUrl = url.trim();
|
|
61
|
+
for (const handler of handlers) {
|
|
62
|
+
const match = handler.match(webUrl);
|
|
63
|
+
if (match) {
|
|
64
|
+
return handler.build(webUrl, match);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return unknownHandler(webUrl);
|
|
68
|
+
}
|
|
32
69
|
function detectOS() {
|
|
33
70
|
if (typeof window === "undefined") {
|
|
34
71
|
return "desktop";
|
|
@@ -42,8 +79,42 @@ function detectOS() {
|
|
|
42
79
|
}
|
|
43
80
|
return "desktop";
|
|
44
81
|
}
|
|
82
|
+
function openLink(url, options = {}) {
|
|
83
|
+
const {
|
|
84
|
+
fallbackToWeb = true,
|
|
85
|
+
fallbackDelay = 2500,
|
|
86
|
+
openInNewTab = false
|
|
87
|
+
} = options;
|
|
88
|
+
const os = detectOS();
|
|
89
|
+
const result = generateDeepLink(url);
|
|
90
|
+
let deepLink = null;
|
|
91
|
+
if (os === "ios" && result.ios) {
|
|
92
|
+
deepLink = result.ios;
|
|
93
|
+
} else if (os === "android" && result.android) {
|
|
94
|
+
deepLink = result.android;
|
|
95
|
+
}
|
|
96
|
+
if (deepLink && (os === "ios" || os === "android")) {
|
|
97
|
+
window.location.href = deepLink;
|
|
98
|
+
if (fallbackToWeb) {
|
|
99
|
+
setTimeout(() => {
|
|
100
|
+
if (openInNewTab) {
|
|
101
|
+
window.open(result.webUrl, "_blank");
|
|
102
|
+
} else {
|
|
103
|
+
window.location.href = result.webUrl;
|
|
104
|
+
}
|
|
105
|
+
}, fallbackDelay);
|
|
106
|
+
}
|
|
107
|
+
} else {
|
|
108
|
+
if (openInNewTab) {
|
|
109
|
+
window.open(result.webUrl, "_blank");
|
|
110
|
+
} else {
|
|
111
|
+
window.location.href = result.webUrl;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
45
115
|
export {
|
|
46
116
|
detectOS,
|
|
47
|
-
generateDeepLink
|
|
117
|
+
generateDeepLink,
|
|
118
|
+
openLink
|
|
48
119
|
};
|
|
49
120
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/platforms/linkedin.ts","../src/platforms/unknown.ts","../src/platforms/youtube.ts","../src/platforms/instagram.ts","../src/index.ts"],"sourcesContent":["import { DeepLinkHandler } from '../types';\n\nexport const linkedinHandler: DeepLinkHandler = {\n match: (url) => url.match(/linkedin\\.com\\/in\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const profileId = match[1];\n\n return {\n webUrl,\n ios: `linkedin://in/${profileId}`,\n android: `intent://in/${profileId}#Intent;scheme=linkedin;package=com.linkedin.android;end`,\n platform: 'linkedin',\n };\n },\n};\n","import { DeepLinkResult } from '../types';\n\nexport function unknownHandler(webUrl: string): DeepLinkResult {\n return {\n webUrl,\n ios: null,\n android: null,\n platform: 'unknown',\n };\n}\n","import { DeepLinkHandler } from '../types';\n\nexport const youtubeHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(/youtube\\.com\\/watch\\?v=([^&]+)/) ??\n url.match(/youtu\\.be\\/([^?]+)/),\n\n build: (webUrl, match) => {\n const videoId = match[1];\n\n return {\n webUrl,\n ios: `vnd.youtube://watch?v=${videoId}`,\n android: `intent://watch?v=${videoId}#Intent;scheme=vnd.youtube;package=com.google.android.youtube;end`,\n platform: 'youtube',\n };\n },\n};\n","import { DeepLinkHandler } from '../types';\n\nexport const instagramHandler: DeepLinkHandler = {\n match: (url) =>\n url.match(/instagram\\.com\\/([^/?]+)/),\n\n build: (webUrl, match) => {\n const username = match[1];\n\n return {\n webUrl,\n ios: `instagram://user?username=${username}`,\n android: `intent://user?username=${username}#Intent;scheme=instagram;package=com.instagram.android;end`,\n platform: 'instagram',\n };\n },\n};\n","import { instagramHandler, linkedinHandler, unknownHandler, youtubeHandler } from \"./platforms\";\nimport { DeepLinkResult } from \"./types\";\n\nexport * from './types';\n\nconst handlers = [\n youtubeHandler,\n linkedinHandler,\n instagramHandler\n];\nexport function generateDeepLink(url: string): DeepLinkResult {\n const webUrl = url.trim();\n\n for (const handler of handlers) {\n const match = handler.match(webUrl);\n if (match) {\n return handler.build(webUrl, match);\n }\n }\n\n return unknownHandler(webUrl);\n}\n\nexport function detectOS(): 'ios' | 'android' | 'desktop' {\n if (typeof window === 'undefined') {\n return 'desktop';\n }\n\n const userAgent = window.navigator.userAgent.toLowerCase();\n\n if (/iphone|ipad|ipod/.test(userAgent)) {\n return 'ios';\n }\n\n if (/android/.test(userAgent)) {\n return 'android';\n }\n\n return 'desktop';\n}\n\nexport interface OpenLinkOptions {\n fallbackToWeb?: boolean;\n fallbackDelay?: number;\n openInNewTab?: boolean;\n}\n\nexport function openLink(url: string, options: OpenLinkOptions = {}): void {\n const {\n fallbackToWeb = true,\n fallbackDelay = 2500,\n openInNewTab = false\n } = options;\n\n const os = detectOS();\n const result = generateDeepLink(url);\n\n let deepLink: string | null = null;\n\n if (os === 'ios' && result.ios) {\n deepLink = result.ios;\n } else if (os === 'android' && result.android) {\n deepLink = result.android;\n }\n\n if (deepLink && (os === 'ios' || os === 'android')) {\n window.location.href = deepLink;\n\n if (fallbackToWeb) {\n setTimeout(() => {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }, fallbackDelay);\n }\n } else {\n if (openInNewTab) {\n window.open(result.webUrl, '_blank');\n } else {\n window.location.href = result.webUrl;\n }\n }\n}\n\n"],"mappings":";AAEO,IAAM,kBAAmC;AAAA,EAC9C,OAAO,CAAC,QAAQ,IAAI,MAAM,6BAA6B;AAAA,EAEvD,OAAO,CAAC,QAAQ,UAAU;AACxB,UAAM,YAAY,MAAM,CAAC;AAEzB,WAAO;AAAA,MACL;AAAA,MACA,KAAK,iBAAiB,SAAS;AAAA,MAC/B,SAAS,eAAe,SAAS;AAAA,MACjC,UAAU;AAAA,IACZ;AAAA,EACF;AACF;;;ACbO,SAAS,eAAe,QAAgC;AAC7D,SAAO;AAAA,IACL;AAAA,IACA,KAAK;AAAA,IACL,SAAS;AAAA,IACT,UAAU;AAAA,EACZ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A,IAChB,eAAe;AAAA,EACjB,IAAI;AAEJ,QAAM,KAAK,SAAS;AACpB,QAAM,SAAS,iBAAiB,GAAG;AAEnC,MAAI,WAA0B;AAE9B,MAAI,OAAO,SAAS,OAAO,KAAK;AAC9B,eAAW,OAAO;AAAA,EACpB,WAAW,OAAO,aAAa,OAAO,SAAS;AAC7C,eAAW,OAAO;AAAA,EACpB;AAEA,MAAI,aAAa,OAAO,SAAS,OAAO,YAAY;AAClD,WAAO,SAAS,OAAO;AAEvB,QAAI,eAAe;AACjB,iBAAW,MAAM;AACf,YAAI,cAAc;AAChB,iBAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,QACrC,OAAO;AACL,iBAAO,SAAS,OAAO,OAAO;AAAA,QAChC;AAAA,MACF,GAAG,aAAa;AAAA,IAClB;AAAA,EACF,OAAO;AACL,QAAI,cAAc;AAChB,aAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,IACrC,OAAO;AACL,aAAO,SAAS,OAAO,OAAO;AAAA,IAChC;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "universal-app-opener",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "A zero-dependency library to generate deep links from HTTP URLs",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -17,11 +17,6 @@
|
|
|
17
17
|
"dist",
|
|
18
18
|
"README.md"
|
|
19
19
|
],
|
|
20
|
-
"scripts": {
|
|
21
|
-
"build": "tsup",
|
|
22
|
-
"dev": "tsup --watch",
|
|
23
|
-
"prepublishOnly": "pnpm build"
|
|
24
|
-
},
|
|
25
20
|
"keywords": [
|
|
26
21
|
"deep-link",
|
|
27
22
|
"mobile",
|
|
@@ -43,5 +38,9 @@
|
|
|
43
38
|
"devDependencies": {
|
|
44
39
|
"tsup": "^8.0.0",
|
|
45
40
|
"typescript": "^5.3.0"
|
|
41
|
+
},
|
|
42
|
+
"scripts": {
|
|
43
|
+
"build": "tsup",
|
|
44
|
+
"dev": "tsup --watch"
|
|
46
45
|
}
|
|
47
|
-
}
|
|
46
|
+
}
|