11
2- async function send_notification ( title , text ) {
2+ async function addToClipboard ( value ) {
3+ await chrome . offscreen . createDocument ( {
4+ url : 'html/offscreen.html' ,
5+ reasons : [ chrome . offscreen . Reason . CLIPBOARD ] ,
6+ justification : 'Write text to the clipboard.'
7+ } ) ;
8+ chrome . runtime . sendMessage ( {
9+ type : 'copy-data-to-clipboard' ,
10+ target : 'offscreen-doc' ,
11+ data : value
12+ } ) ;
13+ }
14+
15+ async function sendNotification ( title , text ) {
316 chrome . notifications . create ( {
417 type : 'basic' ,
518 iconUrl : '/images/logo128.png' ,
@@ -9,7 +22,7 @@ async function send_notification(title, text){
922 } ) ;
1023}
1124
12- async function post_url ( endpoint , url ) {
25+ async function postURL ( endpoint , url ) {
1326 console . log ( 'Processing URL: ' + url ) ;
1427
1528 let _url = ( await chrome . storage . local . get ( 'url' ) ) [ 'url' ] ;
@@ -39,19 +52,20 @@ async function genericOnClick(ctx) {
3952 console . log ( 'Processing URL: ' + ctx . srcUrl ) ;
4053 let response ;
4154 try {
42- response = await post_url ( 'remote' , ctx . srcUrl )
55+ response = await postURL ( 'remote' , ctx . srcUrl )
4356 } catch ( error ) {
44- await send_notification ( 'Fetch Error' , 'Error: ' + error . message ) ;
57+ await sendNotification ( 'Fetch Error' , 'Error: ' + error . message ) ;
4558 }
4659 const data = await response . json ( ) ;
4760 console . log ( data ) ;
4861 if ( response . ok ) {
4962 console . log ( data [ 'url' ] ) ;
50- await send_notification ( 'Image Uploaded' , data [ 'url' ] ) ;
63+ await addToClipboard ( data [ 'url' ] ) ;
64+ await sendNotification ( 'Image Uploaded' , data [ 'url' ] ) ;
5165 // await navigator.clipboard.writeText(data['url']);
5266 } else {
5367 console . log ( data [ 'error' ] ) ;
54- await send_notification ( 'Processing Error' , 'Error: ' + data [ 'error' ] ) ;
68+ await sendNotification ( 'Processing Error' , 'Error: ' + data [ 'error' ] ) ;
5569 }
5670 }
5771 break ;
@@ -61,19 +75,19 @@ async function genericOnClick(ctx) {
6175 console . log ( 'Processing URL: ' + ctx . linkUrl ) ;
6276 let response ;
6377 try {
64- response = await post_url ( 'shorten' , ctx . linkUrl )
78+ response = await postURL ( 'shorten' , ctx . linkUrl ) ;
6579 } catch ( error ) {
66- await send_notification ( 'Fetch Error' , 'Error: ' + error . message ) ;
80+ await sendNotification ( 'Fetch Error' , 'Error: ' + error . message ) ;
6781 }
6882 const data = await response . json ( ) ;
6983 console . log ( data ) ;
7084 if ( response . ok ) {
7185 console . log ( data [ 'url' ] ) ;
72- await send_notification ( 'Short Created' , data [ 'url' ] ) ;
73- // await navigator.clipboard.writeText( data['url']);
86+ await addToClipboard ( data [ 'url' ] ) ;
87+ await sendNotification ( 'Short Created' , data [ 'url' ] ) ;
7488 } else {
7589 console . log ( data [ 'error' ] ) ;
76- await send_notification ( 'Processing Error' , 'Error: ' + data [ 'error' ] ) ;
90+ await sendNotification ( 'Processing Error' , 'Error: ' + data [ 'error' ] ) ;
7791 }
7892 }
7993 break ;
0 commit comments