Skip to content

Commit

Permalink
v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsmfm committed Jul 29, 2024
1 parent 73913c1 commit 39fd942
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change log

## 1.0.1

- Use activeTab and scripting instead

## 1.0.0

- Migrate to MV3
Expand Down
9 changes: 1 addition & 8 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": ["<all_urls>"],
"js": ["src/content.ts"],
"run_at": "document_start"
}
],
"action": {
"default_icon": {
"16": "icons/icon16.png",
Expand Down
47 changes: 46 additions & 1 deletion src/background.ts
Original file line number Diff line number Diff line change
@@ -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],
});
}
}
});
Expand Down
40 changes: 0 additions & 40 deletions src/content.ts

This file was deleted.

0 comments on commit 39fd942

Please sign in to comment.