@@ -2,13 +2,14 @@ import "regenerator-runtime/runtime"; // needed for ``await`` support
2
2
import Base from "../../core/base" ;
3
3
import logging from "../../core/logging" ;
4
4
import Parser from "../../core/parser" ;
5
+ import registry from "../../core/registry" ;
5
6
6
7
const logger = logging . getLogger ( "push" ) ;
7
8
8
9
export const parser = new Parser ( "push" ) ;
9
10
parser . addArgument ( "url" , null ) ;
10
11
parser . addArgument ( "push-id" , null ) ;
11
- parser . addArgument ( "mode" , "replace" ) ;
12
+ parser . addArgument ( "mode" , "replace" , [ "replace" , "append" , "desktop-notification" ] ) ;
12
13
13
14
export default Base . extend ( {
14
15
name : "push" ,
@@ -20,7 +21,9 @@ export default Base.extend({
20
21
logger . debug ( "received push marker" ) ;
21
22
const data = e ?. detail ?. body ;
22
23
if ( data === this . options . pushId ) {
23
- if ( this . el . tagName === "FORM" ) {
24
+ if ( this . options . mode === "desktop-notification" ) {
25
+ this . desktop_notification ( ) ;
26
+ } else if ( this . el . tagName === "FORM" ) {
24
27
this . el . submit ( ) ;
25
28
} else {
26
29
this . perform_inject ( ) ;
@@ -38,6 +41,48 @@ export default Base.extend({
38
41
} else {
39
42
this . el . innerHTML = data ;
40
43
}
44
+ registry . scan ( this . el ) ;
45
+ } catch ( e ) {
46
+ logger . error (
47
+ `Could not fetch from ${ this . options . url } on push-id ${ this . options . pushId } .`
48
+ ) ;
49
+ }
50
+ } ,
51
+
52
+ async desktop_notification ( ) {
53
+ try {
54
+ const response = await fetch ( this . options . url ) ;
55
+ const data = await response . json ( ) ;
56
+
57
+ if ( data . length === 0 ) {
58
+ return ;
59
+ }
60
+
61
+ // Let's check if the browser supports notifications
62
+ if ( ! ( "Notification" in window ) ) {
63
+ logger . error ( "This browser does not support notifications." ) ;
64
+ return ;
65
+ }
66
+
67
+ // Notifications need to be granted.
68
+ // Note: Current browsers don't allow an automatic request for
69
+ // permission but need an interaction to allow it.
70
+ // The following code won't work out of the box in such cases.
71
+ if ( ! ( Notification . permission in [ "denied" , "granted" ] ) ) {
72
+ Notification . requestPermission ( ( permission ) => {
73
+ // Whatever the user answers, we make sure Chrome stores the information
74
+ if ( ! ( "permission" in Notification ) ) {
75
+ Notification . permission = permission ;
76
+ }
77
+ } ) ;
78
+ }
79
+
80
+ // Let's check if the user is okay to get some notification
81
+ if ( Notification . permission === "granted" ) {
82
+ for ( const message of data ) {
83
+ new Notification ( message . title , message ) ;
84
+ }
85
+ }
41
86
} catch ( e ) {
42
87
logger . error (
43
88
`Could not fetch from ${ this . options . url } on push-id ${ this . options . pushId } .`
0 commit comments