From 39fd942851e945e9b8d9dd23361d4e5afb39a78b Mon Sep 17 00:00:00 2001 From: Fumiaki MATSUSHIMA Date: Tue, 30 Jul 2024 00:11:42 +0900 Subject: [PATCH] v1.0.1 --- CHANGELOG.md | 4 ++++ manifest.json | 9 +-------- src/background.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/content.ts | 40 ---------------------------------------- 4 files changed, 51 insertions(+), 49 deletions(-) delete mode 100644 src/content.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index c95aaa5..791ee7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change log +## 1.0.1 + +- Use activeTab and scripting instead + ## 1.0.0 - Migrate to MV3 diff --git a/manifest.json b/manifest.json index 6234c81..4ec9179 100644 --- a/manifest.json +++ b/manifest.json @@ -7,18 +7,11 @@ "16": "icons/icon16.png", "128": "icons/icon128.png" }, - "permissions": ["tabCapture"], + "permissions": ["tabCapture", "activeTab", "scripting"], "background": { "service_worker": "src/background.ts", "type": "module" }, - "content_scripts": [ - { - "matches": [""], - "js": ["src/content.ts"], - "run_at": "document_start" - } - ], "action": { "default_icon": { "16": "icons/icon16.png", diff --git a/src/background.ts b/src/background.ts index 6e9c2f8..654ce6b 100644 --- a/src/background.ts +++ b/src/background.ts @@ -1,9 +1,54 @@ +const runPipScript = async (tabId: number) => { + if (!document.pictureInPictureElement) { + const { streamId }: { streamId: string } = await chrome.runtime.sendMessage( + { + action: "GET_STREAM_ID", + tabId, + } + ); + + const stream = await navigator.mediaDevices.getUserMedia({ + video: { + mandatory: { + minWidth: 50, + minHeight: 50, + maxWidth: 1920, + maxHeight: 1080, + minFrameRate: 10, + maxFrameRate: 60, + chromeMediaSource: "tab", + chromeMediaSourceId: streamId, + }, + } as any, + audio: false, + }); + + const video = document.createElement("video"); + video.srcObject = stream; + + video.addEventListener("loadedmetadata", () => { + video.requestPictureInPicture(); + video.play(); + }); + + video.addEventListener("leavepictureinpicture", () => { + stream.getTracks().forEach((t) => t.stop()); + }); + } else { + document.exitPictureInPicture(); + } +}; + chrome.action.onClicked.addListener(async (tab) => { if (tab.id && tab.url) { if (tab.url.startsWith("chrome://")) { // noop } else { - await chrome.tabs.sendMessage(tab.id!, { tabId: tab.id }); + await chrome.scripting.executeScript({ + target: { tabId: tab.id }, + func: runPipScript, + args: [tab.id], + }); } } }); diff --git a/src/content.ts b/src/content.ts deleted file mode 100644 index 36cbad5..0000000 --- a/src/content.ts +++ /dev/null @@ -1,40 +0,0 @@ -chrome.runtime.onMessage.addListener(async ({ tabId }: { tabId: number }) => { - if (!document.pictureInPictureElement) { - const { streamId }: { streamId: string } = await chrome.runtime.sendMessage( - { - action: "GET_STREAM_ID", - tabId, - } - ); - - const stream = await navigator.mediaDevices.getUserMedia({ - video: { - mandatory: { - minWidth: 50, - minHeight: 50, - maxWidth: 1920, - maxHeight: 1080, - minFrameRate: 10, - maxFrameRate: 60, - chromeMediaSource: "tab", - chromeMediaSourceId: streamId, - }, - } as any, - audio: false, - }); - - const video = document.createElement("video"); - video.srcObject = stream; - - video.addEventListener("loadedmetadata", () => { - video.requestPictureInPicture(); - video.play(); - }); - - video.addEventListener("leavepictureinpicture", () => { - stream.getTracks().forEach((t) => t.stop()); - }); - } else { - document.exitPictureInPicture(); - } -});