Skip to content

Commit eaa33ba

Browse files
committed
Merge pull request #7 from OsirisNL/css-support
Add support for custom CSS
2 parents 060957c + bfe026d commit eaa33ba

12 files changed

+251
-5
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@ set(obs-browser_SOURCES
2929
obs-browser/main.cpp
3030
obs-browser/main-source.cpp
3131
shared/browser-source.cpp
32+
shared/base64.cpp
33+
obs-browser/browser-load-handler.cpp
3234
)
3335

3436
set(obs-browser_HEADERS
3537
shared/browser-manager.hpp
3638
shared/browser-source.hpp
3739
shared/browser-listener.hpp
3840
shared/browser-settings.hpp
39-
shared/browser-types.h)
41+
shared/browser-types.h
42+
shared/base64.hpp
43+
obs-browser/browser-load-handler.hpp)
4044

4145
if (APPLE)
4246
list(APPEND obs-browser_SOURCES
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/******************************************************************************
2+
Copyright (C) 2014 by John R. Bradley <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
#include "browser-load-handler.hpp"
19+
#include "browser-listener.hpp"
20+
#include "base64.hpp"
21+
22+
BrowserLoadHandler::BrowserLoadHandler(const std::string css)
23+
: css(css)
24+
{
25+
}
26+
27+
BrowserLoadHandler::~BrowserLoadHandler()
28+
{
29+
30+
}
31+
32+
void BrowserLoadHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode)
33+
{
34+
if (frame->IsMain())
35+
{
36+
std::string base64EncodedCSS = base64_encode(reinterpret_cast<const unsigned char*>(css.c_str()),css.length());
37+
std::string href = "data:text/css;charset=utf-8;base64," + base64EncodedCSS;
38+
39+
std::string script = "";
40+
script += "var link = document.createElement('link');";
41+
script += "link.setAttribute('rel', 'stylesheet');";
42+
script += "link.setAttribute('type', 'text/css');";
43+
script += "link.setAttribute('href', '" + href + "');";
44+
script += "document.getElementsByTagName('head')[0].appendChild(link);";
45+
46+
frame->ExecuteJavaScript(script, href, 0);
47+
}
48+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/******************************************************************************
2+
Copyright (C) 2014 by John R. Bradley <[email protected]>
3+
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 2 of the License, or
7+
(at your option) any later version.
8+
9+
This program is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU General Public License for more details.
13+
14+
You should have received a copy of the GNU General Public License
15+
along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
******************************************************************************/
17+
18+
#pragma once
19+
20+
#include <vector>
21+
22+
#include <include/cef_load_handler.h>
23+
24+
#include <obs-module.h>
25+
26+
class BrowserListener;
27+
28+
class BrowserLoadHandler : public CefLoadHandler
29+
{
30+
public:
31+
32+
BrowserLoadHandler(const std::string css);
33+
34+
~BrowserLoadHandler();
35+
36+
public: /* CefLoadHandler overrides */
37+
38+
virtual void BrowserLoadHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) OVERRIDE;
39+
40+
private:
41+
const std::string css;
42+
43+
private:
44+
IMPLEMENT_REFCOUNTING(BrowserLoadHandler);
45+
46+
};

obs-browser/browser-manager-base.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "browser-settings.hpp"
77
#include "browser-client.hpp"
88
#include "browser-render-handler.hpp"
9+
#include "browser-load-handler.hpp"
910

1011
BrowserManager::BrowserManager()
1112
: pimpl(new BrowserManager::Impl())
@@ -101,8 +102,11 @@ int BrowserManager::Impl::CreateBrowser(
101102
new BrowserRenderHandler(browserSettings.width,
102103
browserSettings.height, browserListener));
103104

105+
CefRefPtr<BrowserLoadHandler> loadHandler(
106+
new BrowserLoadHandler(browserSettings.css));
107+
104108
CefRefPtr<BrowserClient> browserClient(
105-
new BrowserClient(renderHandler));
109+
new BrowserClient(renderHandler,loadHandler));
106110

107111
CefWindowInfo windowInfo;
108112
windowInfo.transparent_painting_enabled = true;

obs-browser/main-source.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ static obs_properties_t *browser_source_get_properties(void *)
7474
obs_module_text("Height"), 1, 4096, 1);
7575
obs_properties_add_int(props, "fps",
7676
obs_module_text("FPS"), 1, 60, 1);
77+
obs_properties_add_text(props, "css",
78+
obs_module_text("CSS"), OBS_TEXT_MULTILINE);
7779
#ifdef __APPLE__
7880
// osx is the only process-isolated cef impl
7981
obs_properties_add_button(props, "restart",

shared/base64.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
base64.cpp and base64.h
3+
4+
Copyright (C) 2004-2008 René Nyffenegger
5+
6+
This source code is provided 'as-is', without any express or implied
7+
warranty. In no event will the author be held liable for any damages
8+
arising from the use of this software.
9+
10+
Permission is granted to anyone to use this software for any purpose,
11+
including commercial applications, and to alter it and redistribute it
12+
freely, subject to the following restrictions:
13+
14+
1. The origin of this source code must not be misrepresented; you must not
15+
claim that you wrote the original source code. If you use this source code
16+
in a product, an acknowledgment in the product documentation would be
17+
appreciated but is not required.
18+
19+
2. Altered source versions must be plainly marked as such, and must not be
20+
misrepresented as being the original source code.
21+
22+
3. This notice may not be removed or altered from any source distribution.
23+
24+
René Nyffenegger [email protected]
25+
26+
*/
27+
28+
#include "base64.hpp"
29+
#include <iostream>
30+
31+
static const std::string base64_chars =
32+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
33+
"abcdefghijklmnopqrstuvwxyz"
34+
"0123456789+/";
35+
36+
37+
static inline bool is_base64(unsigned char c) {
38+
return (isalnum(c) || (c == '+') || (c == '/'));
39+
}
40+
41+
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
42+
std::string ret;
43+
int i = 0;
44+
int j = 0;
45+
unsigned char char_array_3[3];
46+
unsigned char char_array_4[4];
47+
48+
while (in_len--) {
49+
char_array_3[i++] = *(bytes_to_encode++);
50+
if (i == 3) {
51+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
52+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
53+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
54+
char_array_4[3] = char_array_3[2] & 0x3f;
55+
56+
for (i = 0; (i <4); i++)
57+
ret += base64_chars[char_array_4[i]];
58+
i = 0;
59+
}
60+
}
61+
62+
if (i)
63+
{
64+
for (j = i; j < 3; j++)
65+
char_array_3[j] = '\0';
66+
67+
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
68+
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
69+
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
70+
char_array_4[3] = char_array_3[2] & 0x3f;
71+
72+
for (j = 0; (j < i + 1); j++)
73+
ret += base64_chars[char_array_4[j]];
74+
75+
while ((i++ < 3))
76+
ret += '=';
77+
78+
}
79+
80+
return ret;
81+
82+
}
83+
84+
std::string base64_decode(std::string const& encoded_string) {
85+
int in_len = encoded_string.size();
86+
int i = 0;
87+
int j = 0;
88+
int in_ = 0;
89+
unsigned char char_array_4[4], char_array_3[3];
90+
std::string ret;
91+
92+
while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
93+
char_array_4[i++] = encoded_string[in_]; in_++;
94+
if (i == 4) {
95+
for (i = 0; i <4; i++)
96+
char_array_4[i] = base64_chars.find(char_array_4[i]);
97+
98+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
99+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
100+
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
101+
102+
for (i = 0; (i < 3); i++)
103+
ret += char_array_3[i];
104+
i = 0;
105+
}
106+
}
107+
108+
if (i) {
109+
for (j = i; j <4; j++)
110+
char_array_4[j] = 0;
111+
112+
for (j = 0; j <4; j++)
113+
char_array_4[j] = base64_chars.find(char_array_4[j]);
114+
115+
char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
116+
char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
117+
char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
118+
119+
for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
120+
}
121+
122+
return ret;
123+
}

shared/base64.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#include <string>
2+
3+
std::string base64_encode(unsigned char const*, unsigned int len);
4+
std::string base64_decode(std::string const& s);

shared/browser-client.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
#include "browser-client.hpp"
2121

22-
BrowserClient::BrowserClient(CefRenderHandler *renderHandler)
23-
: renderHandler(renderHandler)
22+
BrowserClient::BrowserClient(CefRenderHandler *renderHandler,
23+
CefLoadHandler *loadHandler)
24+
: renderHandler(renderHandler), loadHandler(loadHandler)
2425
{
2526
}
2627

@@ -29,6 +30,11 @@ CefRefPtr<CefRenderHandler> BrowserClient::GetRenderHandler()
2930
return renderHandler;
3031
}
3132

33+
CefRefPtr<CefLoadHandler> BrowserClient::GetLoadHandler()
34+
{
35+
return loadHandler;
36+
}
37+
3238
CefRefPtr<CefLifeSpanHandler> BrowserClient::GetLifeSpanHandler()
3339
{
3440
return this;

shared/browser-client.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,21 @@
2121

2222
class BrowserSource;
2323
class BrowserRenderHandler;
24+
class BrowserLoadHandler;
2425

2526
class BrowserClient : public CefClient, public CefLifeSpanHandler,
2627
public CefContextMenuHandler
2728
{
2829
public:
29-
BrowserClient(CefRenderHandler *renderHandler);
30+
BrowserClient(CefRenderHandler *renderHandler,
31+
CefLoadHandler *loadHandler);
3032

3133
public: /* CefClient overrides */
3234
virtual CefRefPtr<CefRenderHandler> GetRenderHandler() OVERRIDE;
3335
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() OVERRIDE;
3436
virtual CefRefPtr<CefContextMenuHandler> GetContextMenuHandler()
3537
OVERRIDE;
38+
virtual CefRefPtr<CefLoadHandler> GetLoadHandler() OVERRIDE;
3639

3740
public: /* CefLifeSpanHandler overrides */
3841
virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser,
@@ -49,6 +52,7 @@ class BrowserClient : public CefClient, public CefLifeSpanHandler,
4952
CefRefPtr<CefMenuModel> model);
5053
private:
5154
CefRefPtr<CefRenderHandler> renderHandler;
55+
CefRefPtr<CefLoadHandler> loadHandler;
5256

5357
public:
5458
IMPLEMENT_REFCOUNTING(BrowserClient);

shared/browser-settings.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
struct BrowserSettings {
2323
std::string url;
24+
std::string css;
2425
unsigned int width;
2526
unsigned int height;
2627
unsigned int fps;

0 commit comments

Comments
 (0)