Tuesday, 15 January 2019

How to override JavaScript alert function inside an iframe before it finished loading?

I am using headless chrome browser with wraith(https://github.com/BBC-News/wraith). The problem is when I have a page that opens an alert wraith fails

ERROR: unexpected alert open: {Alert text : Bla bla bla.}
  (Session info: headless chrome=71.0.3578.98)
  (Driver info: chromedriver=71.0.3578.33 (269aa0e3f0db08097f0fe231c7e6be200b6939f7),platform=Mac OS X 10.13.6 x86_64)

This is what I tried, but has failed so far:

$("#ifr").attr("src", original_url_asked);//cannot redirect or wraith complains

var iframe = document.getElementById('ifr');
iframe.contentWindow.alert = function(){ console.log('no');};

$('#ifr').on('readystatechange',function (){
    Window.prototype.alert = function(){};
    window.alert = function ( text ) { console.log( 'tried to alert: ' + text ); return true; };
});

$('#ifr').on('load', function() {
    window.alert = function ( text ) { console.log( 'tried to alert: ' + text ); return true; };
    $('#ifr').contents().find('div.backend_left, div.backend_right_top').remove();
    resizeIFrameToFitContent(document.getElementById('ifr'));
});



from How to override JavaScript alert function inside an iframe before it finished loading?

No comments:

Post a Comment