8

I have a script that loads the code dynamically. It is kind of a search engine. When I press a search button, the action gets triggered and a new page opens with many parameters.

I want to override one of the parameters generated with the script in the new URL. JS code is quite big and hard to read, but I have found the important part in the Firebug DOM editor.

This is the pattern of the URL generated when you perform the search:

http://www.example.com/...?ParameterOne=123&ParameterTwo=Two&ThisParameter=Sth&ParameterFour=Four...

What I want to edit is "ThisParameter" and change its value. This is the part edited in the DOM that does what I want:

Foobar = {
_options: [],
...
var options = {"ParameterOne":123,"ParameterTwo":"Two","ThisParameter":"ABC","ParameterFour":Four,...}
...

And this is the output of "ThisParameter" when you choose "Copy path" in Firebug's DOM tab:

_options[0].ThisParameter

I am wondering it this is possible at all. What makes me think that it is, is the fact that I can change this parameter in Firebug and it works perfectly. So, if Firebug can edit it, there should be a way to influence it with another script.

Looking forward to any suggestions, thank you in advance!

take2
  • 616
  • 2
  • 17
  • 31
  • 1
    Can you add a hidden input to the form whose values are sent when the Search button is pressed? If you can, what happens when you add ``? – Stefan Oct 23 '12 at 18:39
  • @Stefan Unfortunately not, everything is loaded together. But I have tried to do it with Firebug when it's displayed, it's not working. – take2 Oct 23 '12 at 18:43
  • 1
    Who owns the javascript code ? Can't you ask the developer to add your feature ? If you are using a known plugin, can you tell us which it is ? – LeGEC Oct 31 '12 at 09:32

3 Answers3

1

Since you cannot edit the dynamic script you have the following options:

  1. You have to try to give the script the correct input and hope it uses your value.
  2. Add a script to the results page which will read the url and arguments, change it and redirect, as we discussed here. (If you put everything in functions it should not conflict with the dynamic script if the functions are uniquely named.)

You could try adding something like this jQuery code to the page with the search button:

$('input[name=search_button_name]').click(function(e) {
    e.preventDefault();
    var form_search = $('#search_form_id');
    $('<input>').attr({
        type: 'hidden',
        name: 'ThisParameter',
        value: 'SomethingElse'
     }).appendTo(form_search);
     f.submit();
});
Community
  • 1
  • 1
Stefan
  • 3,850
  • 2
  • 26
  • 39
  • 1
    There are no form tags, only fieldset. There is no unique ID, they change on every page load. Only thing unique is the class of the button and of the DIV that contains the HTML output. – take2 Oct 23 '12 at 19:12
  • I have edited the code a bit, maybe it's easier to understand now. I thought that this will be much easier than the previous question, but it obviously isn't :/ – take2 Oct 23 '12 at 19:17
  • 1
    @take2: OK, sounds like the javascript/jquery fires on the search button click event, reads the fieldset inputs, creates a search url, then sends the values to the remote url via AJAX, and on succesfull callback the output div gets loaded. But I don't understand what you are actually able to edit? Where are you able to add HTML or javascript or php code (or whatever), in this process? – Stefan Oct 23 '12 at 19:25
  • 1
    My js knowledge is modest, but I think it's like that. I can try to find the click event in the DOM... I just use one line of code do "call" the script and then it gets displayed dynamically. No script control, no HTML control. I get the output and that's it when it comes to editing. Therefore these scripts are my only option to edit it. – take2 Oct 23 '12 at 19:29
  • If one of the fieldset inputs are named "ThisParameter" then we can catch the mousedown or mayb mouseover event of the button and change the value of "ThisParameter", before the other script fires on the click event. – Stefan Oct 23 '12 at 19:34
  • Unfortunately, there isn't any input with such name. Thank you really for helping out with this, I suppose it's really annoying to see how complicated this is, I know that it's making me frustrated. It seems that this piece of code I have cited in the question is the only place where this parameter is defined. But is there any other approach, where we wouldn't concentrate on changing the URL, but e. g. to just block this "word" of code and replace it with another word? – take2 Oct 23 '12 at 19:42
  • Well, since FireBug is able to catch that code and allows you to edit it, there must be a way...? But I don't think it's possible for us to modify the code in the browser's memory with javascript like this, before it's executed. – Stefan Oct 23 '12 at 19:46
  • Last try: Maybe what we can also do is to try to generate the correct url yourself when the search button is clicked. Then we use ajax to call that url and we write the output into the div ourselves AFTER the 'incorrect' ajax call has been made. This would be quite easy to do, if we can supply/find the values of the other parameters. – Stefan Oct 23 '12 at 19:47
  • Unfortunately, there is again some dynamic reading involved, because the values in the URL generated aren't always exactly the same as the content in the input fields. So we couldn't replicate it. Incredibly complicated indeed. I agree that there should be some way to edit it if Firebug can do it, hopefully I'll find out once. Thank you for your efforts! – take2 Oct 23 '12 at 19:55
0

You can override any js function and method, or wrap you code around it. The easiest thing would be to look at the code you get and once it gets loaded, you re-declare a method with your own functionality.

I you are trying to replace a parameter in a specific jquery request, you can even wrap around the jquerys ajax method:

var jquery_ajax = $.ajax
$.ajax = function(options){
    // parse only a specific occurence
    if(options.url.indexOf("example.com") > -1) {
           // change the url/params object - depending on where the parameter is
        options.params.ThisParameter = "My Custom value"
    }
    // call the original jquery ajax function
    jquery_ajax(options);
}

But it would be a lot cleaner to override the method that builds the ajax request rather than the ajax request itself.

nxtwrld
  • 1,942
  • 14
  • 15
0

I would investigate further on the scope of the variable options (var options), is it global? i.e. if you type 'options' in the Firebug console, does it display its properties?

If so, you could then access it via your own script and change is value, e.g.

options.ThisParameter = 'my-own-value';

You might hook your script to the click event of the search button.

I hope this helps, it could be more specific maybe if you have some sample code somewhere.

David Riccitelli
  • 7,491
  • 5
  • 42
  • 56