Skip to content

Commit f20612f

Browse files
committed
feat: add clipboard and notification as optional options
1 parent f635c41 commit f20612f

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

copy-video-frame/background.js

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* global browser */
22

3-
// tabId => dataURI
43
let tempData = new Map();
5-
/*
4+
5+
// tabId => dataURI
66
function notify(title, message = "", iconUrl = "icon.png") {
77
try {
88
const nid = browser.notifications.create("" + Date.now(), {
@@ -20,7 +20,6 @@ function notify(title, message = "", iconUrl = "icon.png") {
2020
// noop
2121
}
2222
}
23-
*/
2423

2524
browser.menus.create({
2625
title: "Copy Video Frame",
@@ -43,44 +42,43 @@ browser.menus.create({
4342
rect: elBrect,
4443
});
4544

46-
tempData.set(tab.id, dataURI);
47-
48-
//DATAURI = dataURI;
49-
//const blob = await (await fetch(dataURI)).blob();
50-
/*
51-
await navigator.clipboard.write([
52-
new ClipboardItem({
53-
"image/png": blob,
54-
}),
55-
]);
56-
notify("Copy Video Frame", "Image in clipboard\n(Insert with CTRL+V)");
57-
*/
58-
59-
//const objurl = URL.createObjectURL(blob, { type: "image/png" });
60-
61-
browser.tabs.create({
62-
active: true,
63-
url: "show.html?tabId=" + tab.id,
64-
});
45+
// if we have the clipboardWrite permission, just copy into the clipboard
46+
if (
47+
await browser.permissions.contains({
48+
permissions: ["clipboardWrite"],
49+
})
50+
) {
51+
const blob = await (await fetch(dataURI)).blob();
52+
await navigator.clipboard.write([
53+
new ClipboardItem({
54+
"image/png": blob,
55+
}),
56+
]);
57+
notify("Copy Video Frame", "Image in clipboard\n(Insert with CTRL+V)");
58+
} else {
59+
// 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+
});
65+
}
6566
} catch (e) {
6667
console.error(e);
67-
//notify("Copy Video Frame", e.toString());
68+
notify("Copy Video Frame", e.toString());
6869
}
6970
},
7071
});
7172

7273
browser.runtime.onMessage.addListener((data, sender) => {
73-
console.debug(data);
7474
if (data.tabId) {
7575
return Promise.resolve(tempData.get(data.tabId));
7676
}
7777
return false;
7878
});
7979

80-
function handleRemoved(tabId, removeInfo) {
80+
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
8181
if (tempData.has(tabId)) {
8282
tempData.delete(tabId);
8383
}
84-
}
85-
86-
browser.tabs.onRemoved.addListener(handleRemoved);
84+
});

copy-video-frame/manifest.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@
1111
"manifest_version": 2,
1212
"name": "Copy Video Frame",
1313
"permissions": ["menus", "activeTab"],
14-
"version": "1.1.9"
14+
"optional_permissions": ["clipboardWrite", "notifications"],
15+
"version": "1.1.10"
1516
}

0 commit comments

Comments
 (0)