Source: agent.ts

  1. import { AgentInfo } from "./types";
  2. import { hasUserAgentData } from "./utils";
  3. import { getClientHintsAgent } from "./userAgentData";
  4. import { getLegacyAgent } from "./userAgent";
  5. /**
  6. * @namespace eg.agent
  7. */
  8. /**
  9. * Extracts accuate browser and operating system information from the user agent string or client hints.
  10. * @ko 유저 에이전트 문자열 또는 client hints에서 정확한 브라우저와 운영체제 정보를 추출한다.
  11. * @function eg.agent#getAccurateAgent
  12. * @param - Callback function to get the accuate agent <ko>정확한 에이전트를 가져오기 위한 callback 함수</ko>
  13. * @return - get the accuate agent promise. If Promise are not supported, null is returned. <ko> 정확한 에이전트 promise를 가져온다. Promise를 지원 하지 않는 경우, null을 반환한다. </ko>
  14. * @example
  15. import { getAccurateAgent } from "@egjs/agent";
  16. // eg.agent.getAccurateAgent()
  17. getAccurateAgent().then(agent => {
  18. const { os, browser, isMobile } = agent;
  19. });
  20. getAccurateAgent(agent => {
  21. const { os, browser, isMobile } = agent;
  22. });
  23. */
  24. export function getAccurateAgent(callback?: (result: AgentInfo) => void): Promise<AgentInfo> | null {
  25. if (hasUserAgentData()) {
  26. return navigator.userAgentData.getHighEntropyValues([
  27. "architecture",
  28. "model",
  29. "platform",
  30. "platformVersion",
  31. "uaFullVersion",
  32. "fullVersionList",
  33. ]).then(info => {
  34. const agentInfo = getClientHintsAgent(info);
  35. callback && callback(agentInfo);
  36. return agentInfo;
  37. });
  38. }
  39. callback && callback(agent());
  40. if (typeof Promise === "undefined" || !Promise) {
  41. return null;
  42. }
  43. return Promise.resolve(agent());
  44. }
  45. /**
  46. * Extracts browser and operating system information from the user agent string.
  47. * @ko 유저 에이전트 문자열에서 브라우저와 운영체제 정보를 추출한다.
  48. * @function eg.agent#agent
  49. * @param - user agent string to parse <ko>파싱할 유저에이전트 문자열</ko>
  50. * @return - agent Info <ko> 에이전트 정보 </ko>
  51. * @example
  52. import agent from "@egjs/agent";
  53. // eg.agent();
  54. const { os, browser, isMobile } = agent();
  55. */
  56. function agent(userAgent?: string): AgentInfo {
  57. if (typeof userAgent === "undefined" && hasUserAgentData()) {
  58. return getClientHintsAgent();
  59. } else {
  60. return getLegacyAgent(userAgent);
  61. }
  62. }
  63. export { getLegacyAgent };
  64. export default agent;
  65. export * from "./types";
comments powered by Disqus