@@ -63,7 +63,7 @@ function post(target, data, callback) {
6363 }
6464 rt . post ( target , data ) ;
6565}
66- rt . listen ( id , function ( obj ) {
66+ function onMessage ( obj ) {
6767 var maps = {
6868 Command : function ( data ) {
6969 comm . post ( { cmd : 'Command' , data : data } ) ;
@@ -77,10 +77,14 @@ rt.listen(id, function (obj) {
7777 HttpRequested : function ( res ) {
7878 comm . post ( { cmd : 'HttpRequested' , data : res } ) ;
7979 } ,
80+ NotificationClick : onNotificationClick ,
81+ NotificationClose : onNotificationClose ,
8082 } ;
8183 var func = maps [ obj . cmd ] ;
8284 if ( func ) func ( obj . data ) ;
83- } ) ;
85+ }
86+ rt . listen ( id , onMessage ) ;
87+ rt . listen ( 'Broadcast' , onMessage ) ;
8488
8589/**
8690 * @desc Wrap methods to prevent unexpected modifications.
@@ -210,6 +214,25 @@ var comm = {
210214 var req = comm . requests [ r . id ] ;
211215 if ( req ) req . callback ( r ) ;
212216 } ,
217+ UpdateValues : function ( data ) {
218+ var values = comm . values ;
219+ if ( values && values [ data . uri ] ) values [ data . uri ] = data . values ;
220+ } ,
221+ NotificationClicked : function ( id ) {
222+ var options = comm . notif [ id ] ;
223+ if ( options ) {
224+ var onclick = options . onclick ;
225+ onclick && onclick ( ) ;
226+ }
227+ } ,
228+ NotificationClosed : function ( id ) {
229+ var options = comm . notif [ id ] ;
230+ if ( options ) {
231+ delete comm . notif [ id ] ;
232+ var ondone = options . ondone ;
233+ ondone && ondone ( ) ;
234+ }
235+ } ,
213236 } ;
214237 var func = maps [ obj . cmd ] ;
215238 if ( func ) func ( obj . data ) ;
@@ -501,6 +524,41 @@ var comm = {
501524 return comm . Request ( details ) ;
502525 } ,
503526 } ,
527+ GM_notification : {
528+ value : function ( text , title , image , onclick ) {
529+ if ( ! text ) {
530+ throw 'Invalid parameters.' ;
531+ }
532+ var options = typeof text === 'object' ? text : {
533+ text : text ,
534+ title : title ,
535+ image : image ,
536+ onclick : onclick ,
537+ } ;
538+ var id = comm . notif [ '' ] = ( comm . notif [ '' ] || 0 ) + 1 ;
539+ comm . notif [ id ] = options ;
540+ comm . post ( {
541+ cmd : 'Notification' ,
542+ data : {
543+ id : id ,
544+ text : options . text ,
545+ title : options . title ,
546+ image : options . image ,
547+ } ,
548+ } ) ;
549+ } ,
550+ } ,
551+ GM_setClipboard : {
552+ value : function ( text , type ) {
553+ comm . post ( {
554+ cmd : 'SetClipboard' ,
555+ data : {
556+ type : type ,
557+ data : text ,
558+ } ,
559+ } ) ;
560+ } ,
561+ } ,
504562 } ;
505563 comm . forEach ( grant , function ( name ) {
506564 var prop = gm_funcs [ name ] ;
@@ -551,6 +609,7 @@ var comm = {
551609 var idle = [ ] ;
552610 var end = [ ] ;
553611 comm . command = { } ;
612+ comm . notif = { } ;
554613 comm . version = data . version ;
555614 comm . values = { } ;
556615 // reset load and checkLoad
@@ -616,11 +675,33 @@ function handleC(e) {
616675 document . head . appendChild ( style ) ;
617676 }
618677 } ,
678+ Notification : onNotificationCreate ,
679+ SetClipboard : function ( data ) {
680+ post ( 'Background' , { cmd : 'SetClipboard' , data : data } ) ;
681+ } ,
619682 } ;
620683 var func = maps [ req . cmd ] ;
621684 if ( func ) func ( req . data ) ;
622685}
623686
687+ var notifications = { } ;
688+ function onNotificationCreate ( options ) {
689+ post ( 'Background' , { cmd : 'Notification' , data : options } , function ( nid ) {
690+ notifications [ nid ] = options . id ;
691+ } ) ;
692+ }
693+ function onNotificationClick ( nid ) {
694+ var id = notifications [ nid ] ;
695+ id && comm . post ( { cmd : 'NotificationClicked' , data : id } ) ;
696+ }
697+ function onNotificationClose ( nid ) {
698+ var id = notifications [ nid ] ;
699+ if ( id ) {
700+ comm . post ( { cmd : 'NotificationClosed' , data : id } ) ;
701+ delete notifications [ nid ] ;
702+ }
703+ }
704+
624705function objEncode ( obj ) {
625706 var list = [ ] ;
626707 for ( var i in obj ) {
0 commit comments