Skip to content

Instantly share code, notes, and snippets.

@Adam-S-Amir
Last active May 2, 2023 04:46
Show Gist options
  • Save Adam-S-Amir/c686a1504ed531f366ad82b77a76f1fe to your computer and use it in GitHub Desktop.
Save Adam-S-Amir/c686a1504ed531f366ad82b77a76f1fe to your computer and use it in GitHub Desktop.
NaCL Browser Compatibility Checker

NaCL Chrome plugin checker Add this to HTML

<style>
    * {
      overflow: hidden;
    }
    #Selection {
      display: block;
      position: absolute;
      top: 1;
      left: 5;
      visibility: visible;
    }
  </style>
  <div id="Selection">
    <h3 id="checker">Checking browser compatibility, please wait...</h3>
  </div>
  <script src="https://cdn.jsdelivr.net/gh/MagnusMarx/NaCL/check_browser.js"></script>
  <script src="https://cdn.jsdelivr.net/gh/MagnusMarx/NaCL/js/check.js"></script>
function HandleMessage(event) {
console.log(event.data);
var data = JSON.parse(event.data);
console.log(data)
console.log(data[0])
console.log(data[0] == 'ReadDirectory')
if (data[0] == 'ReadDirectory') {
HandlePepperMountMessage(data);
}
}
function Start() {
document.getElementById("Selection").style.display = "none";
}
let help = "NaCl disabled: Native Client is not enabled.<br>" + "Please go to <em id='link' href='chrome://flags#enable-nacl' target='_blank'>chrome://flags#enable-nacl</em> and enable the Native Client plugin.";
function checkBrowser() {
var isValidBrowser = false;
var browserSupportStatus = 0;
var checker = new browser_version.BrowserChecker(15, // Minumum Chrome version.
navigator["appVersion"],
navigator["plugins"]);
checker.checkBrowser();
isValidBrowser = checker.getIsValidBrowser();
browserSupportStatus = checker.getBrowserSupportStatus();
switch (browserSupportStatus) {
case browser_version.BrowserChecker.StatusValues.NACL_ENABLED:
console.log('Native Client plugin enabled.');
document.getElementById("Selection").style.display = "none";
break;
case browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER:
console.log('UNKNOWN BROWSER');
break;
case browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD:
console.log('Chrome too old: You must use Chrome version 15 or later.');
console.log('NEED CHROME 15 OR LATER');
break;
case browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED:
console.log(help);
document.getElementById('checker').innerHTML = "" + help + "";
let link = document.getElementById('link');
link.onclick = function () {
let link2 = 'chrome://flags#enable-nacl';
window.open(link2, "_blank");
}
console.log('NativeClient NOT ENABLED');
break;
case browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER:
console.log(
'file: URL detected, please use a web server to host Native ' +
'Client applications.');
console.log('file:// URLs NOT ALLOWED');
default:
console.log('Unknown error: Unable to detect browser and/or ' +
'Native Client support.');
console.log('UNKNOWN ERROR');
break;
}
return isValidBrowser && browserSupportStatus == browser_version.BrowserChecker.StatusValues.NACL_ENABLED;
}
checkBrowser();
// Create a namespace object
var browser_version = browser_version || {};
/**
* Class to provide checking for version and NativeClient.
* @param {integer} arg1 An argument that indicates major version of Chrome we
* require, such as 14.
*/
/**
* Constructor for the BrowserChecker. Sets the major version of
* Chrome that is required to |minChromeVersion|.
* @param minChromeVersion The earliest major version of chrome that
* is supported. If the Chrome browser version is less than
* |minChromeVersion| then |isValidBrowswer| will be set to false.
* @param opt_maxChromeVersion Ignored. Retained for backwards compatibility.
* @param appVersion The application version string.
* @param plugins The plugins that exist in the browser.
* @constructor
*/
browser_version.BrowserChecker = function (minChromeVersion,
appVersion, plugins,
opt_maxChromeVersion) {
/**
* Version specified by the user. This class looks to see if the browser
* version is >= |minChromeVersion_|.
* @type {integer}
* @private
*/
this.minChromeVersion_ = minChromeVersion;
/**
* List of Browser plugin objects.
* @type {Ojbect array}
* @private
*/
this.plugins_ = plugins;
/**
* Application version string from the Browser.
* @type {integer}
* @private
*/
this.appVersion_ = appVersion;
/**
* Flag used to indicate if the browser has Native Client and is if the
* browser version is recent enough.
* @type {boolean}
* @private
*/
this.isValidBrowser_ = false;
/**
* Actual major version of Chrome -- found by querying the browser.
* @type {integer}
* @private
*/
this.chromeVersion_ = null;
/**
* Browser support status. This allows the user to get a detailed status
* rather than using this.browserSupportMessage.
*/
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.UNKNOWN;
}
/**
* The values used for BrowserChecker status to indicate success or
* a specific error.
* @enum {id}
*/
browser_version.BrowserChecker.StatusValues = {
UNKNOWN: 0,
NACL_ENABLED: 1,
UNKNOWN_BROWSER: 2,
CHROME_VERSION_TOO_OLD: 3,
NACL_NOT_ENABLED: 4,
NOT_USING_SERVER: 5
};
/**
* Determines if the plugin with name |name| exists in the browser.
* @param {string} name The name of the plugin.
* @param {Object array} plugins The plugins in this browser.
* @return {bool} |true| if the plugin is found.
*/
browser_version.BrowserChecker.prototype.pluginExists = function (name,
plugins) {
for (var index = 0; index < plugins.length; index++) {
var plugin = this.plugins_[index];
var plugin_name = plugin['name'];
// If the plugin is not found, you can use the Javascript console
// to see the names of the plugins that were found when debugging.
if (plugin_name.indexOf(name) != -1) {
return true;
}
}
return false;
}
/**
* Returns browserSupportStatus_ which indicates if the browser supports
* Native Client. Values are defined as literals in
* browser_version.BrowserChecker.StatusValues.
* @ return {int} Level of NaCl support.
*/
browser_version.BrowserChecker.prototype.getBrowserSupportStatus = function () {
return this.browserSupportStatus_;
}
/**
* Returns isValidBrowser (true/false) to indicate if the browser supports
* Native Client.
* @ return {bool} If this browser has NativeClient and correct version.
*/
browser_version.BrowserChecker.prototype.getIsValidBrowser = function () {
return this.isValidBrowser_;
}
/**
* Checks to see if this browser can support Native Client applications.
* For Chrome browsers, checks to see if the "Native Client" plugin is
* enabled.
*/
browser_version.BrowserChecker.prototype.checkBrowser = function () {
var versionPatt = /Chrome\/(\d+)\.(\d+)\.(\d+)\.(\d+)/;
var result = this.appVersion_.match(versionPatt);
// |result| stores the Chrome version number.
if (!result) {
this.isValidBrowser_ = false;
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.UNKNOWN_BROWSER;
} else {
this.chromeVersion_ = result[1];
// We know we have Chrome, check version and/or plugin named Native Client
if (this.chromeVersion_ >= this.minChromeVersion_) {
var found_nacl = this.pluginExists('Native Client', this.plugins_);
if (found_nacl) {
this.isValidBrowser_ = true;
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.NACL_ENABLED;
console.log('Browser is supported by Native Client');
} else {
this.isValidBrowser_ = false;
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.NACL_NOT_ENABLED;
console.log('Browser is not supported by Native Client');
}
} else {
// We are in a version that is less than |minChromeVersion_|
this.isValidBrowser_ = false;
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.CHROME_VERSION_TOO_OLD;
}
}
var my_protocol = window.location.protocol;
if (my_protocol.indexOf('file') == 0) {
this.isValidBrowser_ = false;
this.browserSupportStatus_ =
browser_version.BrowserChecker.StatusValues.NOT_USING_SERVER;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment