Skip to content

Commit 4f372b2

Browse files
committed
fix: fallback to tab capture when using canvas fails when CORS restrictions apply (insecure operation)
1 parent 013981f commit 4f372b2

File tree

2 files changed

+22
-18
lines changed

2 files changed

+22
-18
lines changed

copy-video-frame/background.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,33 @@ browser.menus.create({
2525
title: "Copy Video Frame",
2626
contexts: ["video"],
2727
onclick: async (info, tab) => {
28-
try {
28+
//try
29+
{
2930
// first we get the <video>
30-
const drmProtected = (
31+
const vidElrestricted = (
3132
await browser.tabs.executeScript(tab.id, {
3233
frameId: info.frameId,
34+
// DRM and CORS can lead to tab capture fallback
3335
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() });
36+
var restricted = vidEl.mediaKeys !== null;
37+
if(!restricted){
38+
try {
39+
let canvas = document.createElement('canvas');
40+
canvas.width = vidEl.videoWidth;
41+
canvas.height = vidEl.videoHeight;
42+
let context = canvas.getContext('2d');
43+
context.drawImage(vidEl, 0, 0, vidEl.videoWidth, vidEl.videoHeight);
44+
browser.runtime.sendMessage({ "dataURI": canvas.toDataURL() });
45+
}catch(e){
46+
restricted = true;
47+
}
4248
}
43-
drmProtected;
49+
restricted;
4450
`,
4551
})
4652
)[0];
4753

48-
console.log("drmProtected", drmProtected);
49-
50-
if (drmProtected) {
54+
if (vidElrestricted) {
5155
// then we get the video coords + size (x,y,width,height)
5256
let elBrect = await browser.tabs.executeScript(tab.id, {
5357
frameId: info.frameId,
@@ -83,15 +87,15 @@ drmProtected;
8387
});
8488
}, 750);
8589
}
86-
} catch (e) {
90+
} /*catch (e) {
8791
console.error(e);
8892
notify("Copy Video Frame", e.toString());
89-
}
93+
} */
9094
},
9195
});
9296

9397
browser.runtime.onMessage.addListener((data, sender) => {
94-
//console.debug('onMessage', data, sender);
98+
//console.debug("onMessage", data, sender);
9599
if (data.dataURI) {
96100
tempData.set(sender.tab.id, data.dataURI);
97101
return;

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.11"
15+
"version": "1.1.12"
1616
}

0 commit comments

Comments
 (0)