Search currentScript in HTML

Here is a sample code:

HTML

<script> alert('This is alert!') </script> 

Js

 window.alert = function(data) //alert() over-riding { scriptObject = document.currentScript; //gives me <script> object } 

Update: The above code now does not work (it previously worked, compatibility has been removed for IE .) In Internet Explorer 11.420.10586.0. Why can it find a Script object in Chrome, Firefox, Safari, and Microsoft Edge, but not in Internet Explorer? Is there an alternative way?


Question:

HTML

 <script> ReferenceError.prototype.__defineGetter__('name', function fff() { javascript:alert(1) }),x </script> 

Js

 window.alert = function(data) //alert() over-riding { scriptObject = ? // I need to get the Script object } 

I tried arguments.callee.caller find fff() but couldn't catch the Script object.

Alert () is not executed in Chrome for the above script. Use Firefox instead. I could not get the Script object in any browser.

Any solution please?

+6
source share
1 answer

In the simplest case, when your overridden alert is called immediately (blocking) a script, simple document.scripts[document.scripts.length-1] might be good:

 <pre id="log"></pre> <script> window.alert = function(a){ log.innerText += a + ' ' + document.scripts[document.scripts.length-1].outerHTML + '\n'; } </script> <script id="a">alert('first')</script> <script id="b">alert('second')</script> <script id="c">alert('third')</script> 
+1
source

All Articles