<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How to communicate with JavaScript in the web page in PaletteSet (c # calls or sends data to javascript) in .NET Forum</title>
    <link>https://forums.autodesk.com/t5/net-forum/how-to-communicate-with-javascript-in-the-web-page-in-paletteset/m-p/11476517#M11423</link>
    <description>&lt;P&gt;&lt;SPAN&gt;What APIs can control the webview opened in PaletteSet?&lt;BR /&gt;&lt;SPAN class=""&gt;Is there any API in C# that can control this webview?&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;For example, the DOM and JavaScript that control it, like Microsoft's webview2, c# can control the webview2 itself and communicate with the JavaScript running in it.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the command to display PaletteSet, and show a web browser ( is it chrome? ) ：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod( "showWeb" )]
public void showWeb() {
  var ps = new PaletteSet( "pages" ) {
    { "page1", new Uri( "...." ) }
  };
  ps.Visible = true;
  ps.Size = new System.Drawing.Size( 1200, 600 );
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the command called for JavaScript：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[ JavaScriptCallback( "helloWorld" )]
public string helloWorld( string dataFromJavaScript ) {
  var serializer = new JavaScriptSerializer();
  return serializer.Serialize( new Hashtable() {
        { "retCode", 0 },
        { "retValue", "data from c#" }
      });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the code for JavaScript to call c#（ The execAsync function comes from the global variable (window) in the browser in PaletteSet.）：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function callNET( fnName, fnParams ) {
  const data = JSON.stringify({
    functionName: fnName,
    invokeAsCommand: false,
    functionParams: fnParams
  })
  
  return new Promise( (resolve, reject) =&amp;gt; {
    execAsync(
      data,
      json =&amp;gt; {
        const { retCode, retValue } = JSON.parse( json )
        retCode === 0 ? resolve( retValue ) : reject( "err", retCode )
      },
      err =&amp;gt; reject( err )
    )
  })
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;There is some JavaScript code in my web page. I know that the JavaScript in the web page can call the method of c # through AutoCAD JavaScript APIs, and can send string data to the c#, and then c# can return the data to JavaScript.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;It's normal when JavaScript calls c# actively, but I don't know how c# actively calls the JavaScript function in the web page, or how c# actively sends data to JavaScript, just like websocket, the server actively sends data to browser clients.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;I don't want to use a timer to poll c# in JavaScript to get data. I want to know how c# actively sends data to JavaScript in the browser in PaletteSet.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;[ The subject line of this post has been edited to include the product name by &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12851358"&gt;@handjonathan&lt;/a&gt; ]&lt;/FONT&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 12 Oct 2022 02:44:15 GMT</pubDate>
    <dc:creator>906521597</dc:creator>
    <dc:date>2022-10-12T02:44:15Z</dc:date>
    <item>
      <title>How to communicate with JavaScript in the web page in PaletteSet (c # calls or sends data to javascript)</title>
      <link>https://forums.autodesk.com/t5/net-forum/how-to-communicate-with-javascript-in-the-web-page-in-paletteset/m-p/11476517#M11423</link>
      <description>&lt;P&gt;&lt;SPAN&gt;What APIs can control the webview opened in PaletteSet?&lt;BR /&gt;&lt;SPAN class=""&gt;Is there any API in C# that can control this webview?&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class=""&gt;For example, the DOM and JavaScript that control it, like Microsoft's webview2, c# can control the webview2 itself and communicate with the JavaScript running in it.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the command to display PaletteSet, and show a web browser ( is it chrome? ) ：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[CommandMethod( "showWeb" )]
public void showWeb() {
  var ps = new PaletteSet( "pages" ) {
    { "page1", new Uri( "...." ) }
  };
  ps.Visible = true;
  ps.Size = new System.Drawing.Size( 1200, 600 );
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the command called for JavaScript：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;[ JavaScriptCallback( "helloWorld" )]
public string helloWorld( string dataFromJavaScript ) {
  var serializer = new JavaScriptSerializer();
  return serializer.Serialize( new Hashtable() {
        { "retCode", 0 },
        { "retValue", "data from c#" }
      });
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;This is the code for JavaScript to call c#（ The execAsync function comes from the global variable (window) in the browser in PaletteSet.）：&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;function callNET( fnName, fnParams ) {
  const data = JSON.stringify({
    functionName: fnName,
    invokeAsCommand: false,
    functionParams: fnParams
  })
  
  return new Promise( (resolve, reject) =&amp;gt; {
    execAsync(
      data,
      json =&amp;gt; {
        const { retCode, retValue } = JSON.parse( json )
        retCode === 0 ? resolve( retValue ) : reject( "err", retCode )
      },
      err =&amp;gt; reject( err )
    )
  })
}&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class=""&gt;There is some JavaScript code in my web page. I know that the JavaScript in the web page can call the method of c # through AutoCAD JavaScript APIs, and can send string data to the c#, and then c# can return the data to JavaScript.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;It's normal when JavaScript calls c# actively, but I don't know how c# actively calls the JavaScript function in the web page, or how c# actively sends data to JavaScript, just like websocket, the server actively sends data to browser clients.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;I don't want to use a timer to poll c# in JavaScript to get data. I want to know how c# actively sends data to JavaScript in the browser in PaletteSet.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#808080"&gt;[ The subject line of this post has been edited to include the product name by &lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/12851358"&gt;@handjonathan&lt;/a&gt; ]&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 12 Oct 2022 02:44:15 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/net-forum/how-to-communicate-with-javascript-in-the-web-page-in-paletteset/m-p/11476517#M11423</guid>
      <dc:creator>906521597</dc:creator>
      <dc:date>2022-10-12T02:44:15Z</dc:date>
    </item>
  </channel>
</rss>

