Reputation: 962
Need a node-js program which can detect the Installed Browsers(or Applications) on the local machine
Upvotes: 1
Views: 752
Reputation: 962
I stumbled upon this question for a few days and found the following links:-
Then I came up with my own version. This assumes that the browser/application is installed in the /Application directory.
var {
spawn
} = require('child_process');
const fs = require("fs"); // Or `import fs from "fs";` with ESM
function detectAllBrowsers(callback) {
const browsers = [
'Chromium',
'Firefox',
'Google Chrome',
'Opera',
'Safari',
'TextEdit'
]
var detectedBrowsers = [];
var promises = [];
browsers.forEach(function(browserName, index) {
// /Applications/Google\ Chrome.app/Contents/Info.plist
var path = '/Applications/' + browserName + '.app';
var aPromise = new Promise(function(resolve, reject) {
if (fs.existsSync(path)) {
var aBrowser = {
name: browserName,
path: path
}
var sp = spawn('/usr/libexec/PlistBuddy', ['-c', 'print :CFBundleShortVersionString', path + '/Contents/Info.plist']);
sp.stdout.setEncoding('utf8')
sp.stdout.on('data', data => {
aBrowser.version = data.trim();
});
sp.on('close', code => {
detectedBrowsers.push(aBrowser);
resolve('done');
});
} else {
resolve('done');
}
});
promises.push(aPromise);
});
Promise.all(promises).then(data => {
callback(detectedBrowsers);
});
}
detectAllBrowsers(function(detectedBrowsers) {
console.log(detectedBrowsers);
});
Upvotes: 2