Skip to content

Commit

Permalink
Send events to all frames
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro authored and WizardCM committed Jul 16, 2023
1 parent 5d74047 commit 5292ff3
Showing 1 changed file with 38 additions and 28 deletions.
66 changes: 38 additions & 28 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,25 @@ void BrowserApp::ExecuteJSFunction(CefRefPtr<CefBrowser> browser,
const char *functionName,
CefV8ValueList arguments)
{
CefRefPtr<CefV8Context> context =
browser->GetMainFrame()->GetV8Context();
std::vector<CefString> names;
browser->GetFrameNames(names);
for (auto &name : names) {
CefRefPtr<CefFrame> frame = browser->GetFrame(name);
CefRefPtr<CefV8Context> context = frame->GetV8Context();

context->Enter();
context->Enter();

CefRefPtr<CefV8Value> globalObj = context->GetGlobal();
CefRefPtr<CefV8Value> obsStudioObj = globalObj->GetValue("obsstudio");
CefRefPtr<CefV8Value> jsFunction = obsStudioObj->GetValue(functionName);
CefRefPtr<CefV8Value> globalObj = context->GetGlobal();
CefRefPtr<CefV8Value> obsStudioObj =
globalObj->GetValue("obsstudio");
CefRefPtr<CefV8Value> jsFunction =
obsStudioObj->GetValue(functionName);

if (jsFunction && jsFunction->IsFunction())
jsFunction->ExecuteFunction(nullptr, arguments);
if (jsFunction && jsFunction->IsFunction())
jsFunction->ExecuteFunction(nullptr, arguments);

context->Exit();
context->Exit();
}
}

#if !ENABLE_WASHIDDEN
Expand Down Expand Up @@ -270,13 +276,6 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
ExecuteJSFunction(browser, "onActiveChange", arguments);

} else if (message->GetName() == "DispatchJSEvent") {
CefRefPtr<CefV8Context> context =
browser->GetMainFrame()->GetV8Context();

context->Enter();

CefRefPtr<CefV8Value> globalObj = context->GetGlobal();

nlohmann::json payloadJson = nlohmann::json::parse(
args->GetString(1).ToString(), nullptr, false);

Expand All @@ -292,22 +291,33 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
script += wrapperJsonString;
script += ");";

CefRefPtr<CefV8Value> returnValue;
CefRefPtr<CefV8Exception> exception;
std::vector<CefString> names;
browser->GetFrameNames(names);
for (auto &name : names) {
CefRefPtr<CefFrame> frame = browser->GetFrame(name);
CefRefPtr<CefV8Context> context = frame->GetV8Context();

/* Create the CustomEvent object
* We have to use eval to invoke the new operator */
context->Eval(script, browser->GetMainFrame()->GetURL(), 0,
returnValue, exception);
context->Enter();

CefV8ValueList arguments;
arguments.push_back(returnValue);
CefRefPtr<CefV8Value> globalObj = context->GetGlobal();

CefRefPtr<CefV8Value> dispatchEvent =
globalObj->GetValue("dispatchEvent");
dispatchEvent->ExecuteFunction(nullptr, arguments);
CefRefPtr<CefV8Value> returnValue;
CefRefPtr<CefV8Exception> exception;

context->Exit();
/* Create the CustomEvent object
* We have to use eval to invoke the new operator */
context->Eval(script, browser->GetMainFrame()->GetURL(),
0, returnValue, exception);

CefV8ValueList arguments;
arguments.push_back(returnValue);

CefRefPtr<CefV8Value> dispatchEvent =
globalObj->GetValue("dispatchEvent");
dispatchEvent->ExecuteFunction(nullptr, arguments);

context->Exit();
}

} else if (message->GetName() == "executeCallback") {
CefRefPtr<CefV8Context> context =
Expand Down

0 comments on commit 5292ff3

Please sign in to comment.