Created
September 6, 2012 02:32
-
-
Save hapticdata/3650134 to your computer and use it in GitHub Desktop.
bubble console methods from nested iframes into root window.console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//##console.log bubble for nested iframes | |
//##by [Kyle Phillips](http://haptic-data.com) | |
//find each iframe recursively and bubble its logs up to the current window.console | |
(function( d, w ){ | |
function bubble( doc, win, msg ){ | |
Array.prototype.forEach.call(doc.getElementsByTagName('iframe'), function( frame ){ | |
if( !sameOrigin(frame.src) || !frame.contentWindow || !frame.contentWindow.console ){ | |
w.console.log('console of iframe ', frame.src, ' is unreachable'); | |
return; | |
} | |
msg = [msg,' iframe#', frame.id].join(''); | |
[ "log", "error", "warn", "info" ].forEach(function( method ){ | |
frame.contentWindow.console[method] = function(){ | |
w.console[method].apply(w.console, [msg, '-> '].concat(Array.prototype.slice.call(arguments,0)) ); | |
}; | |
}); | |
bubble( frame.contentDocument, frame.contentWindow, msg); | |
}); | |
} | |
function sameOrigin(url) { | |
var host, protocol, srOrigin, origin; | |
// url could be relative or scheme relative or absolute | |
host = d.location.host; // host + port | |
protocol = d.location.protocol; | |
srOrigin = '//' + host; | |
origin = protocol + srOrigin; | |
// Allow absolute or scheme relative URLs to same origin | |
return (url == origin || url.slice(0, origin.length + 1) == origin + '/') || | |
(url == srOrigin || url.slice(0, srOrigin.length + 1) == srOrigin + '/') || | |
// or any other URL that isn't scheme relative or absolute i.e relative. | |
!(/^(\/\/|http:|https:).*/.test(url)); | |
} | |
bubble( d, w, "" ); | |
}( document, window )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment