Code: Select all
'Dav's Browser
'==========================
'Simple web browser - v1.04
'==========================
'A simple web browser for smart Basic.
'Uses javascript for browsing functions.
'Has a basic bookmark manager.
'Coded by Dav , FEB/2015
'
'NEW: Added bookmark manager page.
' You can now add/delete bookmarks.
' Bookmarks saved in file in current
' directory named: 'bookmarks.ini'
'NOTE: Homepage is set to google.com.
' You can change this by changing
' the home$ variable.
option base 1
page "main" set
'init screen
graphics clear 1,1,1
set toolbar off
set browsers scaled 'allow zooming in/out
'get current screen width
curwidth= screen_width()
'load the homepage
home$="http://www.google.com" 'homepage...
url$=home$
gosub loadpage 'load homepag
'make buttons
Buttons=8
DIM Button$(buttons,4)
ButtonsData:'ID,txt,x,y
DATA "back","<",10, 3
DATA "forward",">",60, 3
DATA "stop","Stop",110,3
DATA "reload","Reload",180,3
DATA "home","Home",260,3
DATA "bookmarks","Bookmarks...",340,3
DATA "url","Enter URL...",480,3
DATA "quit","Quit",680,3
GOSUB LoadButtons
CALL MakeButtons
'main loop...
do
if button_pressed("back") then
u$ = browser_text$("n", "window.history.go(-1)")
end if
if button_pressed("forward") then
u$ = browser_text$("n", "window.history.go(1)")
end if
if button_pressed("quit") then break
if button_pressed("home") then
url$=home$
gosub loadpage
end if
if button_pressed("url") then
gosub geturl
end if
if button_pressed("stop") then
u$ = browser_text$("n", "stop()")
end if
if button_pressed("reload") then
u$ = browser_text$("n", "location.reload(true)")
end if
if button_pressed("bookmarks") then
gosub bookmarks
end if
if screen_width() <> curwidth then
curwidth = screen_width()
gosub loadpage
end if
until 0
set toolbar on
gosub DeleteButtons
end
'==========================================
'. G O S U B S
'==========================================
'-------
loadpage:
'-------
browser "n" url url$ at 0,40 size screen_width(),screen_height() -40
return
'-------
LoadButtons:
RESTORE TO ButtonsData
FOR i=1 to Buttons
FOR j=1 to 4
READ Button$(i,j)
NEXT j
NEXT i
RETURN
'-------
DEF MakeButtons
FOR i=1 TO .Buttons
BUTTON .Button$(i,1) text .Button$(i,2) AT .Button$(i,3),.Button$(i,4)
NEXT i
END DEF
'-------
DeleteButtons:
FOR i=1 TO .Buttons
BUTTON .Button$(i,1) DELETE
NEXT i
RETURN
'----------
hidebuttons:
'----------
FOR i=1 TO .Buttons
BUTTON .Button$(i,1) HIDE
NEXT i
return
'----------
showbuttons:
'----------
FOR i=1 TO .Buttons
BUTTON .Button$(i,1) SHOW
NEXT i
return
'-----
geturl:
'-----
gosub hidebuttons
field "geturl" text "" at 0,0 size screen_width(), 38
field "geturl" set text "http://"
field "geturl" select
button "cancel" text "cancel" at 600,3
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
'make bookmarks page
bm$ = "Bookmarks"
PAGE bm$ SET
PAGE bm$ FRAME 0,0,600,300
PAGE bm$ COLOR .3,.3,.3,1
PAGE bm$ AT 50,50
showbookmarks:
LIST bm$ TEXT list$(1) AT 2,2 SIZE 595,260
LIST bm$ SET TEXT list$
BUTTON "close" text "close" AT 525,265
button "addpage" text "Add Current" at 20,265
button "deletepage" text "Delete Selected" at 160,265
button "deletepage" hide
button "loadpage" text "Load Selected" at 340,265
button "loadpage" hide
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
gosub loadpage
break
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
UNTIL BUTTON_PRESSED("close")
page "main" set
gosub showbuttons
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