
function analyzeObject(element) {
  if (element === null) {
    console.error("Null object to analyze");
    return {
      type: "notflash"
    };
  }
  var param = element.firstChild;
  if (param === null) {
    console.error("No child to analyze in", element.localName);
    return {
      type: "notflash"
    };
  }
  var width, height;
  width = element.getAttribute("width");
  height = element.getAttribute("height");

  while (param) {
    if (param.localName == 'embed') {

      var embed_type = param.getAttribute("type")
      if (embed_type != "application/x-shockwave-flash") {
        console.log("Not flash, but", embed_type);
        return {
          type: "notflash"
        };
      }

      width = param.hasAttribute("width") ? param.getAttribute("width") : width;
      height = param.hasAttribute("height") ? param.getAttribute("height") : height;
//      allowfullscreen = param.getAttribute("allowfullscreen");

      if (param.hasAttribute('src')) {
        var url = param.getAttribute('src');

        console.log("matching url", url);

        // YOUTUBE
        var matches = url.match('^https?:\/\/(?:www\.)?youtube\.(?:googleapis\.)?com/v/([A-Za-z0-9_\-]{11})');
        if (matches) {
          return {
            type: "youtube",
            src: url,
            width: width,
            height: height,
            videoid: matches[1],
            processor: replaceYT
          };
        }

        // VIMEO
        matches = url.match('^https?:\/\/(www\.)?vimeo.com/moogaloop\.swf\?.*clip_id=([0-9]*)');
        if (matches) {
          return {
            type: "vimeo",
            src: url,
            width: width,
            height: height,
            videoid: matches[2],
            processor: replaceVimeo
          };
        }
      }
    }
    param = param.nextSibling;
  }
}

// replace the vimeo embed by its iframe
function replaceVimeo(container, a) {
  if (a.type != "vimeo") {
    console.error("Wrong type, got", a.type);
    return;
  }

  if (a.videoid === undefined) {
    console.error("Couldn't match VIMEO video ID in URL", a.src);
    return ;
  }

  var replacement = document.createElement("iframe");
  replacement.setAttribute('src', 'https://player.vimeo.com/video/' + a.videoid);
  replacement.setAttribute('width', a.width);
  replacement.setAttribute('height', a.height);
  replacement.setAttribute('frameborder', '0');
//  replacement.setAttribute('webkitallowfullscreen'); // not needed
//  replacement.setAttribute('mozallowfullscreen'); // deprecated
  replacement.setAttribute('allowfullscreen', '');

  replaceObjectTag(container, replacement);
}


// replace the youtube embed by its iframe
function replaceYT(container, a) {
  if (a.type != "youtube") {
    console.error("Wrong type, got", a.type);
    return;
  }

  if (a.videoid === undefined) {
    console.error("Couldn't match YT video ID in URL", a.src);
    return ;
  }

  var replacement = document.createElement("iframe");
  replacement.setAttribute('class', 'youtube-player');
  replacement.setAttribute('type', 'text/html');
  replacement.setAttribute('width', a.width);
  replacement.setAttribute('height', a.height);
  replacement.setAttribute('src', 'https://www.youtube.com/embed/' + a.videoid);
  replacement.setAttribute('allowfullscreen', '');
  replacement.setAttribute('frameborder', '0');

  replaceObjectTag(container, replacement);
}

function replaceObjectTag(obj, replacement) {
  obj.parentNode.insertBefore(replacement, obj);
  obj.parentNode.removeChild(obj);
}

var elements = document.getElementsByTagName("object")

var embeds = [];
for (var i = 0; i < elements.length; i++) {
  embeds.push(elements.item(i));
}

for (var i in embeds) {
  try {
    console.log('processing', i);
    var analyzed = analyzeObject(embeds[i]);

    if (typeof analyzed.processor === 'function') {
      analyzed.processor(embeds[i], analyzed);
    } else {
      console.log("Found unprocessable object", embeds[i].localName, "of type", analyzed.type);
    }
  }
  catch (e) {
    console.error("Exception:", e.message, e.stack);
  }
}


@@ -1,156 +1,108 @@
-%0Afunction analyzeObject(element) %7B%0A  if (element === null) %7B%0A    console.error(%22Null object to analyze%22);%0A    return %7B%0A      type: %22notflash%22%0A    %7D;%0A  %7D
+//%0A// This is injected in the content.%0A// Author: Hubert Figuiere%0A//%0A%0A%0Afunction analyzeObject(element) %7B
 %0A  v
@@ -111,16 +111,26 @@
  param =
+ element ?
  element
@@ -140,16 +140,23 @@
 rstChild
+ : null
 ;%0A  if (
@@ -201,16 +201,27 @@
 o child 
+or element 
 to analy
@@ -227,16 +227,44 @@
 yze in%22,
+%0A                  element ?
  element
@@ -273,16 +273,27 @@
 ocalName
+ : '(null)'
 );%0A    r
@@ -1795,24 +1795,25 @@
 ;%0A        %7D%0A
+%0A
       %7D%0A    

Make the video replacer more robust.