Question: I got some JS Code that gets inside a random Anonymous js function.
I want that code (for example `alert('hello')` ) to dump/alert 
the entire script block/object which it was injected into. 

kinda like document.body.innerHTML but for the anonymous function block 

result should be like :

```
Function()({ somecode; MyAlert(...) } )()

```

or

```
Try { some code; mycode; } catch(e) { }

```

Answer: 
1. Mind your terms. **"(browser) script block" literally means script element's code** [by the spec](http://www.w3.org/TR/html5/scripting-1.html#the-script-block%27s-type).
Use "javascript block" ***or*** "javascript object" to mean a [block](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/block) ***or*** an [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects).
Do not create confusing new terms; do read and research.
2. Blocks are not objects; they are language statements.
Just like you cannot "get the code/variables of current line", **you cannot "get the code/variables of current block"**, try block or not.
3. Stepping back, for now you can **use [`Function.caller`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller) to get the function calling your code**:

```js
var mycode = function me(){ if ( me.caller ) alert( me.caller.toString() ); };
(function(){ var some = 'code'; mycode(); })();
// Alert "function(){ var some = 'code'; mycode(); }", even when it is anonymous
```

> 
> Note that you get the whole function's code, not the function block's code which excludes parameters and function name.
> 
> 
> 

4. **Function.caller may be removed in future**, like [`arguments.caller`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/caller). (Both are troubles. What if a cross origin function on the call stack contains private api key code? How should js engines [inline](http://ariya.ofilabs.com/2013/04/automatic-inlining-in-javascript-engines.html) your code?)

When the time comes, or **when caller is null** (when it is global code), you may still be able to get **textual stacktrace** ([`new Error().stack`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error/stack)) and **current script element** ([`document.currentScript`](https://developer.mozilla.org/en-US/docs/Web/API/Document/currentScript)), but their capabilities are pretty limited.

You can get a script element's code - if any - with its textContent or innerHTML property.
5. Your question sounds like an [XY Problem](https://meta.stackexchange.com/a/66378). You want to do something that no modern language is meant to do, but never say for what purpose.
**Try to describe your real problem.**
If you're **not** in strict mode you can go up the stack from something which was referenceable (i.e. a named function expression) using (non-standard) [`.caller`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/caller)

```
function getFunctionReference(callback) {
    var ref = getFunctionReference.caller;
    if (callback) callback(ref);
    return ref;
}

```

Now you can do things like

```
(function () {
    getFunctionReference(alert);
}());
// alerts the .toString of the IIFE

```

This only works for *functions*, you can't do this on top level code.

---

The best way to explore your code is actually with the *Console*, and you can use the [`debugger;`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) statement or breakpoints to follow exactly what is happening and when.