tags:

views:

477

answers:

4

I am trying to detect which web in sharepoint that the user is looking at right now. One approach could be to read the URls from the browser and try to compare them to a reference URL to the sharepoint solution. I have not yet been able to locate any solution that works in both IE and Firefox.

The idea is to write a small C# app that will harvest the URLs and do the comparing.

TIA

+1  A: 

You are unlikely to find such an answer. All modern browsers restrict the ability of JavaScript on a page to access such info as it poses such a big privacy risk to the user.

David Arno
A: 

Actually I was thinking about writing a small C# app that should fetch the IE and Firefox instances on the box and then query each instance about the URLs. Is that possible ?

Kasper
+1  A: 

It is possible to do this in a very hacky and prone to breakage way using the Win32 API function FindWindow.

The following C++ example that finds a running instance of the windows Calculator and gets the value of the edit field in it. You should be able to do something similar in C#. Disclaimer: I haven't actually checked to make sure this code compiles, sorry. :)

float GetCalcResult(void)
{
    float retval = 0.0f;

    HWND calc= FindWindow("SciCalc", "Calculator");
    if (calc == NULL) {
        calc= FindWindow("Calc", "Calculator");
    }
    if (calc == NULL) {
        MessageBox(NULL, "calculator not found", "Error", MB_OK);
        return 0.0f;
    }
    HWND calcEdit = FindWindowEx(calc, 0, "Edit", NULL);
    if (calcEdit == NULL) {
        MessageBox(NULL, "error finding calc edit box", "Error", MB_OK);
        return 0.0f;
    }

    long len = SendMessage(calcEdit, WM_GETTEXTLENGTH, 0, 0) + 1;
    char* temp = (char*) malloc(len);
    SendMessage(calcEdit, WM_GETTEXT, len, (LPARAM) temp);
    retval = atof(temp);
    free(temp);

    return retval;
}

In order to find out the right parameters to use in FindWindow and FindWindowEx, use the Visual Studio tool Spy++ to inspect a running instance of your browser window. Sorry, I don't have a code sample for web browsers on-hand, but it should be possible. Note that your solution will be Windows OS specific, and also changes to the UI architecture in future versions of the web browsers could cause your solution to stop working.

Using this method to lift the URL right out of the address bar obviously only works for the current tab. I can't see how this would work for all tabs unless you did something really tricky like simulating user input to cycle through the tabs. That would be very intrusive and a user could easily mess up your application by interrupting it with input of their own, but it might work if you're writing something that runs unattended, like an automated test script. If that is the case, you may want to look into other tools like AutoIt.

This advice is all paraphrased from a blog post I once wrote. Good luck!

Parappa
A: 

Just off the top of my head, you might consider using the built-in Firefox language (no idea what it's called). I'm sure it provides a mechanism to do exactally what you talking about. Otherwise those plugin's written for delicious, etc, wouldn't work.

As for IE, you're going to need to either do it in C++ or find some managed wrapper for this. I'm not sure how to make an IE plugin, but if you dig deep enough, you should be able to find something.

Cheers!