How Can I communicate with Chrome(Chrome Extension) using C#?

后端 未结 3 1902
情深已故
情深已故 2021-01-31 23:29

I want to create a bridge which can communicate between my C# application and extension.
Here is the explanation of what I actually want: I have created an extension which w

相关标签:
3条回答
  • 2021-02-01 00:07

    Most of google chrome extensions are written in HTML, CSS or Javascript. Then, bundling your extension directory into a .crx Chrome Extension file is as simple as running a short Python script. But, C# could be translated into the Javascript, you can write your own business logic for your extensions. Look at Script#

    0 讨论(0)
  • 2021-02-01 00:09

    You might be able to do this if you start chrome in debug mode for RemoteDebugging and then you can connect to it via the specified Port, but it's a bit of a hassle.

    You could try to investigate how Adobe Brackets does it but i have a feeling it's the same method.

    0 讨论(0)
  • 2021-02-01 00:30

    Hm...there's probably better ways, but an easy way might be opening an HttpListener in your c# app and communicate with it from the extension like this:

    var listener = "http://localhost:60024/";
    
    function getCommand(){
    
        var postData = { 
            "action": "getCommand" 
        };
    
        $.post( listener, postData, function(response){
            //Parse response and do whatever c# wants
        });
    }
    
    function send(data){
    
        var postData = {
            "action" : "send",
            "data": data
        };
    
        $.post(listener, postData);
    }
    
    
    setInterval(getCommand, 1000);
    

    In the example i'm using jQuery.post, which can be added to the extension context, but you could use XMLHttpRequest if you like it better. And on the c# side:

    using System;
    using System.Net;
    
    
    namespace HttpListenerTEst
    {
        class Program
        {
            private static HttpListener _listener;
    
            static void Main(string[] args)
            {
                _listener = new HttpListener();
                _listener.Prefixes.Add("http://localhost:60024/");
                _listener.Start();
                _listener.BeginGetContext(new AsyncCallback(Program.ProcessRequest), null);
    
                Console.ReadLine();
            }
    
            static void ProcessRequest(IAsyncResult result)
            {
                HttpListenerContext context = _listener.EndGetContext(result);
                HttpListenerRequest request = context.Request;
    
                //Answer getCommand/get post data/do whatever
    
                _listener.BeginGetContext(new AsyncCallback(Program.ProcessRequest), null);
            }
        }
    }
    

    In the ProcessRequest function you can read post data or send something back.

    Get post data:

            string postData;
            using (var reader = new StreamReader(request.InputStream, request.ContentEncoding))
            {
                postData = reader.ReadToEnd();
                //use your favourite json parser here
            }
    

    and send back some stuff with:

            string responseString           = "This could be json to be parsed by the extension";
    
            HttpListenerResponse response   = context.Response;
            response.ContentType            = "text/html";
    
            byte[] buffer                   = System.Text.Encoding.UTF8.GetBytes(responseString);
            response.ContentLength64        = buffer.Length;
            Stream output                   = response.OutputStream;
    
            output.Write(buffer, 0, buffer.Length);
            output.Close();
    

    Just some quick brainstorming, looking forward to better ideas :)

    0 讨论(0)
提交回复
热议问题