Skip to content

Instantly share code, notes, and snippets.

@legege
Created April 3, 2013 14:02
Show Gist options
  • Save legege/5301477 to your computer and use it in GitHub Desktop.
Save legege/5301477 to your computer and use it in GitHub Desktop.
This is an AngularJS directive for MJPEG streams. This directive uses an iframe to stop the browser from loading the MJPEG stream. This is useful for Chrome: https://code.google.com/p/chromium/issues/detail?id=73395
<mjpeg url="videoUrl"></mjpeg>
angular.module('myApp.directives', [])
.directive('mjpeg', function() {
return {
restrict: 'E',
replace: true,
template:'<span></span>',
scope: {
'url': '='
},
link: function (scope, element, attrs) {
scope.$watch('url', function (newVal, oldVal) {
if (newVal) {
var iframe = document.createElement('iframe');
iframe.setAttribute('width', '100%');
iframe.setAttribute('frameborder', '0');
iframe.setAttribute('scrolling', 'no');
element.replaceWith(iframe);
var iframeHtml = '<html><head><base target="_parent" /><style type="text/css">html, body { margin: 0; padding: 0; height: 100%; width: 100%; }</style><script> function resizeParent() { var ifs = window.top.document.getElementsByTagName("iframe"); for (var i = 0, len = ifs.length; i < len; i++) { var f = ifs[i]; var fDoc = f.contentDocument || f.contentWindow.document; if (fDoc === document) { f.height = 0; f.height = document.body.scrollHeight; } } }</script></head><body onresize="resizeParent()"><img src="' + newVal + '" style="width: 100%; height: auto" onload="resizeParent()" /></body></html>';
var doc = iframe.document;
if (iframe.contentDocument) { doc = iframe.contentDocument; }
else if (iframe.contentWindow) { doc = iframe.contentWindow.document; }
doc.open();
doc.writeln(iframeHtml);
doc.close();
} else {
element.html('<span></span>');
}
}, true);
}
};
})
@babelouest
Copy link

Thanks for this mjpeg directive, this is what I'm looking for.

Although there is no license mentioned, can you tell me which one it is so I know if and where I can use your directive ?

@daveselinger
Copy link

You are a star. I've been working on this bug for like a week straight. Adding the word "IONIC" to the comment thread so that people searching for ionic see this too. WOO HOO YOU RULE. :)

@ugnelis
Copy link

ugnelis commented Sep 7, 2018

Nice code! There is one bug. When I use this this code, I cannot scroll in parent windows.

Change if (fDoc === document) { f.height = 0; f.height = document.body.scrollHeight; }
with
if (fDoc === document) { f.height = document.body.scrollHeight; }

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment