Skip to content

Commit 6f1d458

Browse files
committed
Add active/inactive JS callback
1 parent d252aee commit 6f1d458

8 files changed

+49
-1
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ window.obsstudio.onVisibilityChange = function(visiblity) {
2424
};
2525
```
2626

27+
### Register for active/inactive callbacks
28+
```
29+
/**
30+
* onActiveChange gets callbacks when the active/inactive state of the browser source changes in OBS
31+
*
32+
* @param {bool} True -> active, False -> inactive
33+
*/
34+
window.obsstudio.onActiveChange = function(active) {
35+
36+
};
37+
```
38+
2739
### Register for scene change callbacks
2840
```
2941
window.addEventListener('obsSceneChanged', function(evt) {

obs-browser/browser-manager-base.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,11 @@ void BrowserManager::ExecuteVisiblityJSCallback(int browserIdentifier, bool visi
8181
pimpl->ExecuteVisiblityJSCallback(browserIdentifier, visible);
8282
}
8383

84+
void BrowserManager::ExecuteActiveJSCallback(int browserIdentifier, bool active)
85+
{
86+
pimpl->ExecuteActiveJSCallback(browserIdentifier, active);
87+
}
88+
8489
void BrowserManager::ExecuteSceneChangeJSCallback(const char *name)
8590
{
8691
pimpl->ExecuteSceneChangeJSCallback(name);
@@ -336,6 +341,17 @@ void BrowserManager::Impl::ExecuteVisiblityJSCallback(int browserIdentifier, boo
336341
});
337342
}
338343

344+
void BrowserManager::Impl::ExecuteActiveJSCallback(int browserIdentifier, bool active)
345+
{
346+
ExecuteOnBrowser(browserIdentifier, [&](CefRefPtr<CefBrowser> b)
347+
{
348+
CefRefPtr<CefProcessMessage> msg = CefProcessMessage::Create("Active");
349+
CefRefPtr<CefListValue> args = msg->GetArgumentList();
350+
args->SetBool(0, active);
351+
b->SendProcessMessage(PID_RENDERER, msg);
352+
});
353+
}
354+
339355
void BrowserManager::Impl::ExecuteSceneChangeJSCallback(const char *name)
340356
{
341357
ExecuteOnAllBrowsers([&](CefRefPtr<CefBrowser> b)

obs-browser/browser-manager-base.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ class BrowserManager::Impl
5656

5757
void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible);
5858

59+
void ExecuteActiveJSCallback(int browserIdentifier, bool active);
60+
5961
void ExecuteSceneChangeJSCallback(const char *name);
6062

6163
void RefreshPageNoCache(int browserIdentifier);

obs-browser/main-source.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,15 @@ static void browser_source_activate(void *data)
154154

155155
if (restart)
156156
BrowserManager::Instance()->RefreshPageNoCache(bs->GetBrowserIdentifier());
157+
158+
bs->ExecuteActiveJSCallback(true);
157159
}
158160

159161
static void browser_source_deactivate(void *data)
160162
{
163+
BrowserSource *bs = static_cast<BrowserSource *>(data);
161164

165+
bs->ExecuteActiveJSCallback(false);
162166
}
163167

164168
// Called when the source is visible

shared/browser-app.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
110110
ExecuteJSFunction(browser, "onVisibilityChange", arguments);
111111
return true;
112112
}
113+
else if (message->GetName() == "Active") {
114+
CefV8ValueList arguments;
115+
arguments.push_back(CefV8Value::CreateBool(args->GetBool(0)));
116+
117+
ExecuteJSFunction(browser, "onActiveChange", arguments);
118+
return true;
119+
}
113120
else if (message->GetName() == "DispatchJSEvent") {
114121
CefRefPtr<CefV8Context> context = browser->GetMainFrame()->GetV8Context();
115122

@@ -161,7 +168,6 @@ bool BrowserApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser,
161168
CefString jsonString = message->GetArgumentList()->GetString(1);
162169

163170
CefRefPtr<CefV8Value> callback = callbackMap[callbackID];
164-
165171
CefV8ValueList args;
166172
args.push_back(CefV8Value::CreateString(jsonString));
167173

shared/browser-manager.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class BrowserManager {
6161
const char *GetModulePath() { return path; }
6262

6363
void ExecuteVisiblityJSCallback(int browserIdentifier, bool visible);
64+
65+
void ExecuteActiveJSCallback(int browserIdentifier, bool active);
6466

6567
void ExecuteSceneChangeJSCallback(const char *name);
6668

shared/browser-source.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,8 @@ void BrowserSource::ExecuteVisiblityJSCallback(bool visible)
9494
{
9595
BrowserManager::Instance()->ExecuteVisiblityJSCallback(browserIdentifier, visible);
9696
}
97+
98+
void BrowserSource::ExecuteActiveJSCallback(bool active)
99+
{
100+
BrowserManager::Instance()->ExecuteActiveJSCallback(browserIdentifier, active);
101+
}

shared/browser-source.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class BrowserSource {
6969
std::shared_ptr<BrowserListener> CreateListener();
7070

7171
void ExecuteVisiblityJSCallback(bool visible);
72+
void ExecuteActiveJSCallback(bool active);
7273

7374
private:
7475
class Impl;

0 commit comments

Comments
 (0)