1. global $
  2.  
  3. global g_wx
  4.  
  5. ----------------------------------------
  6. --
  7. ----------------------------------------
  8. on startMovie
  9.  
  10.     $.import("json")
  11.  
  12.     -- window settings
  13.     _movie.stage.title = "WebView Bind Demo"
  14.     _movie.stage.titlebarOptions.visible = TRUE
  15.     _movie.stage.rect = rect(0, 0, 400, 300)
  16.     _movie.stage.bgcolor = rgb("#121212")
  17.     _movie.stage.resizable = 1
  18.     _movie.centerStage = 1
  19.     _movie.stage.visible = 1
  20.  
  21.     g_wx = xtra("WebView").new()
  22.     g_wx.useDarkMode(TRUE)
  23.     g_wx.webviewCreate(TRUE)
  24.     g_wx.webviewSetAutoSize()
  25.     g_wx.webviewSetHtml("<html><head><meta name='color-scheme' content='dark'></head><body style='margin:20px''>" &\
  26.     "<p>" &\
  27.     "  <label for='inp'>Lingo expression:</label>" &\
  28.     "  <input id='inp' value='23 * 7 - 42' style='width: 100%'>" &\
  29.     "</p>" &\
  30.     "<p>" &\
  31.     "  <label for='res'>Result:</label>" &\
  32.     "  <input id='res' style='width: 100%'>" &\
  33.     "</p>" &\
  34.     "<p><button id='btn'>Evaluate in Lingo - value(...)</button></p>" &\
  35.     "<hr>" &\
  36.     "<p>" &\
  37.     "  <label for='cmd'>Lingo command:</label>" &\
  38.     "  <input id='cmd' value='alert(_movie.path)' style='width: 100%'>" &\
  39.     "</p>" &\
  40.     "<p><button id='btn2'>Execute by Lingo - do(...)</button></p>" &\
  41.     "<script>" &\
  42.     "document.getElementById('btn').addEventListener('click', async () => {" &\
  43.     "    document.getElementById('res').value = await __lingo_eval__(document.getElementById('inp').value);" &\
  44.     "});" &\
  45.     "document.getElementById('btn2').addEventListener('click', async () => {" &\
  46.     "    __lingo_do__(document.getElementById('cmd').value);" &\
  47.     "});" &\
  48.     "</script></body></html>")
  49.  
  50.     g_wx.webviewJSBind("__lingo_eval__")
  51.     g_wx.webviewJSBind("__lingo_do__")
  52.  
  53.     -- This demonstrates how to replace the default browser context menu with a custom menu
  54.     g_wx.webviewJSBind("__show_menu__")
  55.     g_wx.webviewJSEval("document.addEventListener('contextmenu', (e) => {e.preventDefault();__show_menu__();})")
  56. end
  57.  
  58. ----------------------------------------
  59. -- This is required when running the movie in Director, i.e. in the authoring IDE
  60. ----------------------------------------
  61. --on stopMovie
  62. --  if not voidP(g_wx) then
  63. --      g_wx.forget()
  64. --      g_wx = VOID
  65. --  end if
  66. --end
  67.  
  68. ----------------------------------------
  69. --
  70. ----------------------------------------
  71. on __show_menu__
  72.     hMenu = g_wx.createPopupMenu()
  73.     g_wx.insertMenuItem(hMenu, -1, "Show Message Window", 1)
  74.     g_wx.insertSeparator(hMenu, -1)
  75.     g_wx.insertMenuItem(hMenu, -1, "Quit", 2)
  76.     cmdID = g_wx.showPopupMenu(hMenu)
  77.     g_wx.destroyMenu(hMenu)
  78.     case cmdID of
  79.         1: _player.debugPlaybackEnabled = 1
  80.         2: _player.quit()
  81.     end case
  82. end
  83.  
  84. ----------------------------------------
  85. --
  86. ----------------------------------------
  87. on __lingo_eval__ (args)
  88.     expression = value(args)[1]
  89.     res = value(expression)
  90.  
  91.     -- This only works if result is a number
  92.     -- return string(res)
  93.  
  94.     -- This also works if result is something else, e.g. a string
  95.     return $.json.encode(res)
  96. end
  97.  
  98. ----------------------------------------
  99. --
  100. ----------------------------------------
  101. on __lingo_do__ (args)
  102.     cmd = value(args)[1]
  103.     do(cmd)
  104. end
  105.  
[raw code]