18

I am developing a Chrome extension, and I need to detect which operating system Chrome is running on but i can't seem to find any info on how to do it. Please Help. Thank You.

3 Answers 3

23

Recently added, you can use the getPlatformInfo method in Chrome's own API:

chrome.runtime.getPlatformInfo(function(info) {
    // Display host OS in the console
    console.log(info.os);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Note that it won't work in content script
6

You would need to search for OS name and version inside window.navigator.appVersion.

If you just want to know a platform, see this answer. You can enhance the code by searching for other user agents.

Here is a detailed list of Chrome user agent strings (click on links to see what they mean).

1 Comment

I'd rather use navigator.platform
0

I guess navigator.platform would be more suitable but for me the following script that I often use based on navigator.platform suffices:

const ua = navigator.userAgent

const getBrowser = (): 'Opera' | 'Chrome' | 'Firefox' | 'Safari' | 'IE' | 'Edge' | 'Unknown' | undefined => {
  if (ua.indexOf('Opera') > -1) {
    return 'Opera'
  }
  if (ua.indexOf('Chrome') > -1) {
    return 'Chrome'
  }
  if (ua.indexOf('Firefox') > -1) {
    return 'Firefox'
  }
  if (ua.indexOf('Safari') > -1) {
    return 'Safari'
  }
  if (ua.indexOf('MSIE') > -1) {
    return 'IE'
  }
  if (ua.indexOf('Trident') > -1) {
    return 'IE'
  }
  if (ua.indexOf('Edge' || 'Chrome') > -1) {
    return 'Edge'
  }
  return 'Unknown'
}

const getOS = (): 'Windows' | 'Mac' | 'Linux' | 'Android' | 'iOS' | 'Unknown' => {
  if (ua.indexOf('Windows') > -1) {
    return 'Windows'
  }
  if (ua.indexOf('Mac') > -1) {
    return 'Mac'
  }
  if (ua.indexOf('Linux') > -1) {
    return 'Linux'
  }
  if (ua.indexOf('Android') > -1) {
    return 'Android'
  }
  if (ua.indexOf('iPhone') > -1) {
    return 'iOS'
  }
  if (ua.indexOf('iPad') > -1) {
    return 'iOS'
  }
  if (ua.indexOf('iPod') > -1) {
    return 'iOS'
  }
  return 'Unknown'
}

export { getBrowser, getOS }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.