我可以告诉我的Chrome扩展程序是否在Windows上运行?

问题描述:

在我的Google Chrome扩展程序中,我需要将文本复制到剪贴板上,并且需要知道我的扩展程序是否安装在Windows操作系统上。这可能吗?

In my Google Chrome Extension I need to copy text onto the Clipboard, and I need to know if my extension is installed on Windows OS or not. Is it possible?

PS。如果它是Windows,那么我将用\r\\\
来替换结束行,这会在Windows上使多行文本看起来更好。

PS. If it is Windows, then I will replace end-lines with "\r\n", which makes multi-line text look better on Windows.

有两种方法,至少

Two ways, at least


  1. 您可以简单地依靠 navigator.platform

更好的选择是使用Chrome API: chrome.runtime.getPlatformInfo()

Better option is to use Chrome API: chrome.runtime.getPlatformInfo():


chrome.runtime.getPlatformInfo(函数回调)

返回有关当前平台的信息。

Returns information about the current platform.

PlatformInfo对象

In the form of a PlatformInfo object.

chrome.runtime.getPlatformInfo( function(info) {
  if(info.os == "win") { /* do stuff */ }
});