Browse Source

handy javascript functions

David Rose 16 years ago
parent
commit
5a2e1d1b50
2 changed files with 191 additions and 0 deletions
  1. 99 0
      direct/src/directscripts/DetectPanda3D.js
  2. 92 0
      direct/src/directscripts/RunPanda3D.js

+ 99 - 0
direct/src/directscripts/DetectPanda3D.js

@@ -0,0 +1,99 @@
+// Based on Apple sample code at
+// http://developer.apple.com/internet/webcontent/examples/detectplugins_source.html
+
+
+// initialize global variables
+var detectableWithVB = false;
+var pluginFound = false;
+
+function goURL(daURL) {
+    // Assume we have Javascript 1.1 functionality.
+    window.location.replace(daURL);
+    return;
+}
+
+function redirectCheck(pluginFound, redirectURL, redirectIfFound) {
+    // check for redirection
+    if( redirectURL && ((pluginFound && redirectIfFound) || 
+	(!pluginFound && !redirectIfFound)) ) {
+	// go away
+	goURL(redirectURL);
+	return pluginFound;
+    } else {
+	// stay here and return result of plugin detection
+	return pluginFound;
+    }	
+}
+
+function canDetectPlugins() {
+    if( detectableWithVB || (navigator.plugins && navigator.plugins.length > 0) ) {
+	return true;
+    } else {
+	return false;
+    }
+}
+
+function detectPanda3D(redirectURL, redirectIfFound) {
+    pluginFound = detectPlugin('Panda3D'); 
+    // if not found, try to detect with VisualBasic
+    if(!pluginFound && detectableWithVB) {
+	pluginFound = detectActiveXControl('P3DACTIVEX.P3DActiveXCtrl.1');
+    }
+    // check for redirection
+    return redirectCheck(pluginFound, redirectURL, redirectIfFound);
+}
+
+function detectPlugin() {
+    // allow for multiple checks in a single pass
+    var daPlugins = detectPlugin.arguments;
+    // consider pluginFound to be false until proven true
+    var pluginFound = false;
+    // if plugins array is there and not fake
+    if (navigator.plugins && navigator.plugins.length > 0) {
+	var pluginsArrayLength = navigator.plugins.length;
+	// for each plugin...
+	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
+	    // loop through all desired names and check each against the current plugin name
+	    var numFound = 0;
+	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
+		// if desired plugin name is found in either plugin name or description
+		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
+		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
+		    // this name was found
+		    numFound++;
+		}   
+	    }
+	    // now that we have checked all the required names against this one plugin,
+	    // if the number we found matches the total number provided then we were successful
+	    if(numFound == daPlugins.length) {
+		pluginFound = true;
+		// if we've found the plugin, we can stop looking through at the rest of the plugins
+		break;
+	    }
+	}
+    }
+    return pluginFound;
+} // detectPlugin
+
+
+// Here we write out the VBScript block for MSIE Windows
+if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
+    document.writeln('<script language="VBscript">');
+
+    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
+    document.writeln('detectableWithVB = False');
+    document.writeln('If ScriptEngineMajorVersion >= 2 then');
+    document.writeln('  detectableWithVB = True');
+    document.writeln('End If');
+
+    document.writeln('\'this next function will detect most plugins');
+    document.writeln('Function detectActiveXControl(activeXControlName)');
+    document.writeln('  on error resume next');
+    document.writeln('  detectActiveXControl = False');
+    document.writeln('  If detectableWithVB Then');
+    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
+    document.writeln('  End If');
+    document.writeln('End Function');
+
+    document.writeln('</script>');
+}

+ 92 - 0
direct/src/directscripts/RunPanda3D.js

@@ -0,0 +1,92 @@
+// This script injects the appropriate syntax into the document to
+// embed Panda3D, either for the ActiveX or Mozilla-based plugin.
+
+// It is also possible to write browser-independent code by nesting
+// <object> tags, but this causes problems when you need to reference
+// the particular object that is actually running (which object is
+// it?) for scripting purposes.
+
+// This script writes only a single <object> tag, and it can be
+// assigned the id you specify, avoiding this ambiguity.
+
+var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
+var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
+var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
+
+
+function P3D_Generateobj(objAttrs, params, embedAttrs) 
+{ 
+  var str = '';
+  if (isIE && isWin && !isOpera)
+  {
+    str += '<object ';
+    for (var i in objAttrs)
+    {
+      str += i + '="' + objAttrs[i] + '" ';
+    }
+    str += '>';
+    for (var i in params)
+    {
+      str += '<param name="' + i + '" value="' + params[i] + '" /> ';
+    }
+    str += '</object>';
+  }
+  else
+  {
+    str += '<embed ';
+    for (var i in embedAttrs)
+    {
+      str += i + '="' + embedAttrs[i] + '" ';
+    }
+    str += '> </embed>';
+  }
+
+  document.write(str);
+}
+
+function P3D_RunContent(){
+  var ret = 
+    P3D_GetArgs
+      (arguments, "clsid:924b4927-d3ba-41ea-9f7e-8a89194ab3ac",
+       "application/x-panda3d");
+  P3D_Generateobj(ret.objAttrs, ret.params, ret.embedAttrs);
+}
+
+function P3D_GetArgs(args, classid, mimeType){
+  var ret = new Object();
+  ret.embedAttrs = new Object();
+  ret.params = new Object();
+  ret.objAttrs = new Object();
+
+  for (var i = 0; i < args.length; i = i + 2){
+    var currArg = args[i].toLowerCase();    
+
+    switch (currArg){	
+    case "src":
+    case "data":
+        ret.embedAttrs['src'] = args[i+1];
+        ret.params['data'] = args[i+1];
+        break;
+
+    case "id":
+    case "width":
+    case "height":
+    case "align":
+    case "vspace": 
+    case "hspace":
+    case "class":
+    case "title":
+    case "accesskey":
+    case "name":
+    case "tabindex":
+        ret.embedAttrs[args[i]] = ret.objAttrs[args[i]] = args[i+1];
+        break;
+
+    default:
+        ret.embedAttrs[args[i]] = ret.params[args[i]] = args[i+1];
+    }
+  }
+  ret.objAttrs["classid"] = classid;
+  if (mimeType) ret.embedAttrs["type"] = mimeType;
+  return ret;
+}