Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

added device details function in dom functions #163

Merged
merged 3 commits into from
Mar 16, 2023
Merged
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
59 changes: 57 additions & 2 deletions packages/ella/src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import { isEmpty } from '../general';
import {
CUSTOM_EVENTS,
OS_TYPES
OS_TYPES,
PLATFORM
} from '../utils/constants';

/**
Expand Down Expand Up @@ -868,7 +869,7 @@ export function dataURIToBlob(dataURI: string) {
* To read more on this: https://stackoverflow.com/questions/54424729/ios-show-keyboard-on-input-focus
*
*/
export function forceFocusAndOpenKeyboard(element:HTMLElement, timeout = 0) {
export function forceFocusAndOpenKeyboard(element: HTMLElement, timeout = 0) {
try {
const platformType = getOSName();

Expand Down Expand Up @@ -913,3 +914,57 @@ export function forceFocusAndOpenKeyboard(element:HTMLElement, timeout = 0) {
console.error('Unable to force focus input', err);
}
}

/**
* This method can be used to get the Device details like browser name, user agent, os name and origin i.e desktop or mobile website.
*
* @remarks
* This method depends on userAgent sniffing and therefore susciptible to spoofing. Avoid detecting browsers in business impacting code
*
* @example
* ```
* console.log('Get Device Details - ',getDeviceDetails());
* ```
*/
export function getDeviceDetails() {
try {
if (typeof window !== 'undefined' && navigator != null) {

const browserName = getBrowserName();
vikaz-singh marked this conversation as resolved.
Show resolved Hide resolved

const OSName = getOSName();

let origin = null;
const nAgt = navigator.userAgent;

//Checking here if any of these matches the given user agents then setting origin to mobile website else setting it to Desktop
if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini|Mobile|mobile|CriOS/i.test(nAgt)) {
origin = PLATFORM.MOBILE;

} else {
origin = PLATFORM.DESKTOP;
}


const finalPayload = {
browserName: browserName,
userAgent: navigator.userAgent,
OSName: OSName,
origin: origin //Origin in this is the platform instance like Desktop or Msite
};

return finalPayload;

}

} catch (err) {
console.error(`Error with getDeviceDetails ${err}`);

dispatchCustomEvent(CUSTOM_EVENTS.TRACK_LOG, {
function: 'getDeviceDetails',
error: err
});
}

return '';
}
6 changes: 6 additions & 0 deletions packages/ella/src/utils/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ export const CUSTOM_EVENTS = {
TABLE_CLOSE: 'tableClose',
TRACK_LOG: 'trackLog'
};


export const PLATFORM = {
MOBILE: 'MSite',
DESKTOP: 'Desktop'
};