From a916a2b363c7a315ea8cfd588b43026deb4c36a4 Mon Sep 17 00:00:00 2001 From: smdsbz <694066681@qq.com> Date: Sat, 10 Jul 2021 22:45:16 +0800 Subject: [PATCH 01/12] successfully connecting to OBS --- addon/manifest.json | 3 ++- background_script.js | 12 +++++++++++- package.json | 5 +++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/addon/manifest.json b/addon/manifest.json index 2b2ef94..d5f17ca 100755 --- a/addon/manifest.json +++ b/addon/manifest.json @@ -17,5 +17,6 @@ }, "default_popup": "browserAction/index.html", "default_title": "OBScure" - } + }, + "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self';" } \ No newline at end of file diff --git a/background_script.js b/background_script.js index 77cff58..39e8c88 100755 --- a/background_script.js +++ b/background_script.js @@ -1 +1,11 @@ -// Put all the javascript code here, that you want to execute in background. +const OBSWebSocket = require('obs-websocket-js'); + +(async () => { + const obs = new OBSWebSocket(); + await obs.connect({password: "12345678"}); + const version = await obs.send("GetVersion"); + alert(`OBSRemote version: ${version.version}`); + alert(`obs-websocket version: ${version['obs-websocket-version']}`); + alert(`OBS Studio version: ${version['obs-studio-version']}`); + obs.disconnect(); +})(); diff --git a/package.json b/package.json index 1bb10b0..f96fae7 100755 --- a/package.json +++ b/package.json @@ -16,7 +16,12 @@ "license": "ISC", "devDependencies": { "web-ext-plugin": "^1.3.1", + "webextension-polyfill": "^0.8.0", "webpack": "^5.44.0", "webpack-cli": "^4.7.2" + }, + "dependencies": { + "buffer": "^6.0.3", + "obs-websocket-js": "^4.0.2" } } -- Gitee From bc2faa05b42a7d82ce2572f2793c90798a341add Mon Sep 17 00:00:00 2001 From: smdsbz <694066681@qq.com> Date: Sun, 11 Jul 2021 17:17:18 +0800 Subject: [PATCH 02/12] successfully mirroing window --- addon/manifest.json | 4 +- background_script.js | 91 +++++++++++++++++++++++++++++++++++++++++--- package.json | 2 +- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/addon/manifest.json b/addon/manifest.json index d5f17ca..40e4f87 100755 --- a/addon/manifest.json +++ b/addon/manifest.json @@ -3,6 +3,9 @@ "name": "OBScure", "description": "The more intuitive and privacy-secured way to mirrors web page element to OBS.", "version": "0.0.1", + "permissions": [ + "tabs" + ], "icons": { "64": "icons/icon.png" }, @@ -15,7 +18,6 @@ "default_icon": { "64": "icons/icon.png" }, - "default_popup": "browserAction/index.html", "default_title": "OBScure" }, "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self';" diff --git a/background_script.js b/background_script.js index 39e8c88..8a4ec6b 100755 --- a/background_script.js +++ b/background_script.js @@ -1,11 +1,92 @@ const OBSWebSocket = require('obs-websocket-js'); +const { browser } = require('webextension-polyfill-ts'); + + +let obs; +/** + * `${windowId}/${tabId}` -> OBS source identifier + * @type {Map} + */ +let mirroring_tabs = new Map(); (async () => { - const obs = new OBSWebSocket(); - await obs.connect({password: "12345678"}); - const version = await obs.send("GetVersion"); + obs = new OBSWebSocket(); + await obs.connect({password: '12345678'}); + /* NOTE: Firefox does not support this */ + // browser.runtime.onSuspend.addListener(async () => { + // obs.disconnect(); + // }); + + const version = await obs.send('GetVersion'); + alert(`OBS Studio version: ${version['obs-studio-version']}`); alert(`OBSRemote version: ${version.version}`); alert(`obs-websocket version: ${version['obs-websocket-version']}`); - alert(`OBS Studio version: ${version['obs-studio-version']}`); - obs.disconnect(); + + let current_scene = await obs.send('GetCurrentScene'); + alert(`current scene: ${current_scene.name}`); + alert(`list souces:`); + for (let s of current_scene.sources) + alert(`${JSON.stringify(s)}`); + + let items = await obs.send('GetSceneItemList', {sceneName: current_scene.name}); + alert(`items:`); + for (let i of items.sceneItems) { + alert(JSON.stringify(i)); + } + + // try { + // let window_source_id = await obs.send('CreateSource', { + // sourceName: 'OBScure::test', + // sourceKind: 'window_capture', + // sceneName: current_scene.name, + // sourceSettings: {window: 'Snip & Sketch:Windows.UI.Core.CoreWindow:SceenSketch.exe'} + // }); + // } catch (e) { + // alert(JSON.stringify(e)); + // } + + const rt_info = await browser.runtime.getPlatformInfo(); + alert(JSON.stringify(rt_info)); + + // const tabs = await browser.tabs.query({ + // active: true, currentWindow: true + // }) + // alert(JSON.stringify(tabs)); + + // window_title = `${tab.title} — Mozilla Firefox:MozillaWindowClass:firefox.exe`; + + browser.browserAction.onClicked.addListener(async tab => { + if (!tab.url.match(/https?:\/\/.*?\.bilibili.com\/video\/.*/)) { + alert('not a supported page'); + return; + } + win_title = `${tab.title} — Mozilla Firefox:MozillaWindowClass:firefox.exe`; + alert(win_title); + const scene = (await obs.send('GetCurrentScene')).name; + let source = (await obs.send('CreateSource', { + sourceName: 'OBScure::test', + sourceKind: 'window_capture', + sceneName: scene, + sourceSettings: {window: win_title} + })).itemId; + mirroring_tabs.set( + `${tab.windowId}/${tab.id}`, + {scene_name: scene, source_id: source} + ); + }); + + browser.tabs.onRemoved.addListener(async (tabId, {windowId}) => { + const scene_source = mirroring_tabs.get(`${windowId}/${tabId}`); + if (typeof scene_source === 'undefined') + return; + const {scene_name, source_id} = scene_source; + await obs.send('DeleteSceneItem', { + scene: scene_name, + item: {id: source_id} + }); + }); + + + + // obs.disconnect(); })(); diff --git a/package.json b/package.json index f96fae7..20b8358 100755 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "license": "ISC", "devDependencies": { "web-ext-plugin": "^1.3.1", - "webextension-polyfill": "^0.8.0", + "webextension-polyfill-ts": "^0.26.0", "webpack": "^5.44.0", "webpack-cli": "^4.7.2" }, -- Gitee From d2871a78ee555a196623101829fcc4ab7ca48b85 Mon Sep 17 00:00:00 2001 From: smdsbz <694066681@qq.com> Date: Thu, 15 Jul 2021 21:45:01 +0800 Subject: [PATCH 03/12] reorganizing code --- .vscode/tasks.json | 2 +- README.md | 24 +++++++++ background_script.js | 124 ++++++++++++++++++++++++------------------- 3 files changed, 93 insertions(+), 57 deletions(-) create mode 100755 README.md diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 9a9aa1c..b8809bb 100755 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -15,7 +15,7 @@ "type": "shell", "command": "web-ext run", "options": { - "cwd": "addon" + "cwd": "addon", }, "group": { "kind": "test", diff --git a/README.md b/README.md new file mode 100755 index 0000000..6959549 --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +OBScure +======= + +Conveniently mirroring elements in web page to OBS ... +* without having to manually moving / cropping source in OBS +* no worries about accidentally leaking info + +Usage +----- + +TODO: + +Roadmap +------- + +* [x] Proof-of-Concept: mirror the corrent window to OBS +* [ ] Crop source to the correct size +* [ ] Align mirrored source to a predefined area in OBS +* [ ] Option page for configuring OBS-WebSocket password, and other +* [ ] On mirroring area shifted (e.g. resize, scroll, DOM change), cancel the mirror +* [ ] Not just `