Skip to content

Instantly share code, notes, and snippets.

@amaitland
Last active February 8, 2018 07:16
Show Gist options
  • Save amaitland/48adad9c7f2c57f6c17fad39b2747d00 to your computer and use it in GitHub Desktop.
Save amaitland/48adad9c7f2c57f6c17fad39b2747d00 to your computer and use it in GitHub Desktop.
Register event
CefSharp.RegisterEvent = async function (boundObjName, boundObjMethodName, targetObj, evtName)
{
await CefSharp.BindObjectAsync(boundObjName);
let addEvent = function ()
{
var isElement = targetObj instanceof HTMLElement;
var elem = isElement ? targetObj : document.getElementById(targetObj);
elem.addEventListener(evtName, function (e)
{
var evtArgs = { id: e.target.id, tagName: e.target.tagName };
window[boundObjName][boundObjMethodName](evtName, evtArgs);
});
};
switch (document.readyState)
{
case "loading":
{
// Document still loading
document.addEventListener("DOMContentLoaded", function (event)
{
addEvent();
});
break;
}
case "interactive":
case "complete":
{
addEvent();
break;
}
}
};
(function ()
{
var elem = document.getElementById('test-button');
elem.removeAttribute('disabled');
//boundEvent is the object name to be registered
//raiseEvent is the method on the bound object to be called (as a string)
//elem is the button
//click is the event that will be subscribed to.
CefSharp.RegisterEvent('boundEvent', 'raiseEvent', elem, 'click');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment