Sign in to follow this  
echoHU

Execute JS function with CEF

Recommended Posts

Hello!

I've got a this tag under my html, containing this function:

<script>
	function showNotification(type, text) {
		var image = document.getElementById("logo");
		
		switch(type) {
			case 1:
				image.src = "images/warning.png";
				document.getElementById("header").innerHTML = "Warning";
				break;
			case 2:
				image.src = "images/error.png";
				document.getElementById("header").innerHTML = "Error";
				break;
			default:
				image.src = "images/info.png";
				document.getElementById("header").innerHTML = "Information";
				break;
		}
		
		var content = document.getElementById("content")
		content.innerHTML = text;
	}
</script>

And my Questin is: how to execute this function under CEF Browser?

I have tried something like this, but it's not working:

var boxBrowser = mp.browsers.new('package://PleasantRoleplay/notification-system/notification.html');
boxBrowser.execute('hideWindow();');

mp.events.add('boxShow', (type, ntext) => {
	browser.execute('showNotification(' + type + ',' + ntext + ');');
	
	boxBrowser.execute('showWindow();');
	
	setTimeout(function(){
		boxBrowser.destroy();
	}, 5000); 
});

 

Share this post


Link to post
Share on other sites

@echoHU for simplicity and scalability, ditch execute and use this:

For the browser your code would be:

rpc.register('showNotification', ([type, text]) => {
  var image = document.getElementById("logo");

  switch(type) {
    case 1:
      image.src = "images/warning.png";
      document.getElementById("header").innerHTML = "Warning";
      break;
    case 2:
      image.src = "images/error.png";
      document.getElementById("header").innerHTML = "Error";
      break;
    default:
      image.src = "images/info.png";
      document.getElementById("header").innerHTML = "Information";
      break;
  }

  var content = document.getElementById("content")
  content.innerHTML = text;
});

And for the client:

const rpc = require('rage-rpc');

rpc.callBrowser(boxBrowser, 'showNotification', ["this is the type", "this is the text"]);

// OR if you dont have access to the browser object:

rpc.callBrowsers('showNotification', ["this is the type", "this is the text"]);

This eliminates the need for messy execute and gives you the ability to call your CEF functions from anywhere, even the server or other CEF instances.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.