Solved

WebControl - Modify the javascripts location.href variable, without redirect

Posted on 2008-06-25
10
1,219 Views
Last Modified: 2008-07-07
Hi,
Basically, I have a custom browser made with Visual basic's Web Control object.

I have added one to the stage, and if I load a temporary file (Say c:\temp.html) into it, and call a javascript that says

alert(location.href)

It will bring back file:\\c:\temp.html

I would like to modify this to be my companies main website url. I can modify the file before hand, but I am unsure how to change javascript's location.href variable without it trying to refresh the page, but so every time javascript calls it, or location.path etc, it will use my custom values.

Any help is appreciated
0
Comment
Question by:ocnod1234
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 3

Accepted Solution

by:
GHCS_Mark earned 250 total points
ID: 21870804
You can not do that without completely rewriting the runtimes.  The Location.href javascript function takes the current windows address location.  Therefore, if you modify this value it will attempt to navigate there automatically.  Your only way around this is to set up a website on a local instance of IIS using the header value property and modify your C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS. file to point at your local machine for that domain:

127.0.0.1 www.mywebsite.com
0
 
LVL 17

Assisted Solution

by:zzzzzooc
zzzzzooc earned 250 total points
ID: 21870932
You could just replace any references to 'location.href' with your custom URL.


Private Sub Command1_Click()

    Const html_original As String = _

        "<html><head><script language=javascript>alert(location.href);" & _

        "</script></head><body><b>testing</b></body></html>"

    Dim html_new As String

    html_new = Replace$(html_original, "location.href", "'http://www.site.com'")

    WebBrowser1.Navigate "about:blank"

    WebBrowser1.Document.open

    WebBrowser1.Document.write html_new

    WebBrowser1.Document.Clear

End Sub

Open in new window

0
 

Author Comment

by:ocnod1234
ID: 21885940
Hi,
Thanks for your comments

GHCS - I've thought about doing this with a tomcat server, but when looking into it I realised all traffic to that domain would be transferred to the tomcat server, including images and external javascripts. The problem being that I would have to build in a pretty extensive tomcat apache REWRITE_URI and possible a php system for what I wanted to be a fairly simple project :)

zzzzzoc - I have tried this method and the only issue becomes when either the javascript is included through a <script src=""> tag, or if the location.href is locationed inside of a <a href='#' onClick='window.location.href = location.href +"?id=video1'">

In either of the above 2 cases, the replace would fail, and since I don't have control over the websites it's failing on (Mail.yahoo.com) , not to mention the sites I don't know it will fail on I'm not sure that I could account for that and also account for only javascript files. I'm decent with VisualBasic, but no where near advanced  :)

Aside from writing a download routine to download the external javascripts by parsing the <script tag and pulling the source url, and then replacing it in the original with an actual javascript tag and then run the routines on it, while making sure it keeps in <script tags only, aswell as onClick and other behavior tags, and doesn't get replaced on say a website talking about the subject, I think it might work, but I can't begin to imagine how I would start a project to parse that complex of a scenario and replace the location.href for each one without interfering with the workings of the script. Is it surrounded in a " " , or is it used as a string in a javascript, or is it inside of the text of the website and not being used as a script?

If theres something I'm missing, and believe me, there probably is :) Please let me know, I've been working on this and will post any solutions I might find that seem more robust and managable.

Thanks
0
 
LVL 17

Expert Comment

by:zzzzzooc
ID: 21887864
>>and since I don't have control over the websites it's failing on
I thought you were working with local files?

I don't think you'll be able to easily override the location property unless you override the actual DOM itself with your own classes, if possible. Even then, while working with remote domains, security (and other) issues may arise when the location doesn't genuinely match the content.

If you can pre-download the pages, modify them appropriately then display them locally, it might be the easiest (yet lengthy) solution. From what I've worked with, the <head> element (which includes the scripts) is somewhat static and can't be modified without destroying the original document (ie: what you've already been doing). I would figure it'd be more dynamic and maybe I'm wrong, but my tests are a no-go.

If you do attempt my approach, in regards to .js files, you can pre-download them and append them to the <head> element. It should work, and if not (due to reasons mentioned above), IE would probably accept <scripts> in the <body>. element.
0
 
LVL 3

Expert Comment

by:GHCS_Mark
ID: 21894677
What exactly is the reason for the custom browser, and what are you trying to achieve?  Your comments here seem to suggest there is a bigger picture that would help in answering your question.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:ocnod1234
ID: 21894765
Hi GHCS,
Basically I would like a way to create an ad-free browser. Since a lot of ad's are generated by big websites with a single <script src=""> include method, then I figured, well, easy, I'll just replace the url with something to my server that's blank.

Turned out that wasn't so easy, and once I replaced the outerHTML it didn't update. If I replaced innerHTML, sure, it worked, but ran the javascript from the ads first, kinda defeating the purpose. I tried the navigate to about:blank then write the content in, and that kind of worked, but I ran into issues with location.href pointing to the temporary file. It seemed the most promising of my tests though, as it's the only one that let me modify the content prior to it running the onLoad javascripts.

Hope this helps explain it a bit better.
0
 
LVL 10

Expert Comment

by:cool12399
ID: 21895276
Actually, in response to my previous response...

If you really wanted to do this, you would add some javascript code to change it to the link you wanted.

Let's say you had 'mywebsite.com' as your website.

Then you would add javascript within the page like:
<script language="JavaScript"><!--
parent.document.links[0].href = 'mywebsite.com';
//--></script>

0
 

Author Comment

by:ocnod1234
ID: 21895848
Hi Cool,
Thanks for your comment and time.

The only problem with the solution proposed above is that it doesn't replace the javascript driven links that use the location.href object to populate the links dynamically (As found in mail.yahoo.com)

If you can use the temp file method, and navigation and login to and use mail.yahoo.com successfully, then this will definitely work for me,  and I will give it a shot shortly, but from my tests, yahoo uses the onLoad function to redirect using location.href as a base url , and that redirection that happens at on load is the issue I'm having trouble fixing.

Hope this helps better explain the situation, my biggest issue is that mail.yahoo.com doesn't work, and the reasoning is the location.href, but it's not an actual html link, so the javascript to replace the links[0] might not work because it's a dynamically driven variable loaded at the onLoad statement.

I will attempt adding this directly after the HEAD tag, and then work from there. If it does indeed change the location.href object's reference link, then I give you props :)

The only other solution I've seen recently is to move into using Firefox's open source system as it allows full access to everything, unlike IE's web browser control where it's more like a wrapper object.

Thanks,
Zack M.
0
 
LVL 10

Expert Comment

by:cool12399
ID: 21902533
PS, please close the previous question that I answered as per the EE rules.

Thanks.
0
 

Author Comment

by:ocnod1234
ID: 21902729
Hi Cool,
the previous question still has yet to receive a working solution. The fact that it works in some cases, but not all I honestly do not feel as a solution, since I am trading one issue for another.

As I have thoroughly explained my situation, including the one you mentioned as the solution, prior to your posting, I would clearly see this as proof that I had tried your method, and did not find it a viable solution, which is why I included the Code Set 3 in my original post to show that I have already attempted this method but ran into issues, so therefore it's not a solution, but a work-around with more issues that arise.

As I mentioned in my previous post on there, Yours is the closest to a solution, but it is with severe complications, and unless these can be overcome, it's not a true solution which I hope you understand. I also stated that since this seems to be un-solvable, at least through microsofts web control, that if I did not receive a valid solution any time soon, then I would mark yours as the accepted solution to close the question, but my issue still remains, therefore I do not wish to close it. I am paying for a membership in the hopes that I would find a simple way to replace outterHTML content on a site without reprocutions, which I have not found yet.

Thanks,
Zack M.

P.S. I really do appreciate the time you took in your answers and replies and time looking into this, so please don't take it the wrong way, I am in no way trying to cheap any body out on points, I simply want to find a working solution to my issue that doesn't simply bring up different issues in it's place that soley arise from implementing the first solution.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now