i have posted a lot of code to interface with the Document Object Model of IE or the WebGadget.
Since such things (catching links and writing code to it) have been
requested for quite a while, i decided to put it into some simple functions
for easy usage. As you can see from the example below, there is no
knowledge of COM or Interfaces needed.
If you want to do more specific things than these functions allow, i suggest
you have a close look on the above mentioned thread. It has examples
of how to get individual html elements and setting up various event callbacks.
Its all one includefile. You can get it here:
http://freak.purearea.net/code/WebGadgetExtras.pb
Procedure description:
WebGadget_IsLoaded(#Gadget)
Returns 1 if the page has finished loading and 0 if not. This must be
checked before setting up the links callback. (as it requires the page to be fully loaded)
This is only needed if the page is loaded from a file or from the web.
(not needed the code is directly written with the procedures below)
WebGadget_CatchLinks(#Gadget, @Callback())
This sets up a callback that will receive all link clicks in this page.
It must be called when the page is fully loaded. The link callback is set
for the currently present links only. If the page is reloaded, or the contents
are changed (new page), you have to call this again!
Currently the page may not contain frames for this to work.
The callback must look like this:
Code: Select all
Procedure LinkCallback(Gadget, Link$, Text$, ID$, Class$)
ProcedureReturn 0
EndProcedure
Link$ : receives the target set by the "href" attribute.
Text$ : receives the displayed text.
ID$ : receives the ID attribute of the link.
Class$ : receives the classname for the link.
If you return 0 from the callback, the link will not be executed. If you return
a non-zero value, then the link will be executed. (the target file will be loaded.)
WebGadget_Open(#Gadget, AddHistory)
Opens the WebGadget to write new code into it. Set AddHistory to non-zero
if you want the new page to be a new entry in the history. Set it to 0
if the new content should just replace the old page.
WebGadget_Write(String$)
Writes the given string into a previously opened WebGadget.
WebGadget_Close()
Completes the writing of new code into the WebGadget.
Ok, finally a short example for the usage. Its really quite simple:
Code: Select all
XIncludeFile "WebGadgetExtras.pb"
#WebGadget = 0
Procedure LinkCallback(Gadget, Link$, Text$, ID$, Class$)
Debug "Link: "+Link$
Debug "Text: "+Text$
Debug ""
ProcedureReturn 0 ; prevent link from executing
EndProcedure
If OpenWindow(0, 0, 0, 800, 600, #PB_Window_SystemMenu|#PB_Window_ScreenCentered, "WebGadget example")
If CreateGadgetList(WindowID())
WebGadget(#WebGadget, 0, 0, 800, 600, "")
If WebGadget_Open(#WebGadget, 0)
WebGadget_Write("<html><body><center><br>")
WebGadget_Write("<a href="+Chr(34)+"http://www.purebasic.com"+Chr(34)+">PureBasic.com</a><br>")
WebGadget_Write("<a href="+Chr(34)+"http://forums.purebasic.com"+Chr(34)+">Forum</a><br>")
WebGadget_Write("</center></body></html>")
WebGadget_Close()
EndIf
WebGadget_CatchLinks(#WebGadget, @LinkCallback())
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
EndIf