Skip to content

Commit a6157da

Browse files
committed
Initial commit
0 parents  commit a6157da

File tree

13 files changed

+884
-0
lines changed

13 files changed

+884
-0
lines changed

.github/workflows/release.yml

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Release
2+
on:
3+
push:
4+
tags:
5+
- "v*.*.*"
6+
workflow_dispatch:
7+
inputs:
8+
tag:
9+
required: true
10+
type: string
11+
prerelease:
12+
required: true
13+
type: boolean
14+
jobs:
15+
build:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout
19+
uses: actions/checkout@v2
20+
- name: Publish Release
21+
uses: softprops/action-gh-release@v0.1.14
22+
with:
23+
generate_release_notes: true
24+
fail_on_unmatched_files: true
25+
tag_name: ${{ github.event.inputs.tag || github.ref }}
26+
prerelease: ${{ github.event.inputs.prerelease }}
27+
draft: ${{ github.event_name != 'workflow_dispatch' }}
28+
files: |
29+
custom-browser-events.py

LICENSE

+674
Large diffs are not rendered by default.

README-zh.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# OBS 浏览器源自定义 JS 事件
2+
3+
这是一个针对 OBS 的 Python 脚本,可以让你用快捷键在当前场景组中所有的浏览器源上触发自定义的 JavaScript 事件。
4+
5+
## 用法
6+
7+
1. 启动 OBS Studio。在“**工具 > 脚本**”中, 选择“**Python 设置**”一栏。选中 Python 的安装位置。请确保 Python 已经安装好,且体系结构与 OBS 相同(如 64 位的 OBS 就要装 64 位的 Python,不能装 32 位的 Python)。
8+
9+
![在“Python 设置”中的“Python 安装路径”中打开正确的文件夹。OBS 会显示自己是多少位的。选好了正确的位置后,下方会显示“已加载 Python 版本:3.xx”。](images/1z.png)
10+
11+
2. 仍然在“脚本”窗口中,回到“**脚本**”一栏,点击左下角的加号按钮。选中 `custom-browser-events.py` 文件。
12+
13+
![加号按钮鼠标悬浮时会显示“添加脚本”。](images/2z.png)
14+
15+
3. 脚本加载好后,右边会出现“**Events**”框,可以用来管理您的自定义事件。
16+
17+
![窗口右侧会出现本脚本的说明和“Events”框。初始会有三个默认事件,“obsCustomEvent1”到“obsCustomEvent3”.](images/3z.png)
18+
19+
4. 设置好事件名称后,您可以关闭“**脚本**”窗口,前往“**文件 > 设置 > 快捷键**”设置快捷键。自定义事件会出现在第一个小标题上面。
20+
21+
![每个自定义事件都会有一个对应的新指令,叫做“Custom browser event”加上事件名称。](images/4z.png)
22+
23+
5. 要侦听自定义事件,只需在浏览器源的页面的 JavaScript 代码中添加:
24+
25+
~~~js
26+
window.addEventListener("obsCustomEvent1", function (ev) {
27+
document.body.append("Hello, world!")
28+
})
29+
~~~
30+
31+
或者使用函数定义:
32+
33+
~~~js
34+
function doSomething(ev) {
35+
// ...
36+
}
37+
window.addEventListener("obsCustomEvent1", doSomething)
38+
~~~
39+
40+
您也可以省略“`window.`”。

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Custom JavaScript events for OBS Studio browser sources <br> OBS 浏览器源自定义 JS 事件
2+
3+
[简体中文 · Read in Simplified Chinese](README-zh.md)
4+
5+
This Python script for OBS Studio allows you to trigger custom JavaScript events on all browser sources (in the current scene collection) using hotkeys.
6+
7+
## Usage
8+
9+
1. Launch OBS Studio. Under **Tools > Scripts**, open the **Python Settings** tab. Choose the installation folder of Python. Make sure you have Python installed and it has the same architecture as OBS studio (e.g. for 64-bit OBS Studio install 64-bit Python, not 32-bit Python).
10+
11+
![Browse to the correct path using the “Python Install Path” box in “Python Settings”. It will tell you your OBS Studio architecture. Once the correct path is selected, you should see the line “Loaded Python Version: 3.xx”.](images/1.png)
12+
13+
2. In the same window, navigate back to the **Scripts** tab and click on the plus button in the bottom left. Choose the `custom-browser-events.py` file.
14+
15+
![The plus button says “Add Scripts” when hovered.](images/2.png)
16+
17+
3. After the script is loaded, you should see the **Events** box to the right. Here you can manage your custom events.
18+
19+
![A description of the script and the “Events” box show up in the right side of the window. There are three default events, “obsCustomEvent1” through “obsCustomEvent3”.](images/3.png)
20+
21+
4. Once you are happy with the event names, you can close the **Scripts** window and go to **Files > Settings > Hotkeys** to set your hotkeys. The custom events will appear at the bottom of the topmost section, above the sections for specific scenes.
22+
23+
![You will see new available actions labeled “Custom browser event” followed by your event names.](images/4.png)
24+
25+
5. To listen for custom events, simply add something like this in the JavaScript code for your browser page:
26+
27+
~~~js
28+
window.addEventListener("obsCustomEvent1", function (ev) {
29+
document.body.append("Hello, world!")
30+
})
31+
~~~
32+
33+
Or with a function declaration:
34+
35+
~~~js
36+
function doSomething(ev) {
37+
// ...
38+
}
39+
window.addEventListener("obsCustomEvent1", doSomething)
40+
~~~
41+
42+
Omit “`window.`if you like.

custom-browser-events.py

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Custom JavaScript events for OBS Studio browser sources
2+
# Copyright (C) 2023 DGCK81LNN
3+
# Documentation can be found at https://github.com/DGCK81LNN/obs_custom-browser-events.py
4+
#
5+
# This program is free software: you can redistribute and/or modify it
6+
# under the terms of the GNU General Public License version 3.
7+
#
8+
# This program is distributed WITHOUT ANY WARRANTY;
9+
# See the GNU General Public License for more details.
10+
#
11+
# You should have received a copy of the GNU General Public License
12+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
13+
14+
import obspython as obs
15+
from datetime import datetime
16+
17+
def obs_data_get_string_array(data, name):
18+
items = []
19+
array = obs.obs_data_get_array(data, name)
20+
for i in range(obs.obs_data_array_count(array)):
21+
item = obs.obs_data_array_item(array, i)
22+
items.append(obs.obs_data_get_string(item, "value"))
23+
obs.obs_data_release(item)
24+
obs.obs_data_array_release(array)
25+
return items
26+
27+
events = []
28+
hotkey_ids = {}
29+
hotkey_callbacks = []
30+
31+
def script_description():
32+
return "Assign hotkeys to custom browser events. Set hotkeys in OBS settings.\n\nby DGCK81LNN <https://github.com/DGCK81LNN>"
33+
34+
def script_defaults(settings):
35+
array = obs.obs_data_array_create()
36+
for i in range(3):
37+
item = obs.obs_data_create()
38+
obs.obs_data_set_string(item, "value", "obsCustomEvent%d" % (i + 1))
39+
obs.obs_data_array_insert(array, obs.obs_data_array_count(array), item)
40+
obs.obs_data_release(item)
41+
obs.obs_data_set_default_array(settings, "events", array)
42+
obs.obs_data_array_release(array)
43+
44+
def script_properties():
45+
props = obs.obs_properties_create()
46+
obs.obs_properties_add_editable_list(props, "events", "Events", obs.OBS_EDITABLE_LIST_TYPE_STRINGS, None, None)
47+
return props
48+
49+
def script_load(settings):
50+
global events, hotkey_ids
51+
events = obs_data_get_string_array(settings, "events")
52+
hotkey_ids = {}
53+
54+
for event_name in events:
55+
hotkey_callback = callback_for(event_name)
56+
hotkey_id = obs.obs_hotkey_register_frontend(
57+
script_path() + "//" + event_name,
58+
"Custom browser event %r" % event_name,
59+
hotkey_callback
60+
)
61+
hotkey_callbacks.append(hotkey_callback)
62+
hotkey_save_array = obs.obs_data_get_array(settings, event_name)
63+
obs.obs_hotkey_load(hotkey_id, hotkey_save_array)
64+
obs.obs_data_array_release(hotkey_save_array)
65+
hotkey_ids[event_name] = hotkey_id
66+
67+
def script_unload():
68+
for callback in hotkey_callbacks:
69+
obs.obs_hotkey_unregister(callback)
70+
hotkey_callbacks.clear()
71+
72+
def script_update(settings):
73+
if obs_data_get_string_array(settings, "events") != events:
74+
script_unload()
75+
script_load(settings)
76+
77+
def script_save(settings):
78+
for event_name in events:
79+
hotkey_save_array = obs.obs_hotkey_save(hotkey_ids[event_name])
80+
obs.obs_data_set_array(settings, event_name, hotkey_save_array)
81+
obs.obs_data_array_release(hotkey_save_array)
82+
83+
def callback_for(event_name):
84+
return lambda pressed: trigger(event_name) if pressed else None
85+
86+
def trigger(event_name):
87+
print(
88+
"[%s] Triggered event %r" % (
89+
datetime.now().astimezone().isoformat(timespec="seconds"),
90+
event_name
91+
)
92+
)
93+
for src in obs.obs_enum_sources():
94+
if obs.obs_source_get_id(src) == "browser_source":
95+
cd = obs.calldata_create()
96+
obs.calldata_set_string(cd, "eventName", event_name)
97+
obs.calldata_set_string(cd, "jsonString", "{}")
98+
obs.proc_handler_call(obs.obs_source_get_proc_handler(src), "javascript_event", cd)
99+
obs.calldata_destroy(cd)

images/1.png

16.4 KB
Loading

images/1z.png

13.5 KB
Loading

images/2.png

17.2 KB
Loading

images/2z.png

13.2 KB
Loading

images/3.png

34.3 KB
Loading

images/3z.png

29.2 KB
Loading

images/4.png

44.8 KB
Loading

images/4z.png

35 KB
Loading

0 commit comments

Comments
 (0)