@cardsjd/device 0.1.4 → 0.1.6

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.
Files changed (3) hide show
  1. package/lib/device.js +136 -125
  2. package/lib/index.js +3 -3
  3. package/package.json +20 -21
package/lib/device.js CHANGED
@@ -1,125 +1,136 @@
1
- import '@sagi.io/globalthis';
2
- import { Capacitor } from '@capacitor/core';
3
-
4
- export default class Device {
5
- constructor() {
6
- this.onBackButton = null;
7
- document.addEventListener('deviceready', () => this.onDeviceReady(), false);
8
- }
9
-
10
- onDeviceReady() {
11
- document.addEventListener('backbutton', (e) => this.backButton(e), false);
12
-
13
- globalThis.AndroidFullScreen?.immersiveMode?.();
14
- }
15
-
16
- backButton(e) {
17
- if (typeof this.onBackButton === 'function') {
18
- e.preventDefault();
19
- this.onBackButton();
20
- }
21
- }
22
-
23
- exitApp() {
24
- if (typeof navigator.app?.exitApp === 'function') {
25
- navigator.app.exitApp();
26
- } else if (typeof navigator.device?.exitApp === 'function') {
27
- navigator.device.exitApp();
28
- } else {
29
- window.close();
30
- }
31
- }
32
-
33
- /**
34
- * @returns {OSType}
35
- */
36
- getOS() {
37
- const ua = navigator.userAgent;
38
-
39
- if (/(MSAppHost\/3.0)/i.test(navigator.appVersion)) {
40
- // Windows 10 UWP
41
- return 'windows';
42
- }
43
-
44
- const isKindle = /Kindle|Silk|Amazon|KFDOWI|KFAPWA|KFSAW|KFGIWI|KFGIWI|KFAPW|KFARWI|KFTBWI|KFASWI|KFFOWI|KFJW|KFMEWI|KFOT|KFSAW|KFSOWI|KFTBW|KFTHW|KFTT|WFFOWI|KFJWA|KFJWI|KFTHWA|KFTHWI|KFAPWI|KFKAWI/i.test(ua);
45
- if (/(android)/i.test(navigator.userAgent) && isKindle) {
46
- return 'amazon';
47
- }
48
-
49
- if (/(android)/i.test(navigator.userAgent) && !isKindle) {
50
- return 'android';
51
- }
52
-
53
- if (/(ipod|iphone|ipad)/i.test(navigator.userAgent) || navigator.platform === 'MacIntel') {
54
- return 'ios';
55
- }
56
-
57
- if (/(win)/i.test(navigator.userAgent)) {
58
- return 'windows';
59
- }
60
-
61
- return 'web';
62
- }
63
-
64
- /** @typedef {'FBMessenger' | 'windowsPhone' | 'windows' | 'amazon' | 'android' | 'ios' | 'Steam' | 'windowsPWA' | 'web'} OSType */
65
-
66
- getBrowser() {
67
- return navigator.userAgent;
68
- }
69
-
70
- /**
71
- * @returns {PlatformType}
72
- */
73
- getPlatform() {
74
- //facebook instant
75
- if (typeof globalThis.FBInstant !== 'undefined') {
76
- return 'FBInstant';
77
- }
78
-
79
- //native Cordova
80
- if (typeof globalThis.cordova !== 'undefined') {
81
- const isKindle = /Kindle|Silk|Amazon|KFDOWI|KFAPWA|KFSAW|KFGIWI|KFGIWI|KFAPW|KFARWI|KFTBWI|KFASWI|KFFOWI|KFJW|KFMEWI|KFOT|KFSAW|KFSOWI|KFTBW|KFTHW|KFTT|WFFOWI|KFJWA|KFJWI|KFTHWA|KFTHWI|KFAPWI|KFKAWI/i.test(navigator.userAgent);
82
- if (/(android)/i.test(navigator.userAgent) && isKindle) {
83
- return 'amazon';
84
- }
85
-
86
- if (/(android)/i.test(navigator.userAgent) && !isKindle) {
87
- return 'android';
88
- }
89
-
90
- if (/(ipod|iphone|ipad)/i.test(navigator.userAgent) || navigator.platform === 'MacIntel') {
91
- return 'ios';
92
- }
93
- }
94
-
95
- //native Capacitor
96
- if (Capacitor.isNativePlatform() === true) {
97
- if (Capacitor.getPlatform() === 'ios') {
98
- return 'ios';
99
- }
100
-
101
- if (Capacitor.getPlatform() === 'android') {
102
- return 'android';
103
- }
104
-
105
- return Capacitor.getPlatform();
106
- }
107
-
108
- //stream
109
- const searchParams = new URLSearchParams(window.location.search);
110
- if (searchParams.get('steamId')) {
111
- return 'steam';
112
- }
113
-
114
- //pwa + edg = Edge
115
- const isInStandaloneMode = (window.matchMedia('(display-mode: standalone)').matches) || (globalThis.navigator.standalone) || document.referrer.includes('android-app://');
116
- if (isInStandaloneMode && /(Edg)/i.test(navigator.userAgent)) {
117
- return 'windowsPWA';
118
- }
119
-
120
- return 'web';
121
- }
122
-
123
- /** @typedef {'FBInstant' | 'windowsPhone' | 'windows' | 'amazon' | 'android' | 'ios' | 'steam' | 'windowsPWA' | 'web'} PlatformType */
124
-
125
- }
1
+ import '@sagi.io/globalthis';
2
+ import { Capacitor } from '@capacitor/core';
3
+
4
+ export default class Device {
5
+ constructor() {
6
+ this.onBackButton = null;
7
+ document.addEventListener('deviceready', () => this.onDeviceReady(), false);
8
+ }
9
+
10
+ onDeviceReady() {
11
+ document.addEventListener('backbutton', (e) => this.backButton(e), false);
12
+
13
+ globalThis.AndroidFullScreen?.immersiveMode?.();
14
+ }
15
+
16
+ backButton(e) {
17
+ if (typeof this.onBackButton === 'function') {
18
+ e.preventDefault();
19
+ this.onBackButton();
20
+ }
21
+ }
22
+
23
+ exitApp() {
24
+ if (typeof navigator.app?.exitApp === 'function') {
25
+ navigator.app.exitApp();
26
+ } else if (typeof navigator.device?.exitApp === 'function') {
27
+ navigator.device.exitApp();
28
+ } else {
29
+ window.close();
30
+ }
31
+ }
32
+
33
+ /**
34
+ * @returns {OSType}
35
+ */
36
+ getOS() {
37
+ const ua = navigator.userAgent;
38
+
39
+ if (/(MSAppHost\/3.0)/i.test(navigator.appVersion)) {
40
+ // Windows 10 UWP
41
+ return 'windows';
42
+ }
43
+
44
+ const isKindle = /Kindle|Silk|Amazon|KFDOWI|KFAPWA|KFSAW|KFGIWI|KFGIWI|KFAPW|KFARWI|KFTBWI|KFASWI|KFFOWI|KFJW|KFMEWI|KFOT|KFSAW|KFSOWI|KFTBW|KFTHW|KFTT|WFFOWI|KFJWA|KFJWI|KFTHWA|KFTHWI|KFAPWI|KFKAWI/i.test(ua);
45
+ if (/(android)/i.test(navigator.userAgent) && isKindle) {
46
+ return 'amazon';
47
+ }
48
+
49
+ if (/(android)/i.test(navigator.userAgent) && !isKindle) {
50
+ return 'android';
51
+ }
52
+
53
+ if (/(ipod|iphone|ipad)/i.test(navigator.userAgent) || navigator.platform === 'MacIntel') {
54
+ return 'ios';
55
+ }
56
+
57
+ if (/(win)/i.test(navigator.userAgent)) {
58
+ return 'windows';
59
+ }
60
+
61
+ return 'web';
62
+ }
63
+
64
+ /** @typedef {'FBMessenger' | 'windowsPhone' | 'windows' | 'amazon' | 'android' | 'ios' | 'Steam' | 'windowsPWA' | 'web'} OSType */
65
+
66
+ getBrowser() {
67
+ return navigator.userAgent;
68
+ }
69
+
70
+ /**
71
+ * @returns {PlatformType}
72
+ */
73
+ getPlatform() {
74
+
75
+ //Get override information
76
+ const searchParams = new URLSearchParams(window.location.search);
77
+
78
+ if (searchParams.has('platform')) {
79
+ return searchParams.get('platform');
80
+ }
81
+
82
+ //facebook instant
83
+ if (typeof globalThis.FBInstant !== 'undefined') {
84
+ return 'FBInstant';
85
+ }
86
+
87
+ if (typeof globalThis.CrazyGames !== 'undefined') {
88
+ return 'CrazyGames';
89
+ }
90
+
91
+ //native Cordova
92
+ if (typeof globalThis.cordova !== 'undefined') {
93
+ const isKindle = /Kindle|Silk|Amazon|KFDOWI|KFAPWA|KFSAW|KFGIWI|KFGIWI|KFAPW|KFARWI|KFTBWI|KFASWI|KFFOWI|KFJW|KFMEWI|KFOT|KFSAW|KFSOWI|KFTBW|KFTHW|KFTT|WFFOWI|KFJWA|KFJWI|KFTHWA|KFTHWI|KFAPWI|KFKAWI/i.test(navigator.userAgent);
94
+ if (/(android)/i.test(navigator.userAgent) && isKindle) {
95
+ return 'amazon';
96
+ }
97
+
98
+ if (/(android)/i.test(navigator.userAgent) && !isKindle) {
99
+ return 'android';
100
+ }
101
+
102
+ if (/(ipod|iphone|ipad)/i.test(navigator.userAgent) || navigator.platform === 'MacIntel') {
103
+ return 'ios';
104
+ }
105
+ }
106
+
107
+ //native Capacitor
108
+ if (Capacitor.isNativePlatform() === true) {
109
+ if (Capacitor.getPlatform() === 'ios') {
110
+ return 'ios';
111
+ }
112
+
113
+ if (Capacitor.getPlatform() === 'android') {
114
+ return 'android';
115
+ }
116
+
117
+ return Capacitor.getPlatform();
118
+ }
119
+
120
+ //steam
121
+ if (searchParams.get('steamId')) {
122
+ return 'steam';
123
+ }
124
+
125
+ //pwa + edg = Edge
126
+ const isInStandaloneMode = (window.matchMedia('(display-mode: standalone)').matches) || (globalThis.navigator.standalone) || document.referrer.includes('android-app://');
127
+ if (isInStandaloneMode && /(Edg)/i.test(navigator.userAgent)) {
128
+ return 'windowsPWA';
129
+ }
130
+
131
+ return 'web';
132
+ }
133
+
134
+ /** @typedef {'FBInstant' | 'windowsPhone' | 'windows' | 'amazon' | 'android' | 'ios' | 'steam' | 'windowsPWA' | 'web' | 'CrazyGames'} PlatformType */
135
+
136
+ }
package/lib/index.js CHANGED
@@ -1,3 +1,3 @@
1
- import Device from './device';
2
-
3
- export { Device };
1
+ import Device from './device';
2
+
3
+ export { Device };
package/package.json CHANGED
@@ -1,21 +1,20 @@
1
- {
2
- "name": "@cardsjd/device",
3
- "private": false,
4
- "version": "0.1.4",
5
- "type": "module",
6
- "main": "lib/index.js",
7
- "files": [
8
- "lib"
9
- ],
10
- "scripts": {
11
- "lint": "eslint lib --report-unused-disable-directives --max-warnings 0",
12
- "lint:fix": "npm run lint -- --fix"
13
- },
14
- "dependencies": {
15
- "@capacitor/core": "^6.0.0"
16
- },
17
- "peerDependencies": {
18
- "@cardsjd/device": "^0.1.4",
19
- "@sagi.io/globalthis": "^0.0.2"
20
- }
21
- }
1
+ {
2
+ "name": "@cardsjd/device",
3
+ "private": false,
4
+ "version": "0.1.6",
5
+ "type": "module",
6
+ "main": "lib/index.js",
7
+ "files": [
8
+ "lib"
9
+ ],
10
+ "scripts": {
11
+ "lint": "eslint lib --report-unused-disable-directives --max-warnings 0",
12
+ "lint:fix": "npm run lint -- --fix"
13
+ },
14
+ "dependencies": {
15
+ "@capacitor/core": "^6.0.0"
16
+ },
17
+ "peerDependencies": {
18
+ "@sagi.io/globalthis": "^0.0.2"
19
+ }
20
+ }