Skip to content

Commit 013981f

Browse files
committed
feat: use tab capture only for drm protected content and canvas method for none-drm videos
1 parent f20612f commit 013981f

File tree

2 files changed

+46
-21
lines changed

2 files changed

+46
-21
lines changed

copy-video-frame/background.js

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,28 +27,46 @@ browser.menus.create({
2727
onclick: async (info, tab) => {
2828
try {
2929
// first we get the <video>
30-
await browser.tabs.executeScript(tab.id, {
31-
frameId: info.frameId,
32-
code: `var vidEl = browser.menus.getTargetElement(${info.targetElementId})`,
33-
});
34-
// then we get the video coords + size (x,y,width,height)
35-
let elBrect = await browser.tabs.executeScript(tab.id, {
36-
frameId: info.frameId,
37-
file: "getElBRect.js",
38-
});
39-
elBrect = elBrect[0];
40-
// then we capture visible area of the video
41-
const dataURI = await browser.tabs.captureVisibleTab(tab.windowId, {
42-
rect: elBrect,
43-
});
30+
const drmProtected = (
31+
await browser.tabs.executeScript(tab.id, {
32+
frameId: info.frameId,
33+
code: `var vidEl = browser.menus.getTargetElement(${info.targetElementId});
34+
var drmProtected = vidEl.mediaKeys !== null;
35+
if(!drmProtected){
36+
let canvas = document.createElement('canvas');
37+
canvas.width = vidEl.videoWidth;
38+
canvas.height = vidEl.videoHeight;
39+
let context = canvas.getContext('2d');
40+
context.drawImage(vidEl, 0, 0, vidEl.videoWidth, vidEl.videoHeight);
41+
browser.runtime.sendMessage({ "dataURI": canvas.toDataURL() });
42+
}
43+
drmProtected;
44+
`,
45+
})
46+
)[0];
47+
48+
console.log("drmProtected", drmProtected);
4449

50+
if (drmProtected) {
51+
// then we get the video coords + size (x,y,width,height)
52+
let elBrect = await browser.tabs.executeScript(tab.id, {
53+
frameId: info.frameId,
54+
file: "getElBRect.js",
55+
});
56+
elBrect = elBrect[0];
57+
// then we capture visible area of the video
58+
const dataURI = await browser.tabs.captureVisibleTab(tab.windowId, {
59+
rect: elBrect,
60+
});
61+
tempData.set(tab.id, dataURI);
62+
}
4563
// if we have the clipboardWrite permission, just copy into the clipboard
4664
if (
4765
await browser.permissions.contains({
4866
permissions: ["clipboardWrite"],
4967
})
5068
) {
51-
const blob = await (await fetch(dataURI)).blob();
69+
const blob = await (await fetch(tempData.get(tab.id))).blob();
5270
await navigator.clipboard.write([
5371
new ClipboardItem({
5472
"image/png": blob,
@@ -57,11 +75,13 @@ browser.menus.create({
5775
notify("Copy Video Frame", "Image in clipboard\n(Insert with CTRL+V)");
5876
} else {
5977
// if we dont have the clipbordWrite permission, open the image in a new tab
60-
tempData.set(tab.id, dataURI);
61-
browser.tabs.create({
62-
active: true,
63-
url: "show.html?tabId=" + tab.id,
64-
});
78+
//tempData.set(tab.id, dataURI);
79+
setTimeout(() => {
80+
browser.tabs.create({
81+
active: true,
82+
url: "show.html?tabId=" + tab.id,
83+
});
84+
}, 750);
6585
}
6686
} catch (e) {
6787
console.error(e);
@@ -71,6 +91,11 @@ browser.menus.create({
7191
});
7292

7393
browser.runtime.onMessage.addListener((data, sender) => {
94+
//console.debug('onMessage', data, sender);
95+
if (data.dataURI) {
96+
tempData.set(sender.tab.id, data.dataURI);
97+
return;
98+
}
7499
if (data.tabId) {
75100
return Promise.resolve(tempData.get(data.tabId));
76101
}

copy-video-frame/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
"name": "Copy Video Frame",
1313
"permissions": ["menus", "activeTab"],
1414
"optional_permissions": ["clipboardWrite", "notifications"],
15-
"version": "1.1.10"
15+
"version": "1.1.11"
1616
}

0 commit comments

Comments
 (0)