Created
          March 29, 2024 08:09 
        
      - 
      
- 
        Save M0nteCarl0/49012df6f3a0f3af4e67b760e14a8427 to your computer and use it in GitHub Desktop. 
    Cef Sharp json protocol snippet
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using Newtonsoft.Json; | |
| namespace TransportProto | |
| { | |
| public class JsonComand | |
| { | |
| public string Command { get; set; } | |
| public string Data { get; set; } | |
| } | |
| public class JsonComandTransportProto | |
| { | |
| static public JsonComand FromString(string json) | |
| { | |
| return JsonConvert.DeserializeObject<JsonComand>(json); | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | var src_path_array = [] | |
| var button_ids_array = [] | |
| var targetElements = document.querySelectorAll('.block-video.block'); | |
| for (var i = 0; i < targetElements.length; i++) { | |
| var downloadButton = document.createElement('button'); | |
| downloadButton.style.borderRadius = '10px'; | |
| downloadButton.style.padding = '10px 20px'; | |
| downloadButton.style.background = 'blue'; | |
| downloadButton.style.color = 'white'; | |
| downloadButton.innerText = 'Cкачать'; | |
| downloadButton.style.position = 'absolute'; | |
| downloadButton.style.right = '0'; | |
| downloadButton.style.top = '0'; | |
| var content_preview = targetElements[i].querySelector('video'); | |
| var srcValue = content_preview.getAttribute('src'); | |
| src_path_array.push(srcValue); | |
| button_ids_array.push(downloadButton.id); | |
| downloadButton.id = i.toString(); | |
| downloadButton.addEventListener('click', function () { | |
| var command = { | |
| "Command": "download", | |
| "Data": window.location.origin + src_path_array[Number(this.id)], | |
| }; | |
| var jsonString = JSON.stringify(command); | |
| CefSharp.PostMessage(jsonString); | |
| console.log(src_path_array[Number(this.id)]); | |
| }); | |
| targetElements[i].appendChild(downloadButton); | |
| } | |
| for(var i = 0; i < targetElements.length; i++) { | |
| var downloadButton = document.createElement('button'); | |
| downloadButton.style.borderRadius = '10px'; | |
| downloadButton.style.padding = '10px 20px'; | |
| downloadButton.style.background = 'green'; | |
| downloadButton.style.color = 'white'; | |
| downloadButton.innerText = 'Воспроизвести'; | |
| downloadButton.style.position = 'absolute'; | |
| downloadButton.style.left = '0'; | |
| downloadButton.style.top = '0'; | |
| var content_preview = targetElements[i].querySelector('video'); | |
| var srcValue = content_preview.getAttribute('src'); | |
| src_path_array.push(srcValue); | |
| button_ids_array.push(downloadButton.id); | |
| downloadButton.id = i.toString(); | |
| downloadButton.addEventListener('click', function () { | |
| var command = { | |
| "Command": "play", | |
| "Data": window.location.origin + src_path_array[Number(this.id)], | |
| }; | |
| var jsonString = JSON.stringify(command); | |
| CefSharp.PostMessage(jsonString); | |
| console.log(src_path_array[Number(this.id)]); | |
| }); | |
| targetElements[i].appendChild(downloadButton); | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | private void Browser_JavascriptMessageReceived(object sender, JavascriptMessageReceivedEventArgs e) | |
| { | |
| if (e.Message == null) { return; } | |
| var payload = (string)e.Message; | |
| try | |
| { | |
| JsonComand cmd = JsonComandTransportProto.FromString(payload); | |
| if (cmd == null) { return; } | |
| if (cmd.Command == "download") | |
| { | |
| Task task = Task.Run(async () => | |
| { | |
| browser_view.StartDownload(cmd.Data); | |
| }); | |
| } | |
| if (cmd.Command == "play") | |
| { | |
| string m = cmd.Data; | |
| string path_bin = "BMVLPlayer.exe"; | |
| string arguments = "-m=" + m; | |
| ProcessStartInfo Player = new ProcessStartInfo { FileName = path_bin, Arguments = arguments }; | |
| try | |
| { | |
| Process.Start(Player); | |
| } | |
| catch(Exception ex) | |
| { | |
| logger.Error(ex.Message); | |
| } | |
| } | |
| } | |
| catch (Exception ex){ logger.Error(ex.Message); } | |
| } | |
| browser_view.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true; | |
| browser_view.JavascriptMessageReceived += Browser_JavascriptMessageReceived; | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment