簡體   English   中英

如何使用java腳本檢查chrome瀏覽器是否安裝?

[英]How to check whether chrome browser is installed or not using java script?

我的要求是我需要檢查客戶端計算機上是否安裝了 Chrome 瀏覽器或不使用 Javascript。 我在網上搜索了無法找到出路。 請幫助完成這項工作。

你不能用 JavaScript 做到這一點,即使你可以,你也不應該這樣做。

出於很好的原因,客戶端上的 JavaScript 無法訪問用戶的系統。 (想想,有惡意的服務器。)

您可以使用下一個代碼檢查瀏覽器是否為 Chrome

if(!window.chrome){
   //Chrome code
}else{
   // Chrome block
}

你不能。 不使用 JavaScript。 但是,您可以檢查當前用於查看您的網頁的瀏覽器是否是 Google Chrome。

<script type="text/javascript">
if(window.chrome){
document.write("Browser is Chrome");
}
else{
document.write("Please download Chrome");
}
</script>

您無法直接從 javascript 獲取此類信息。

您可以做的是在腳本中使用該 PowerShell 命令並將結果保存在文件中,稍后您將使用 javascript 讀取該文件。

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation, Publisher, InstallDate | Format-Table -AutoSize

這將從HKEY_LOCAL_MACHINE注冊表文件夾中獲取機器上所有已安裝的程序。

從中檢索信息的文件夾的確切路徑是: HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\

給定的命令將在 PowerShell 終端中顯示application name名稱及其versioninstall locationpublisher nameinstallation date

如果您想將該列表輸出到文件中,只需在命令后添加>FileName.txt ,然后按 Enter 鍵。

請注意,默認情況下,該文件將在C:\Users\YourUserName\文件夾中創建,因此如果您希望在特定位置創建文件,則必須在執行之前使用CD命令到達該特定位置Get-Item-Property命令。

這將使您完成機器部件上的安裝程序

現在我們可以檢查是否在機器部分安裝了 app x

首先在您的 js 應用程序中加載先前生成的文件,您將使用它的內容來確定計算機上是否安裝了應用程序。

如果安裝了“chrome”,更快的方法是將文件加載為字符串,然后執行基本操作:

if (string.includes('chrome') == true) {
    // chrome is installed on the machine 
    // you can do some more stuff 
    // like extracting it's path from the file content

} else {
    console.log('error: chrome is not installed on this computer');
}

不用說,這僅在您要檢查已安裝應用程序的同一台計算機上使用時才有效。

編輯:如果您想在 javascript 中使用更實用的文件,您可以替換

Format-Table -AutoSize >FileName.txt

和 :

 Export-Csv -path .\FileName.txt -NoTypeInformation

這樣,您可以使用string.split(',')方法拆分文件行,而不必做一些額外的事情來處理數據之間的空格。

編輯 2:這是一個完整的工作實現,可讓您使用 NodeJs 直接從 javascript 從 PowerShell 腳本中檢索信息。

get_programs.ps1(PowerShell 腳本文件):

chcp 65001 # sets the encoding for displaying chars correctly
Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850 # restores the default encoding set this will avoid police changes due to the terminal modifications

注意命令末尾的更改,現在是:
| ConvertTo-Csv -NoTypeInformation
這允許在 PowerShell 終端中以 csv 格式記錄數據,這將簡化它作為字符串的解析。

如果您不想使用另一個文件來保存這幾個 PowerShell 命令,您可以使用它

child = spawn("powershell.exe",[`chcp 65001
Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, InstallLocation | ConvertTo-Csv -NoTypeInformation
chcp 850`]);

作為替代品

 child = spawn("powershell.exe",["./get_programs.ps1"]);

如果您選擇這樣做,請不要忘記轉義\字符,否則它將不起作用。

應用程序.js:

var spawn = require("child_process").spawn,child;
child = spawn("powershell.exe",["./get_programs.ps1"]); // here we start our PowerShell script "./" means that it's in the same directory as the .js file

let chromeDetails;

child.stdout.on("data", (data) => { // data event
    // here we receive each outputed line in the PowerShell terminal as an Uint8Array

    if (data.includes('Chrome')) { // check for the 'Chrome' string in data 
        chromeDetails = data.toString(); // adds data converted as string
    }
});

child.stderr.on("data", (data) => { // logs errors
    console.log(`Powershell Errors: ${data}`);
});

child.on("exit", () => { // exit event
    console.log("Powershell Script finished");
    
    if (chromeDetails != undefined) {
        console.log(`> chrome has been detected on this computer
    available informations (appName, version, installPath):
    ${chromeDetails}`);
    
    } else
        console.log('> chrome has not been detected on this computer');
    
});

child.stdin.end(); // we end the child

預期輸出:

Powershell腳本完成
> 在這台計算機上檢測到 chrome
可用信息(appName、版本、安裝路徑):
"谷歌瀏覽器","103.0.5060.114","C:\Program Files\Google\Chrome\Application"

如果您不在 Windows 上,您可能需要查看 NodeJs 文檔中的 Spawning .bat 和 .cmd 文件在 Windows 上,以獲取有關如何調整上述app.js代碼以在您的系統上工作的提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM