Solved

Webbrowser Control and Javascript

Posted on 2006-07-24
2
1,000 Views
Last Modified: 2013-12-25
Hello guys,

What i want to do is to call a vb function when javascript alert function is run from the webbrowser window. So that no alertbox will appear on the screen and page will continue running... I've tried several ways before, one of them is to overwrite the existing javascript function, but it doesnt prevent the alerts when the page is loading... etc...

in short here is what i want;
when called from the browser window

alert("Hello") must call my alerthandle function in vb with "Hello" as parameter

if its not possible then i am looking for a way to disable alert boxes...

Thanks.


Cem
0
Comment
Question by:cem_turk
  • 2
2 Comments
 
LVL 17

Accepted Solution

by:
vb_elmar earned 500 total points
ID: 17175414
Hi,

JavaScript Messageboxes (e.g. alert"Hello") are modal windows, and a standard VB Timer1 timer stops, when a
modal JavaScript Messagebox pops up.

So, a normal VB timer would not be helpful.

To detect Javascript alert dialog boxes, you need a subclassing timer
that doesn't stop, when a VB MsgBox or a JS MsgBox pops up.

The following sample works with a subclassing Timer.

How the sample works :

1)   It detects the alertbox (using findwindow).
2)   If found, it sends a sendkeys"{Enter}".
---> Then the alertbox will disappear


Needs:
======
1 Form with "WebBrowser1" (press Ctrl-T   --->  "Microsoft Internet Controls")
1 Module
1 Class module (name: TimerInterface)





=========
Form1:
=========
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SetTimer& Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
Private Declare Function KillTimer& Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long)


Dim cnt%
Private TIS As TimerInterface 'declare Interface
Implements TimerInterface 'and Implement interface to form


'**********************REQUIRED BY DELEGATON PER METHOD!************************
Private ObjectProcedures() As FunctionSPointerS '<------Class Procedure pointers and addresses!
Private HMEM As Long   '<--------------Memory Handle
Private TIMERPROC As Long  '<---------AddressOf Delegator (who call our Class Method!)
Private HTIMER As Long '<--------Timer Handle
'*******************************************************************************

Private Sub Form_Activate()
    WindowState = 2: BackColor = RGB(166, 166, 144): AutoRedraw = True
    Me.Caption = ""
    WebBrowser1.Move 0, 0, Screen.Width / 2, Screen.Height * 0.9: DoEvents
   
   
       
    WebBrowser1.Navigate "C:\WINDOWS\Desktop\Msgbox.HTM"
       
    Do
    a = WebBrowser1.ReadyState
    If a <> myState Then myState = a: Me.Caption = Me.Caption & "      readystate-->:" & a
    DoEvents
    Loop While a <> myState Or a <> 4
   
    Caption = Caption & "      ***READY***"
   
   
    Set o = WebBrowser1.Document
End Sub

Private Sub Form_Load()
'******************
'REQUIRED! Set Interface Procedure Pointer on TIS to Our Form Implemented Interface!!!
Set TIS = Me
'******************


'REQUIRED BY DELEGATION PER METHOD!**********************************************
ObjectProcedures = GetObjectFunctionsPointers(TIS, 1)
HMEM = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, CalculateSpaceForDelegation(4))
TIMERPROC = GlobalLock(HMEM) 'Get Address
DelegateFunction TIMERPROC, TIS, ObjectProcedures(0).FunctionAddress, 4
'USE DELEGATION For 1st Method In SubclassingInterface Class!!! -->ObjectProcedures(0)
'********************************************************************************


'Set Timer!
HTIMER = SetTimer(Me.hwnd, 0, 0, TIMERPROC) '<<---Call ASM Code!

End Sub

Private Sub Form_Unload(Cancel As Integer)
'Kill Timer!
KillTimer Me.hwnd, HTIMER

'Free Memory Allocated For Delegation
Call GlobalUnlock(HMEM)
Call GlobalFree(HMEM)
End Sub


Private Sub TimerInterface_TIMERPROCEDURE(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
    If cnt = 0 Then
    Beep
    Caption = "dwTime: " & dwTime
        Dim fw&
        fw = FindWindow("#32770", "Microsoft Internet Explorer")
        If fw Then SendKeys "{Enter}"
    End If
    cnt = IIf(cnt = 14, 0, cnt + 1)
End Sub





==========
Module1:
==========
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
Declare Function GlobalFree Lib "kernel32.dll" (ByVal HMEM As Long) As Long
Declare Function GlobalLock Lib "kernel32.dll" (ByVal HMEM As Long) As Long
Declare Function GlobalUnlock Lib "kernel32.dll" (ByVal HMEM As Long) As Long


Public Const GMEM_MOVEABLE As Long = &H2
Public Const GMEM_ZEROINIT As Long = &H40


Public Type FunctionSPointerS
FunctionPtr As Long
FunctionAddress As Long
End Type

Public Function CalculateSpaceForDelegation(ByVal NumberOfParameters As Byte) As Long
CalculateSpaceForDelegation = 31 + NumberOfParameters * 3
End Function

Public Function DelegateFunction(ByVal CallingADR As Long, Obj As Object, ByVal MethodAddress As Long, ByVal NumberOfParameters As Byte) As Boolean
On Error GoTo NotSuccess
Dim TmpA As Long
TmpA = CallingADR
CopyMemory ByVal CallingADR, &H68EC8B55, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, TmpA + 31 + (NumberOfParameters * 3) - 4, 4
CallingADR = CallingADR + 4

Dim StackP As Byte
StackP = 4 + 4 * NumberOfParameters

For u = 1 To NumberOfParameters
CopyMemory ByVal CallingADR, CInt(&H75FF), 2
CallingADR = CallingADR + 2
CopyMemory ByVal CallingADR, StackP, 1
CallingADR = CallingADR + 1
StackP = StackP - 4
Next u

CopyMemory ByVal CallingADR, CByte(&H68), 1
CallingADR = CallingADR + 1
CopyMemory ByVal CallingADR, ObjPtr(Obj), 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CByte(&HE8), 1
CallingADR = CallingADR + 1
Dim PERFCALL As Long
PERFCALL = CallingADR - TmpA - 1
PERFCALL = MethodAddress - (TmpA + (CallingADR - TmpA - 1)) - 5
CopyMemory ByVal CallingADR, PERFCALL, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CByte(&HA1), 1
CallingADR = CallingADR + 1
CopyMemory ByVal CallingADR, TmpA + 31 + (NumberOfParameters * 3) - 4, 4
CallingADR = CallingADR + 4
CopyMemory ByVal CallingADR, CInt(&HC2C9), 2

CallingADR = CallingADR + 2
CopyMemory ByVal CallingADR, CInt(NumberOfParameters * 4), 2

'FINALLY !!! ABSOLUTE CALLING RUTINE!


'WHAT IS BEHIND ASM CODE:
'*****************************
'PUSH EBP
'MOV EBP,ESP
'PUSH OFFSET RETURN ADDRESS

'*********** Depend on Number of Parameters
'PUSH EBP+XX
'  .......
'PUSH EBP+10
'PUSH EBP+0C
'PUSH EBP+08
'***********

'PUSH OBJECT POINTER
'CALL POINTER OBJECT.METHOD
'MOV EAX,DWORD PTR [OFFSET RETURN ADDRESS]
'LEAVE
'RET 00XX Depend on Number of Parameters
'TEMPSTORE dd 00 <------RETURN ADDRESS PTR

'Thats IT! Nothing less than 39 BYTES Of ASM Code!

DelegateFunction = True
Exit Function
NotSuccess:
On Error GoTo 0
End Function
Public Function GetObjectFunctionsPointers(Obj As Object, ByVal NumberOfMethods As Long, Optional ByVal PublicVarNumber As Long, Optional ByVal PublicObjVariantNumber As Long) As FunctionSPointerS()
Dim FPS() As FunctionSPointerS
ReDim FPS(NumberOfMethods - 1)
Dim OBJ1 As Long
OBJ1 = ObjPtr(Obj)
Dim VTable As Long
CopyMemory VTable, ByVal OBJ1, 4
Dim PTX As Long
Dim u As Long
For u = 0 To NumberOfMethods - 1
PTX = VTable + 28 + (PublicVarNumber * 2 * 4) + (PublicObjVariantNumber * 3 * 4) + u * 4
CopyMemory FPS(u).FunctionPtr, PTX, 4
CopyMemory FPS(u).FunctionAddress, ByVal PTX, 4
Next u
GetObjectFunctionsPointers = FPS
End Function




=======================
Class module (name: TimerInterface)
=======================
Public Sub TIMERPROCEDURE(ByVal hwnd As Long, ByVal uMsg As Long, ByVal idEvent As Long, ByVal dwTime As Long)
End Sub



0
 
LVL 17

Expert Comment

by:vb_elmar
ID: 17175420
==============================
File "C:\WINDOWS\Desktop\Msgbox.HTM" :
==============================


<html><head><title>Test</title>
<script type="text/javascript">
function msgbox () {
  alert('Hello World');
}
</script>

</head>
<body id="HauptBody" onload="javascript:msgbox()" bgcolor="#00FFFF">
</body>
</html>
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

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

8 Experts available now in Live!

Get 1:1 Help Now