Detecting NPAPI support in Chrome using javascript

前端 未结 3 1561
悲哀的现实
悲哀的现实 2020-12-11 00:12

As Google Chrome is dropping support for NPAPI post September 2015. Is there any way to detect the NPAPI support in chrome using JavaScript so that Alternative content will

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 00:46

    Had an issue created by chrome 42's disabling of NPAPI. What was done was something in the lines of this: (similar to )

    function isJavaAvailable() {
      var javaRegex = /(Java)(\(TM\)| Deployment)/,
          plugins = navigator.plugins;
      if (navigator && plugins) {
        for (plugin in plugins){
          if(plugins.hasOwnProperty(plugin) &&
             javaRegex.exec(plugins[plugin].name)) {
            return true;
          }
        }
      }
      return false;
    }
    
    var chromeVersion = window.navigator.userAgent.match(/Chrome\/(\d+)\./);
    if (chromeVersion && chromeVersion[1]) {
      if (parseInt(chromeVersion[1], 10) >= 42 && !isJavaAvailable()) {
        // do chrome-no-java-related task
        console.log('Java not available');
      }
    }

    This is not a direct "NPAPI-detector", but can be rewritten to test for the plugins affected by NPAPI disabling through changing the regex for instance. Regex was used for some kind of robustness. Had a look into navigator.plugins['some-number'].names to find what to check for.

提交回复
热议问题