0

Im looking to inject a variable into a page before a method is run. The situation is as follow

CUSTOM JS CAN GO HERE

my.var = 'cake';
my.function();

I dont have access to be able to modify the page directly, however there is a content area at the top of the page I can add some JS if i need to. Basically i need to overwrite the my.var variable with something else before my.function() is run. Is there a way to do this? (the site is running jQuery 1.4.2.

Thanks Dan

  • Im hoping i can can detect when my.function() is about to run, and run an anonymous function to set the variable to my liking before it goes. Also, dont have access to push any files to the site. All I have is a window in the CMS of the site which allows me to pop some custom JS in that area above. – Daniel K. Jones Jun 29 '12 at 05:17
  • If those are setup above the `CUSTOM` injection point, then just re-declare them. – Jared Farrish Jun 29 '12 at 05:17
  • Unfortunately, the one i want to change is below the `CUSTOM` area. – Daniel K. Jones Jun 29 '12 at 05:19

1 Answers1

3

Basically i need to overwrite the my.var variable with something else before my.function() is run. Is there a way to do this?

var oldfunction = my.function;
my.function = function () {
    my.var = "whatever you want";
    oldfunction.apply(this, arguments);
}

This is, of course, if my or my.function isn't overwritten by the code you can't modify directly before the call to my.function (as my.var is).

For example, in the following scenario:

///your code goes here

///code you cannot modify below
var my = {};
my.function = something;
my.var = 'cake';
my.function();

what you want is impossible (unless you're able to redefine something or something in it in the same way).

Basically, in this case the only thing you could do is to write (let's assume something is function () { alert(my.var) })

var oldalert = window.alert;
window.alert = function (message) {
    oldalert(message === 'cake' ? 'whatever you want' : message);
}

Well, you've got an idea.

Injecting some code in between my.var = 'cake'; and my.function(); is, from the other side, imposible. Roughly speaking, you can choose between two options, whether your code will be executed before my.var = 'cake'; or after my.function();. Executing your code aftre my.var = 'cake'; but before my.function(); is impossible (if we're speaking of a production environment; of course you could do anything by hands using the debugger, if you need to modify my.var for a debugging purpose).

penartur
  • 9,792
  • 5
  • 39
  • 50
  • That's what I was thinking, but I'm not sure if that function is declared above or below the custom script area. – Jared Farrish Jun 29 '12 at 05:23
  • I modified my answer correspondingly. Basically, the only option you have is to redefine something. It is **impossible** to break up the execution sequence. In JS, one could be completely sure that **nothing** occurs in between `my.var = 'cake';` and `my.function();`. – penartur Jun 29 '12 at 05:26
  • It's not [*impossible*](http://jsfiddle.net/userdude/ZZYQR/). Seems kinda wrong though. – Jared Farrish Jun 29 '12 at 05:28
  • Yeah, your solution looks pretty damn good penartur. Ive just found out however that the `CUSTOM` area is infact below where the my.function() is called, so it looks like im stuffed... Thanks for your help though. If the scenario was as above, that would most probably have been the best solution. Unfortunately the JS is run syncronously (not on doc load) as well – Daniel K. Jones Jun 29 '12 at 05:30
  • @JaredFarrish I don't understand what your example is supposed to demonstrate. The OP said that there is block of `a; b;` on the page and that they has no control on this block content. As such, it is impossible to inject code to be run after `a;` call but before `b;` call (leaving aside the possibility to change `a` or `b` and the debugger). – penartur Jun 29 '12 at 05:33
  • You said you can't stop execution, but you can. It's not [pretty](http://jsfiddle.net/userdude/ZZYQR/1/), but if you can recreate the script that runs underneath it, you could in theory block the other code from running. – Jared Farrish Jun 29 '12 at 05:38
  • I still don't see from your examples how anyone could stop the execution of the code they can't modify. Could you please write your examples a bit more detailed? Like, "here is the original code, and here is what I prepend/append to the original code to stop the execution of the original code in-between". – penartur Jun 29 '12 at 05:41
  • Once jsFiddle stops churning and loads, I'll show you what I mean. If you can inject a piece of script into the middle of two other scripts in the global scope, you can stop execution with `throw 'whatever';`. Before that, if you have the opportunity, you can essentially either run the following scripts that are being blocked by copying and pasting it into that custom code section, or recreate what they would do. with some other script. I never said this was a good idea or that I supported it (hence, I added no answer on this), but it is possible to "redirect and stop execution". With caveats. – Jared Farrish Jun 29 '12 at 06:20
  • @JaredFarrish "If you can inject a piece of script into the middle of two other scripts in the global scope" - the question was whether you can inject into the middle of execution **without** injecting into the middle of source code. – penartur Jun 29 '12 at 06:22
  • And... What *I* am saying is that you could stop the execution and rerun the remaining script, for instance on `window.onload`. I know what the OP was suggesting (see my comment which mirrors what you demonstrated above). You could *fake it* though under certain circumstances and achieve the *same effect*. – Jared Farrish Jun 29 '12 at 06:25
  • @JaredFarrish And what I said in the first place (and what you're started arguing to) is that you **cannot** stop the execution in the middle **unless** you can modify the source code which executes. That is, given a code `alert(1); alert(2);` there is no way to do that the first line executes and the second won't, unless you either 0) can modify this code, or 1) redefine the `window.alert`. You, for some reason, arguing that I'm wrong, by showing off the examples of how one can modify the code in the way that the second line won't be executed (why won't they just remove the second line then?) – penartur Jun 29 '12 at 06:30
  • Ok, take a look at this: http://jsfiddle.net/userdude/ZZYQR/2/ Maybe we're not on the same page about what specifically is meant here, but note in that fiddle `after()` isn't redefined. Yet it still runs with the new variable value set. – Jared Farrish Jun 29 '12 at 06:37
  • Note, forgot to call `after()` in the original: http://jsfiddle.net/userdude/ZZYQR/3/ Same effect, it's not run but still added and able to run. And interestingly enough, the `foo` also never gets lifted, even if I comment out the resetting of the `foo` in the custom block: http://jsfiddle.net/userdude/ZZYQR/3/ – Jared Farrish Jun 29 '12 at 06:42
  • Basically, you're not **injecting** into the others' code. You're preventing others' code to run, while implementing something similar to other's code by yourself. If I'll assemble the car like yours but with the wall surrounding the driver's seat and then burn your car and place my car into your garage instead of the burned one, this would definitely be far from "*I injected the wall into your car*". – penartur Jun 29 '12 at 06:58
  • Not quite, but this discussion is going nowhere. Semantically, you understand what `injection` means differently that I, where I think you're describing an `intercept` *with* `injection`. Yes, you can't monitor a function or variable on the stack and `intercept` it's execution and modify/return execution without short-circuiting and replaying *on the same code, out-of-place*. I see the distinction you mean, but I think there is another way in certain cases that's "non-optimal" but possible to use if you need to, as this OP could have and remapped the execution in a way under his control. – Jared Farrish Jun 29 '12 at 07:12
  • OK. What I was trying to say is that due to single-threaded JS nature (and its event loop properties) it is impossible to inject/intercept **as opposed to** some different platforms. It seems that we just didn't understood each other at the first moment. – penartur Jun 29 '12 at 07:34