-1

In this case, Let's take Google as example:

The code is JScript .NET, which is basically a .NET version of Javascript. Regardless of language, Anyone with appending type of skill can answer my question.

This code is used in Fiddler(It's a Man-in-the-middle proxy)

if (oSession.uriContains("&q=")) // oSession is a Fiddler object session // uriContains() function, checks for case-insensitive string from the URI
    {
        var str = oSession.fullUrl;
        var sAppend = "test1+test2+test3";
        if (!oSession.uriContains(sAppend))
        {
            oSession.fullUrl = str.replace( "&q=","&q="+sAppend);
        }
    }

For those who are confused, It says, If &q= is present in the URI, replace/append &q= with &q=test1+test2+test3

Problem: It appends test1+test2+test3 instantly, when it sees &q= in the URL.

Basically, how do I make it wait until I click the submit/search button

Thank you.

jarid wilders
  • 25
  • 1
  • 7

2 Answers2

2

Identifying your problem

I'm assuming:

  1. you want to use Fiddler for your solution (since you're already using it)
  2. you've figured out how to alter the request URI (as shown in your example), and your problem is that you want to target only those requests where the "search" button was clicked, not auto-submitted searches

Isolating those requests that stem from the search button being pressed on google is not straightforward, but I came up with a hack that seems to work. Basically, the oq (original query) get parameter is only added when the user explicitly hits button/enter key, so we test for its presence in order to identify such requests.

Solution

In your OnBeforeRequest method in Fiddler Handlers class (where you're already putting your code), we'll:

  1. Check that request is to www.google.com and contains q parameter
    • If true, log (in the fiddler log) that a query was submitted to google.com & highlight request in pink
  2. Check that request contains oq parameter (original query)
    • If true, log alert that submit button was pressed & highlight request in Light Forest Green

Code

if(oSession.HostnameIs('www.google.com') && oSession.uriContains("&q=")){
    FiddlerApplication.Log.LogString('query submitted to google.com...');
    oSession['ui-backcolor'] = 'pink'; //highlight this request

    //test for original query
    if(oSession.uriContains('&oq=')){
        FiddlerApplication.Log.LogString('SUBMIT BUTTON OR ENTER CLICKED (probably)');
        oSession['ui-backcolor'] = '#369369'; //highlight in Light Forest Green
        //whatever sort of request manipulation you want to do here...
    }

}

Other notes:

  • I'm assuming you want to prepend your query string to the existing q value, i.e. q=Hello will become q=test1+test2+test3Hello. If you want to replace it you need More Regex.
  • Depending on your needs, Naomi or Ahmed's request may be better (it's in-browser, not in fiddler).
  • Modifying a request or response in Fiddler
  • Understanding Fiddlerscript
  • Your solution is perfect for me.Also, what does `FiddlerApplication.Log.LogString` actually do? Is it necessary for the code to work? Thank you so much. – jarid wilders Sep 24 '14 at 08:17
  • The `LogString` statements just output a message in the fiddler log; it's mostly for demo/debugging here & no you don't need it. Same goes for changing the background color- it's just a visual indicator that your script is working as expected & is not necessary (tho I recommend keeping it, it's useful IMO). – Stop Slandering Monica Cellio Sep 24 '14 at 14:53
  • Could you please check this link, very similar question:http://stackoverflow.com/q/25892688/3987368 – jarid wilders Sep 24 '14 at 17:35
  • See the last two resources above under "other notes." You've been given a fish as well as a fishing lesson. Now go cast your line & stop asking for more fish. :) (you are asking for fully working solutions one after the next & it doesn't seem like you are trying stuff/working to find a solution on your own.) – Stop Slandering Monica Cellio Sep 24 '14 at 18:03
  • I can't see no fish, I need to find a good fishing spot.:-) (Just a doubt, Is it possible to remove a small URL parameter sent by the server via Fiddler?) A hint would be nice. Thank you – jarid wilders Sep 24 '14 at 18:35
  • @jaridwilders Just like the guy on the other question said, "Server's don't send URLs." I suspect this is an [XY Problem](http://mywiki.wooledge.org/XyProblem), step back & ask your high level question. What is your goal? – Stop Slandering Monica Cellio Sep 24 '14 at 20:21
  • Let me be more clear. In relation to my question, I want the opposite results, I want to detach/remove a querystring before it reaches my web browser.In `OnBeforeResponse` I replaced querystring `test` with `StackOveflow`, it shows properly in Fiddler, but not in Google chrome. http://i.imgur.com/4dgvIYX.png .If this is not possible with Fiddler,then what other solutions do you recommend? – jarid wilders Sep 25 '14 at 12:38
  • Misdirecting someone about the page they're on is a very sketchy goal. What is your goal? – Stop Slandering Monica Cellio Sep 25 '14 at 15:01
  • I appended `-adult` to search query, so that it will not show me adult videos. My kids will notice, if they read the address bar, I don't want to them to notice the tampering. Any solutions? I want to do this with Fiddler, if possible. – jarid wilders Sep 25 '14 at 15:04
  • I can change HTML content in OnBeforeResponse but not a query or a parameter in URL. Is there a reason for that? – jarid wilders Sep 25 '14 at 17:10
  • @jaridwilders basically you are trying to trick your users (your children). This sort of trickery, if the browser supported it, would be a MAJOR security problem, and there's not really any legitimate reason to do this, so No, you can't do this. – Stop Slandering Monica Cellio Sep 25 '14 at 18:49
1

Well in Javascript you can bind actions to events. In your case the event is the submit of the form For example:

function addEventsToHTML(){
    var form1 = document.getElementById('form1');
    form1.onsubmit = submitHandler;
    function submitHandler(){
        alert("You just submit: "+this.email.value);
    }
}

If you want to bind it on click you can do:

object.onclick=function(){myScript};

Or

object.addEventListener("click", myScript);

Note: The addEventListener() method is not supported in Internet Explorer 8 and earlier versions.

AhmadAssaf
  • 3,556
  • 5
  • 31
  • 42
  • I want to test this code specifically on Google Search. How can I do this? – jarid wilders Sep 23 '14 at 12:51
  • go to a website like css-tricks.com (i choose this because i am not sure if the Google form ID is auto generated or not, so that you might not get the same ID always). Anyhow, just type this into the console and try to search `document.getElementById('search-form').addEventListener("click", function() { alert('OK')});` – AhmadAssaf Sep 23 '14 at 13:18