Author Topic: Browser Preview Scripts for Server Side Languages  (Read 3910 times)

Theno

  • Guest
Browser Preview Scripts for Server Side Languages
« on: April 19, 2010, 05:28:29 PM »
Hi friends,

I wrote two scripts for browser preview of the page you currently work on, in IE and Firefox, using installed server on your PC. These scripts can be used by any web programmer who uses server side languages like ASP, ASP.NET, PHP, JSP etc. just with small changes.

Scripts are written for ASP language, other programmers can use it with changing the path string as they wish. IE browser preview opens the ie page automatically using the server string that you added. And if you run the script again while the old ie page is still open, it finds and closes the old one and opens a new one. I couldn't do it for firefox in same way because of the limitations of the windows scripting engine. But however it can be done with small programs like AHK etc. But I didn't implement something like this yet, since I want to keep the codes simple.

IE Browser Preview Script (for ASP)
Code: Visual Basic
  1. Set objArguments = Wscript.Arguments     'Create the Arguments object
  2.  
  3. If objArguments.Count > 0 Then
  4.                 file_path = objArguments.Item(0)                'Get the argument that we gave when we run the script
  5.                 root_place = Instr(1, file_path, "wwwroot", 1)          'Find the place of wwwroot string inside the path
  6.  
  7.                 If not root_place = 0 Then
  8.                         separate_from = root_place+8            'If we find wwwroot string, extend selection with 8 chars to the next \ that we will separate the path from
  9.                 Else
  10.                         dot_place = Instr(1, file_path, ":", 1)          'If we can't find wwwroot then seacrh for : char
  11.                         separate_from = dot_place+2      'Then extend selection with 2 chars to the next \ that we will separate the path from
  12.                 End If
  13.  
  14.                 If not separate_from = 0 Then
  15.                         file_path = Mid(file_path, separate_from)        'Separate path from the point that we got
  16.                         file_path = Replace(file_path, "\", "/", 1, -1, 1)              'Replace \ chars with / chars since we will open the path in the browser
  17.                         file_path = Replace(file_path, " ", "%20", 1, -1, 1)            'Replace space chars with %20 chars since we will open the path in the browser
  18.                         page = "http://localhost/" & file_path          'Add server address to the file path to run our server side script
  19.                 End If
  20.  
  21.                 'Create other necessary objects.
  22.                 Set objShell = WScript.CreateObject("WScript.Shell")
  23.                 Set objShellApplication = WScript.CreateObject("Shell.Application")
  24.                 Set objShellWindows = objShellApplication.Windows
  25.                 Set objIE = WScript.CreateObject("InternetExplorer.Application")
  26.  
  27.                 'Find the old window if it is opened already.
  28.                 For n = 0 to objShellWindows.Count - 1          'Search among the open IE windows
  29.                         Set IE = objShellWindows.Item(n)
  30.                         URL = IE.LocationURL     'Get the URL of IE window that we currently work on
  31.                         If URL = page Then              'If the URL of the window is equal to our file path. Then we found the old window that we search for
  32.                                 IE.Quit          'Close the old IE window since we will open a new one
  33.                         End If
  34.                 Next
  35.  
  36.                 objIE.Navigate page      'Open a new IE window and navigate to our page.
  37.                 IEName = objIE.Name      'Get the name of the window that we just opened
  38.                 objShell.AppActivate IEName      'Activate the window to focus and bring it to front.
  39. Else
  40.          MsgBox "No parameter given."
  41. End If
  42.  


Firefox Browser Preview (for ASP)

Code: Visual Basic
  1. Set WshArguments = Wscript.Arguments     'Create the Arguments object
  2.  
  3. If WshArguments.Count > 0 Then
  4.                 file_path = WshArguments.Item(0)                'Get the argument that we gave when we run the script
  5.                 root_place = Instr(1, file_path, "wwwroot", 1)          'Find the place of wwwroot string inside the path
  6.  
  7.                 If not root_place = 0 Then
  8.                         separate_from = root_place+8            'If we find wwwroot string, extend selection with 8 chars to the next \ that we will separate the path from
  9.                 Else
  10.                         dot_place = Instr(1, file_path, ":", 1)          'If we can't find wwwroot then seacrh for : char
  11.                         separate_from = dot_place+2      'Then extend selection with 2 chars to the next \ that we will separate the path from
  12.                 End If
  13.  
  14.                 If not separate_from = 0 Then
  15.                         file_path = Mid(file_path, separate_from)        'Separate path from the point that we got
  16.                         file_path = Replace(file_path, "\", "/", 1, -1, 1)              'Replace \ chars with / chars since we will open the path in the browser
  17.                         file_path = Replace(file_path, " ", "%20", 1, -1, 1)            'Replace space chars with %20 chars since we will open the path in the browser
  18.                         page = " http://localhost/" & file_path         'Add server address to the file path to run our server side script
  19.                 End If
  20.  
  21.                 Set WshShell = WScript.CreateObject("WScript.Shell")            'Create the Shell object
  22.                 firefox_path = """C:\Program Files (x86)\Firefox\firefox.exe"""         'Define the full path of firefox.exe on the PC
  23.                 Return = WshShell.Run (firefox_path & page,1)           'Run firefox with page you created and gave as a argument.
  24. Else
  25.          MsgBox "No parameter given."
  26. End If

You can find the explanations of the codes as comments near them also you can see the codes in the screenshots too. They are created and copied inside the main folder of HE but you can store it anywhere you want but this time you will need the give the full path of the script while creating the tool.

You can see the tool details that are needed to be created as screenshots too.

I hope it can be useful for other web programmers.

Special thanks to Alex and Stefan for their interest and helps. Also if you are interested in extending code with AHK please refer to Stefan's message in this topic.

http://forum.hippoedit.com/index.php/topic,747.msg3286.html#new

Thanks...
« Last Edit: July 14, 2011, 06:07:10 AM by alex »

Offline alex

  • Developer
  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2222
  • Karma: +37/-3
    • HippoEDIT
Re: Browser Preview Scripts for Server Side Languages
« Reply #1 on: April 20, 2010, 02:56:03 PM »
Hi Theno,

thanks a lot for your support! ;)
Useful post and good how-to. I am doing HE site in PHP, so I can try to use your scripts until "normal" support will be added into HE.

Checking your images, I got the idea, that it Will be good to add Import/Export possibilities for tools/help items in "Manage" dialogs that it will be easier to share them. I will note it as todo for 1.50.

Best regards,
Alex.

Theno

  • Guest
Re: Browser Preview Scripts for Server Side Languages
« Reply #2 on: April 20, 2010, 10:28:57 PM »
Hi Alex,

Quote
thanks a lot for your support!
Nop, I am glad you found them useful. ;)

Quote
Checking your images, I got the idea, that it Will be good to add Import/Export possibilities for tools/help items in "Manage" dialogs that it will be easier to share them. I will note it as todo for 1.50.

Yes this is a very good idea it can very time saving.

P.S : I added the scripts as attachment to the first post.

Thanks...
« Last Edit: April 20, 2010, 10:34:34 PM by Theno »