Page 1 of 1

PB 6.30B2 SetGadgetItemText has length limit with WebViewGadget

Posted: Fri Sep 26, 2025 4:39 am
by Seymour Clufley
There seems to be a length limit, so that trying to load a very long HTML string doesn't work. You can tell this because the HTML onload event (an alert box) doesn't occur.

This bug can be worked around by saving the HTML to file and loading that in the gadget using SetGadgetText, but still.

This bug was also present with PB6.2, and probably before that.

Code: Select all

Macro ImageAsPNGDataURI(img,var,pngdepth)
  UsePNGImageEncoder()
  *mem = EncodeImage(img,#PB_ImagePlugin_PNG,0,pngdepth)
  var = "data:image/png;base64,"+Base64Encoder(*mem,MemorySize(*mem))
  FreeMemory(*mem)
EndMacro


Procedure.s WVG_LoadHTML(html.s)
  
  Static win.i
  Static wg.i
  
  If wg=0
    ww=1600 : wh=1200
    win = OpenWindow(#PB_Any,0,0,ww,wh, "WebViewGadget")
    wg = WebViewGadget(#PB_Any,0,0,ww,wh)
  EndIf
  
  SetWindowTitle(win,"HTML length: "+Str(Len(html)))
  SetGadgetItemText(wg,#PB_WebView_HtmlCode,html)
  
EndProcedure




; this will work
h.s = "<body onload="+Chr(34)+"alert('loaded 1')"+Chr(34)+"></body>"
WVG_LoadHTML(h)
For a = 1 To 100 : WaitWindowEvent(50) : Next a



; this will not work
iw = 1200
ih = 1000
img.i = CreateImage(#PB_Any,iw,ih,32)
StartDrawing(ImageOutput(img))
DrawingMode(#PB_2DDrawing_AlphaBlend)
For a = 1 To 1000
  bx = Random(iw)
  by = Random(ih)
  bw = Random(50,10)
  bh = Random(50,10)
  Box(bx,by,bw,bh,RGBA(Random(255),Random(255),Random(255),Random(255)))
Next a
For x = 0 To iw-1
  For y = 0 To ih-1
    Plot(x,y,RGBA(0,0,0,Random(128)))
  Next y
Next x
StopDrawing()
ImageAsPNGDataURI(img,du.s,32)
FreeImage(img)

h.s = "<body onload="+Chr(34)+"alert('loaded 2')"+Chr(34)+"><img src='"+du+"' /></body>"
du="" ; free memory
WVG_LoadHTML(h)

Repeat
  WaitWindowEvent(50)
Until GetAsyncKeyState_(#VK_ESCAPE)

Re: PB 6.30B2 SetGadgetItemText has length limit with WebViewGadget

Posted: Fri Sep 26, 2025 9:24 am
by Fred
You're right, it's a limitation of the WebView2 NavigateToString() function as you can see here: https://learn.microsoft.com/en-us/dotne ... .0.3405.78

"The htmlContent parameter may not be larger than 2 MB (2 * 1024 * 1024 bytes) in total size. The origin of the new page is about:blank."

As the content is converted to wide string, it's more than 2MB and fail. I will add a debugger check to limit the string size to 1000000 chars, it should be fine.

The recommanded way for big page is to write it on disk first.