11/* global browser */
22
3- // tabId => dataURI
43let tempData = new Map ( ) ;
5- /*
4+
5+ // tabId => dataURI
66function 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
2524browser . 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
7273browser . 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+ } ) ;
0 commit comments