The included version has been extended with page loading via Clipboard.
It is used to deliver info about programs in the launcher etc.
Due to the excellent structure of the program, the changes were easy to make.
Changes are marked in red.
The main loop (marked in green) has been simplified by the addition of the function 'Buttonpressed'
I hope that Dav will adopt the changes
Code: Select all
'=========================================
'Simple web browser in smart BASIC - v1.08+
'=========================================
'Works on ipad, iphone, ipod touch
'Coded by Dav, December/2015
'
'A simple web browser made in smart Basic.
'Uses javascript for browsing functions.
'Has a basic bookmark manager. Only loads
'one page at a time, but I'm working on a
'multitab version right now...
'
'NEW:  Shows info bar at top: Connection
'      status, Time, Battery level.  The
'      current url now shown below menu.
'      You can select it to copy url.
'      Bookmark menu now drops down and
'      closes up out of sight (moving).
'r'
'Dutchman added conditional recall to launcher
'     and URL-transfer from launcher
''
'NOTE: Bookmarks saved in file in current
'      directory named: 'bookmarks.ini'
'      Homepage is set to GOOGLE.COM, but
'      you can change this by changing
'      the home$ variable below...
home$="http://www.google.com" '<<< change
'----- detect users device
if lowstr$(device_type$())="ipad" then
  dev=1
else
  dev=0
end if
'----- initialise
graphics
option base 1
set buttons custom
page "main" set
'----- init screen
graphics clear .4,.4,.4
set toolbar off
set browsers scaled 'allow zooming in/out
'----- get current screen width
curwidth= screen_width()
gosub drawinfobar
''
'r' ### Before loading startpage !!
''----- make buttons
draw color 0,0,0
fill color .9,.9,.9
if dev = 1 then
  set buttons font size 36
  button "back" text " "&chr$(9666)&" " at 10, 28
  button "forward" text " "&chr$(9656)&" " at 70, 28
  button "stop" text " "&chr$(9679)&" " at 130,28
  button "reload" text " "&chr$(8634)&" " at 194,28
  button "home" text " "&chr$(9751)&" " at 266,28
  button "bookmarks" text "Bookmarks..." at 344,28
  button "url" text "Url..." at 579,28
  button "quit" text "Quit" at 676,28
else
  set buttons font size 20
  button "back" text chr$(9666) at 3, 28
  button "forward" text chr$(9656) at 40, 28
  button "stop" text chr$(9679) at 78,28
  button "reload" text chr$(8634) at 113,28
  button "home" text chr$(9751) at 153,28
  button "bookmarks" text chr$(9873) at 195,28
  button "url" text chr$(9166) at 235,28
  button "quit" text "X" at 285,28
end if
'r'----- load the startpage
'  ### after buttons set !!!
IF Launch$="launcher" THEN url$=launch$.line$
IF url$="" THEN url$=home$
gosub loadpage
''
'g'----- M A I N  L O O P -----
do
  SLOWDOWN
  '------ handle buttons
  IF ButtonPressed THEN 'check AND reset all buttons at once
    if ._back then u$ = browser_text$("n", "window.history.go(-1)")
    if ._forward then u$ = browser_text$("n", "window.history.go(1)")
    if ._quit then break
    if ._home then ! url$=home$ ! gosub loadpage ! endif
    if ._url then gosub geturl
    if ._stop then u$ = browser_text$("n", "stop()")
    if ._reload then u$ = browser_text$("n", "location.reload(true)")
    if ._bookmarks then gosub bookmarks
  ENDIF 'buttonpressed
  '----- if screen rotated...
  if screen_width() <> curwidth then
    curwidth = screen_width()
    url$=browser_text$("n", "window.location.href")
    gosub loadpage
    gosub drawinfobar
  end if
  '----- update infobar and connection every 10 secs
  if time()>10 then
    refresh off
    gosub drawinfobar
    refresh on
    time reset
  end if
  '----- check current url location every other second
  'if it changes, update field to show the change
  if odd(time()) then
    title$=browser_text$("n", "window.location.href")
    ft$=field_text$("url")
    'if it's changed, show change in field
    if title$ <> ft$ then
      field "url" back color 1,1,1
      field "url" font color 0,0,0 
      if dev=1 then
        field "url" text title$ at 10,85 size screen_width()-20,24
      else
        field "url" text title$ at 5,65 size screen_width()-5,25
      end if
    end if
  end if
until 0
'r'----- Exit
Exit:
IF Launch$="launcher" THEN
  {/- Launcher.sb}
ENDIF
''
set toolbar on
page bm$ hide
page "main" hide
text
END
'===== S U B R O U T I N E S and F U N C T I O N S =============
'r'
DEF ButtonPressed
._back=button_pressed("back")
._forward=button_pressed("forward")
._stop=button_pressed("stop")
._reload=button_pressed("reload")
._home=button_pressed("home")
._bookmarks=button_pressed("bookmarks")
._url=button_pressed("url")
._quit=button_pressed("quit")
exit=._back+._forward+._stop+._reload
exit+=._home+._bookmarks+._url+._quit
RETURN exit
END DEF
''
loadpage:'-------
field "url" back color 1,1,1
field "url" font color 0,0,0 
if dev=1 then
  browser "n" url url$ at 0,110 size screen_width(),screen_height() -110 
  title$=browser_text$("n", "window.location.href")
  field "url" text title$ at 10,85 size screen_width()-20,24
else
  browser "n" url url$ at 0,95 size screen_width(),screen_height() -95
  title$=browser_text$("n", "window.location.href")
  field "url" text title$ at 5,65 size screen_width()-5,25 
end if
return
'
hidebuttons:'----------
button "back" hide
button "forward" hide
button "stop" hide
button "reload" hide
button "home" hide
button "bookmarks" hide
button "url" hide
button "quit" hide
return
'
showbuttons:'----------
button "back" show
button "forward" show
button "stop" show
button "reload" show
button "home" show
button "bookmarks" show
button "url" show
button "quit" show
return
'
geturl:'-----
gosub hidebuttons
if dev=1 then
  field "geturl" text "" at 0,0 size screen_width(), 60
else
  field "geturl" text "" at 0,0 size screen_width(), 38
end if
field "geturl" set text "http://"
field "geturl" select
if dev=1 then
  button "cancel" text "Cancel" at screen_width()-150,3
else
  button "cancel" text "Cancel" at screen_width()-80,3
end if
do
  if button_pressed("cancel") then goto skip
until field_changed("geturl") = 1
u2$ = field_text$("geturl")
if u2$ <> "" and u2$ <>"http://" then
   u2$=ltrim$(u2$)
   'make sure http:// given
   if capstr$(left$(u2$,7))<> "HTTP://" then
     u2$="http://"&u2$
   end if
   url$=u2$
   gosub loadpage
end if
skip:
button "cancel" delete
field "geturl" delete
gosub showbuttons
return
'
bookmarks:'--------
gosub loadbookmarks
sw=screen_width()
sh=screen_height()
'make bookmarks page
bm$ = "Bookmarks"
page bm$ set
page bm$ frame 0,0,sw-10,sh/2
page bm$ color .7,.7,.7,1
if dev=1 then
  page bm$ at 5,-1000 '60
else
  page bm$ at 5,-1000 '50
end if
button "bookmarks" hide
showbookmarks:
if dev=1 then
  list bm$ text list$(1) at 4,4 size sw-18,sh/2-62
else
  list bm$ text list$(1) at 4,4 size sw-18,sh/2-42
end if
list bm$ set text list$
if dev=1 then
  button "close" text "Close" at sw- 130,sh/2-57
  button "addpage" text "Add Current" at 5,sh/2-57
  button "deletepage" text "Delete page" at 225,sh/2-57
  button "deletepage" hide
  button "loadpage" text "Load page" at 442,sh/2-57
  button "loadpage" hide
else
  button "close" text "Close" at sw- 85,sh/2-36
  button "addpage" text "Add" at 5,sh/2-36
  button "deletepage" text "Delete" at 65,sh/2-36
  button "deletepage" hide
  button "loadpage" text "Load" at 145,sh/2-36
  button "loadpage" hide
end if
'only drop down menu first time
if bdown=0 then
  for yy = -500 to 100 step 4
    page bm$ at 5,yy
    pause .001
  next yy
  bdown=1 'mark as down
end if
do
  if list_selected(bm$) <> -1 and booknum > 0 then 
    button "deletepage" show
    button "loadpage" show
     
    if button_pressed("loadpage") then
      pause .2 'delay to look like a click
      url$=list$(list_selected(bm$))
      'page "main" set
      gosub showbuttons
      for yy = 100 to -550 step -4
        page bm$ at 5,yy
        pause .001
      next yy
      gosub loadpage
      goto bookdone
    end if
    if button_pressed("deletepage") then
      list$(list_selected(bm$))=""
      gosub deletebookmark
      gosub savebookmarks
      goto showbookmarks
    end if
  end if
  if button_pressed("addpage") then
     url$=browser_text$("n", "window.location.href")
     if url$<>"" then
        gosub addbookmark
        gosub savebookmarks
        goto showbookmarks
     end if 
  end if
  if screen_width() <> curwidth then
     curwidth = screen_width()
     url$=browser_text$("n", "window.location.href")
     gosub drawinfobar
     gosub loadpage
     goto bookmarks
  end if
until button_pressed("close")
'raise menu back up
for yy = 100 to -550 step -4
   page bm$ at 5,yy
   pause .001
next yy
bookdone:
page "main" set
gosub showbuttons
bdown=0
return
'
loadbookmarks:'------------
if file_exists("bookmarks.ini") =0 then
   makenew:
   'make default list of bookmarks
   booknum=5
   DIM list$(booknum)
   list$(1)="http://www.google.com"
   list$(2)="http://www.qbasicnews.com/dav/"
   list$(3)="http://www.yahoo.com"
   list$(4)="http://kibernetik.pro"
   list$(5)="http://www.bing.com"
   gosub savebookmarks
else
   file "bookmarks.ini" setpos 0
   file "bookmarks.ini" readline a$
   if a$ = "" then goto makenew
   booknum = val(a$)
   if booknum < 0 then goto makenew
   if booknum = 0 then
     dim list$(1)
   else
     dim list$(booknum)
     for p = 1 to booknum
       file "bookmarks.ini" readline a$
       'if invalid entry for some reason
       if a$ ="" then
         'last booknum was last
         bookmark = p -1
         break
       end if
       list$(p) = a$
     next p
   end if
end if
return
'
savebookmarks:'------------
'out with the old...
file "bookmarks.ini" delete
'in with the new...
if booknum = 0 then
   file "bookmarks.ini" writeline str$(0)
else
   file "bookmarks.ini" writeline str$(booknum)
   for p = 1 to booknum
     file "bookmarks.ini" writeline list$(p)
   next p
end if
return
'
deletebookmark:'-------------
if booknum > 1 then
  'temporary holder array
  dim temp$(booknum)
  bc=1
  for h=1 to booknum
    if list$(h) <>"" then
      temp$(bc)=list$(h)
      bc=bc+1
    end if
  next h
  'remake list$()
  booknum =bc -1
  dim list$(booknum)
  'copy temp$() to list$()
  for p = 1 to booknum
    list$(p)= temp$(p)
  next p
else
  booknum=0
end if
return
'
addbookmark:'----------
booknum=booknum + 1
if booknum = 1 then
  dim list$(1)
  list$(1) = url$
else
  'make temp array holder
  dim temp$(booknum)
  for p = 1 to booknum -1
      temp$(p) = list$(p)
  next p
  temp$(booknum) = url$
  'make new list$()
  dim list$(booknum)
  for p = 1 to booknum
     list$(p) = temp$(p)
  next p
end if
return
'
drawinfobar:'----------
sw=screen_width()
fill color .7,.7,.7
fill rect 0,0 to sw,25
draw color .3,.3,.3
ampm$="AM" ! hr=current_hour()
min$=str$(current_minute())
if len(min$)=1 then min$="0"&min$
if hr>12 then
  hr=hr-12 ! ampm$="PM"
end if
tm$=str$(hr)&":"&min$&" "&m$
draw text tm$ at ((sw/2)-text_width(tm$)/2),4
if dev=1 then
  bat$=str$(battery_level())&"%"
  draw text bat$ at sw-text_width(bat$)-80,4
end if
draw rect sw-70,5 to sw-20,19
f=battery_level()/2
fill color .3,.3,.3
fill rect sw-70,5 to sw-70+f,19
fill rect sw-20, 9 to sw-17, 14
if system_ext_ip$() ="" then
     fill color .7,.7,.7
     fill rect 0,0 to 110,25
     fill color .7,0,0
     draw color .3,.3,.3
     if dev=1 then
        draw text "OFFLINE" at 25,3
     else
        draw text "OFF" at 25,3
     end if
else 
     fill color .7,.7,.7
     fill rect 0,0 to 110,25
     fill color 0,.7,0
     draw color .3,.3,.3
     if dev=1 then
        draw text "ONLINE" at 25,3
     else
        draw text "ON" at 25,3
     end if
end if
fill circle 12,12 size 5,5
draw color 0,0,0
fill color .9,.9,.9
return

