Detecting Pepper (PPAPI) Flash with Javascript

旧时模样 提交于 2019-12-03 08:33:01

I think it should be done this way:

var isPPAPI = false;
var type = 'application/x-shockwave-flash';
var mimeTypes = navigator.mimeTypes;

if (mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin &&
    mimeTypes[type].enabledPlugin.filename == 'pepflashplayer.dll') isPPAPI = true;

Demo on jsFiddle.


UPD №1: Not sure if needed, but I wrote a little explanation:

If our browser has a MIME types enumeration, we can get a plugin associated with a specified type. So we're getting plugin which associated with 'application/x-shockwave-flash' and check if its filename is 'pepflashplayer.dll'. I think that this name is constant and will not be changed in future.


UPD №2:

To enable/disable PPAPI in Google Chrome, you should go to this page: chrome://plugins/

(Sorry, this URL needs to be pasted directly into the address bar.)


UPD №3:

I did some investigation and found an interesting article that helped me to implement a cross-platform solution. I think that this code should work on all OS:

var isPPAPI = false;
var type = 'application/x-shockwave-flash';
var mimeTypes = navigator.mimeTypes;

var endsWith = function(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

if (mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin &&
   (mimeTypes[type].enabledPlugin.filename == "pepflashplayer.dll" ||
    mimeTypes[type].enabledPlugin.filename == "libpepflashplayer.so" ||
    endsWith(mimeTypes[type].enabledPlugin.filename, "Chrome.plugin"))) isPPAPI = true;

Check out an updated fiddle.


UPD №4:

Slightly changed the code to meet today's realities. Now condition looks like this:

if (mimeTypes && mimeTypes[type] && mimeTypes[type].enabledPlugin &&
   (mimeTypes[type].enabledPlugin.filename.match(/pepflashplayer|Pepper/gi))) isPPAPI = true;

Check out jsFiddle.

I couldn't get the other examples to work, but the following code works for me on both Mac and PC with Chrome PPAPI enabled or disabled. Also works correctly on other browsers.

function checkForPepper() {
  return navigator.plugins && _.any(navigator.plugins, function(plugin) {
    return plugin.filename === 'pepflashplayer.dll' || plugin.filename === 'PepperFlashPlayer.plugin';
  });
}
alert ('Pepper enabled: '+ checkForPepper());

Note: Requires underscore.js. Fiddle here.

A version of Darren's check that doesn't require Underscore.js

var checkForPepper = function() {
if (navigator.plugins) {
    for (var i=0, count = navigator.plugins.length; i < count; i++) {
        var filename = navigator.plugins[i].filename;
        if (filename === 'pepflashplayer.dll' || filename === 'PepperFlashPlayer.plugin') return true;
    }
}
return false;

}

I made a cleaner version of this method using regexp. Includes tests.

http://jsfiddle.net/YNCVh/

>>> Currently Running Pepper? true

>>> Test Case Plugin Matched? pepflashplayer.dll : true

>>> Test Case Plugin Matched? PepperFlashPlayer.plugin : true

>>> Test Case Plugin Matched? libpepflashplayer.so : true

/**
 * Regular expression to test for pepper PPAPI plugins
 */
var PPAPI_REGEX = /^(lib)?pep(per)?flashplayer/i;

/**
 * Returns true if the current agent is running PPAPI version of flash
 */
function runningPepperFlash() {
  if (navigator.plugins) {
    for (var i = 0, count = navigator.plugins.length; i < count; i++) {
      var plugin = navigator.plugins[i].filename;
      var has_pepper = PPAPI_REGEX.test(plugin);
      if (has_pepper) {
        return true;
      }
    }
    return false;
  }
}

// ----------------------------------------------------------
// test cases:

/**
 * Test case against the three (3) known plugin file types (win,mac,linux)
 */
function executeTestCases() {
  var plugins = ['pepflashplayer.dll', 'PepperFlashPlayer.plugin', 'libpepflashplayer.so'];
  for (var i = 0; i < plugins.length; i++) {
    var has_pepper = PPAPI_REGEX.test(plugins[i]);
    console.log('Test Case Plugin Matched? ', plugins[i], ': ', has_pepper);
  }

}

console.log('Currently Running Pepper?', runningPepperFlash());

executeTestCases();​

I know you asked for Javascript, but this can also be done (more easily) in Flash. Just check

if (Capabilities.manufacturer === "Google Pepper") {
    Alert.show("Using PPAPI");
}

Source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!