Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding currentScript in HTML

Here's 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 doesn't seem to work now (It worked earlier, compatibility removed for IE) in Internet Explorer 11.420.10586.0. Why it is able to find the Script object in Chrome, Firefox, Safari and Microsoft Edge, but not in Internet Explorer? Is there any alternate way?


Issue:

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 to find fff(), but unable to catch the script object.

Alert() doesn't execute in Chrome for the above script. Use Firefox, instead. I couldn't get the script object in any browser.

Any solution please?

like image 597
verstappen_doodle Avatar asked Feb 25 '16 13:02

verstappen_doodle


People also ask

How do I remove a script in HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

How may I reference the script tag that loaded the currently executing script?

Use document. document. currentScript will return the <script> element whose script is currently being processed.

Where is script located in HTML?

You can place any number of scripts in an HTML document. Scripts can be placed in the <body> , or in the <head> section of an HTML page, or in both.

Which object is used to access information about the browser that is executing current script?

The window. navigator object contains information about the visitor's browser.


1 Answers

In the simplest scenario when your overridden alert is invoked immediately in (blocking) script, simple document.scripts[document.scripts.length-1] could be good to go:

<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>
like image 92
myf Avatar answered Oct 11 '22 00:10

myf