BBjHtmlView::injectScript

Description

In BBj 16.0 and higher, this method injects the specified JavaScript into this BBjHtmlView control, making it available for subsequent use by BBjHtmlView::executeScript.

Syntax

Return Value

Method

void

injectScript(string script)

void

injectScript(string script, boolean top)

Parameters

Parameter

Description

script

JavaScript to be injected into this BBjHtmlView.

top

A value specifying whether this script is to be injected into the top level window of the page (BBj 17.0 and higher).

Return Value

None.

Remarks

None.

Example

Copy

BBjHtmlView::injectScript Example

rem ' BBjHtmlView::injectScript
bui = info(3,6)="5"
web = info(3,6)="6"
client = 0
if !bui and !web then
   client = msgbox("BBjHtmlView Client",7+32+512,"BBjHtmlView","Swing","JavaFX","Chromium")
   switch client
      case 1; rem ' Swing
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=FALSE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=FALSE",ERR=*NEXT)
         break
      case 2; rem ' JavaFX
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=FALSE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=TRUE",ERR=*NEXT)
         break
      case 3; rem ' Chromium
         print stbl("!OPTIONS","CHROMIUM_HTMLVIEW=TRUE",ERR=*NEXT)
         print stbl("!OPTIONS","JAVAFX_HTMLVIEW=TRUE",ERR=*NEXT)
         break
   swend
endif
sysgui = unt
open (sysgui)"X0"
sysgui! = bbjapi().getSysGui()
window! = sysgui!.addWindow(50,50,300,300,"injectScript",$00090003$)
window!.setCallback(window!.ON_CLOSE,"eoj")
gosub makehtml
htmlview! = window!.addHtmlView(101,25,25,250,150,html$)
htmlview!.setCallback(htmlview!.ON_PAGE_LOADED,"loaded")
htmlview!.setCallback(htmlview!.ON_SCRIPT_LOADED,"scriptloaded")
htmlview!.setCallback(htmlview!.ON_SCRIPT_FAILED,"scriptfailed")
executeScript! = window!.addButton(1,25,200,250,25,"executeScript")
executeScript!.setCallback(executeScript!.ON_BUTTON_PUSH,"executeScript")
js! = window!.addStaticText(100,25,250,250,25,"",$$)
print "getClientType: ",htmlview!.getClientType(err=*next)
print "getClientVersion: ",htmlview!.getClientVersion(err=*next)
process_events
eoj:
release
makehtml:
html$ = "<html><head></head>"
html$ = html$+"<body>"
html$ = html$+"<input type='button' onclick='testjs(""testing"")' value='Click Me!' style='margin:15px;'>"
html$ = html$+"<script type=""text/javascript"">"
html$ = html$+"function testjs(parameter1){alert(parameter1);}"
html$ = html$+"</script>"
html$ = html$+"</body></html>"
return
loaded:
print sysgui!.getLastEvent()
script$ = "function testjs(parameter1){alert(parameter1);}"
htmlview!.injectScript(script$,1)
if bui then htmlview!.injectScript(script$,0)
rem htmlview!.setText(html$)
htmlview!.clearCallback(htmlview!.ON_PAGE_LOADED)
return
scriptloaded:
event! = sysgui!.getLastEvent()
msg$ = date(0:"%Hz:%mz:%sz.%tz %p ")+event!.getEventName()+" url="+str(event!.getUrl())+",text="+str(event!.getText())
print msg$
i = msgbox(msg$)
gosub click; rem ' this works because we waited until the script was loaded
return
scriptfailed:
msg$ = date(0:"%Hz:%mz:%sz.%tz %p ")+event!.getEventName()+" url="+str(event!.getUrl())+",text="+str(event!.getText())
print msg$
i = msgbox(msg$)
return
executeScript:
r = rnd(255)
g = rnd(255)
b = rnd(255)
rgb$ = "#"+hta(chr(r))+hta(chr(g))+hta(chr(b))
js$ = "document.body.style.backgroundColor = """+rgb$+""""
if bui or web then js$ = "style.backgroundColor = """+rgb$+""""
print js$
js!.setText(js$)
System.err.println("getScriptValue="+str(htmlview!.getScriptValue()))
print "executeScript=",htmlview!.executeScript(js$)
System.err.println("getScriptValue="+str(htmlview!.getScriptValue()))
print htmlview!.executeScript("testjs('"+js$+"')")
return

See Also

BBjAPI

BBjSysGui

BBjControl

BBjWindow

See the BBj Object Diagram for an illustration of the relationship between BBj Objects.