Webgadget capture and process urls with name=value pairs

Share your advanced PureBasic knowledge/code with the community.
User avatar
fiver
User
User
Posts: 36
Joined: Wed May 05, 2004 8:21 pm
Location: An outer spiral arm of the Milky Way

Webgadget capture and process urls with name=value pairs

Post by fiver »

Here's an example of capturing and processing webgadget get type urls with name=value pairs, tested on Linux, but should be cross platform.
Uses a timer to check what's happening to the url, this is a really simple example but could be developed to do more useful things.

Code: Select all

;-- OS specific
CompilerSelect #PB_Compiler_OS
CompilerCase #PB_OS_Windows
#FileSep$ = "\"
CompilerCase #PB_OS_Linux
#FileSep$ = "/"
CompilerCase #PB_OS_MacOS
#FileSep$ = "/"
CompilerEndSelect

;-- Randomise page name
File$ = GetCurrentDirectory() + "tmp" + #FileSep$ + Hex(Date()+ElapsedMilliseconds()) + ".htm"

DefaultURL$ =   "file://" + File$

;-- temp file for pages
CreateDirectory("tmp")

html$ = "<html style='overflow: auto;'>"
html$ + "<head>"
;-- Google fonts, cos we can ;-)
html$ + "<link href='http://fonts.googleapis.com/css?family=Alegreya' rel='stylesheet' type='text/css'>"
html$ + "<style type='text/css'>"
html$ + "div {margin:0.5em;}"
html$ + ".title {clear:both;padding-top:2em;font-size:2em;font-family:'Alegreya', serif;}"
html$ + "body {background:#EEEEEE;overflow:auto;}"
html$ + "</style>"
html$ + "<title>Title</title>"
html$ + "</head>"
html$ + "<body>"
html$ + "<div class='title'>Catch and process web page get type urls</div>"
html$ + "<div class='container'>"
html$ + "<div class='title'><a href='" + DefaultURL$ + "?state=foo'>foo</a></div>"
html$ + "<div class='title'><a href='" + DefaultURL$ + "?state=bar'>bar</a></div>"
html$ + "<div class='title'><a href='" + DefaultURL$ + "?state=bazuel'>bazuel</a></div>"

html$ + "</div><div style='clear:both;padding:2em;'>&nbsp;</div></body></html>"

MainWindow = OpenWindow(#PB_Any, 0, 00, 800, 600, "Catch and process web page get type urls", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)

If MainWindow
  If CreateFile(0, File$)
    WriteString(0,html$)
    CloseFile(0)
    Browser = WebGadget(#PB_Any, 0, 0, 800, 600, DefaultURL$)
    ;-- 250 mS also acts as hyperlink debounce
    AddWindowTimer(MainWindow, 123, 250)
  EndIf

EndIf

Repeat
  Event = WaitWindowEvent(5)
  Type = EventType()
  Gadget = EventGadget()
  Window = EventWindow()
  Menu = EventMenu()

  Select Event
    Case  #PB_Event_Gadget
    Case #PB_Event_Menu
    Case #PB_Event_Timer
      If  EventTimer() = 123
        ;-- Process the state parameter from the url
        State$ = GetURLPart(GetGadgetText(Browser), "state")
        ;-- Check State$ is actually set
        If State$ <> ""
          ;--Reset Browser to avoid multiple hits on the same state
          SetGadgetState(Browser, #PB_Web_Back)
          ;-- Probably do more complex things here, alter html and so forth
          Debug State$
        EndIf
      EndIf
    Case #PB_Event_CloseWindow
      Break
  EndSelect

ForEver
;-- purge temp file
DeleteDirectory("tmp", "*.*", #PB_FileSystem_Recursive)

End   
EDIT:
Corrected confusing combined url/file name.
Last edited by fiver on Sun Feb 19, 2012 11:23 am, edited 2 times in total.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Webgadget capture and process urls with name=value pairs

Post by luis »

I see, interesting way of using the webgadget in a crossplatform way :)

Thanks.
"Have you tried turning it off and on again ?"
Post Reply