@@ -33,87 +33,97 @@ export default Base.extend({
3333 } ,
3434
3535 async perform_inject ( ) {
36+ let data = null ;
37+
3638 try {
3739 const response = await fetch ( this . options . url ) ;
38- const data = await response . text ( ) ;
39- if ( this . options . mode === "append" ) {
40- this . el . insertAdjacentHTML ( "beforeend" , data ) ;
41- } else {
42- this . el . innerHTML = data ;
43- }
44- registry . scan ( this . el ) ;
40+ data = await response . text ( ) ;
4541 } catch ( e ) {
4642 logger . error (
4743 `Could not fetch from ${ this . options . url } on push-id ${ this . options . pushId } .`
4844 ) ;
4945 }
50- } ,
5146
52- async desktop_notification ( ) {
53- try {
54- // Let's check if the browser supports notifications
55- if ( ! ( "Notification" in window ) ) {
56- logger . error ( "This browser does not support notifications." ) ;
57- return ;
58- }
47+ if ( data === null ) {
48+ return ;
49+ }
5950
60- // Notifications need to be granted.
61- // Note: Current browsers don't allow an automatic request for
62- // permission but need an interaction to allow it.
63- // The following code won't work out of the box in such cases.
64- if ( ! ( Notification . permission in [ "denied" , "granted" ] ) ) {
65- Notification . requestPermission ( ( permission ) => {
66- // Whatever the user answers, we make sure Chrome stores the information
67- if ( ! ( "permission" in Notification ) ) {
68- Notification . permission = permission ;
69- }
70- } ) ;
71- }
51+ if ( this . options . mode === "append" ) {
52+ this . el . insertAdjacentHTML ( "beforeend" , data ) ;
53+ } else {
54+ this . el . innerHTML = data ;
55+ }
56+ registry . scan ( this . el ) ;
57+ } ,
7258
73- // Let's check if the user is okay to get some notification
74- if ( Notification . permission === "granted" ) {
75- const response = await fetch ( this . options . url ) ;
76- const data = await response . json ( ) ;
59+ async desktop_notification ( ) {
60+ // Let's check if the browser supports notifications
61+ if ( ! ( "Notification" in window ) ) {
62+ logger . error ( "This browser does not support notifications." ) ;
63+ return ;
64+ }
7765
78- if ( data . length === 0 ) {
79- return ;
66+ // Notifications need to be granted.
67+ // Note: Current browsers don't allow an automatic request for
68+ // permission but need an interaction to allow it.
69+ // The following code won't work out of the box in such cases.
70+ if ( ! ( Notification . permission in [ "denied" , "granted" ] ) ) {
71+ Notification . requestPermission ( ( permission ) => {
72+ // Whatever the user answers, we make sure Chrome stores the information
73+ if ( ! ( "permission" in Notification ) ) {
74+ Notification . permission = permission ;
8075 }
76+ } ) ;
77+ }
8178
82- for ( const message of data ) {
83- const notification = new Notification ( message . title , message ) ;
84- if ( message . data ?. url ) {
85- // If ``message.data`` contains an URL, open it.
86- // If not, the browser will simply get the focus when
87- // clicking on the desktop notification.
88- notification . addEventListener ( "click" , ( ) => {
89- window . open ( message . data . url , "_self" ) ;
90-
91- // Opening a URL in a new tab from JavaScript is
92- // equivalent to opening a popup window which is
93- // blocked by default.
94- // Therefore, we open the URL in the same tab and
95- // rely on other mechanisms that the user's work
96- // isn't lost (auto-save, form unlod protection.)
97-
98- // Don't focus the originating tab.
99- //e.preventDefault();
100- // Open in new window.
101- //const new_window = window.open(message.data.url, "_blank");
102- //try {
103- // new_window.focus();
104- //} catch (e) {
105- // // Opening of new window (aka "popup") might
106- // // have been blocked.
107- // window.focus();
108- //}
109- } ) ;
110- }
111- }
112- }
79+ // Let's check if the user is okay to get some notification
80+ if ( Notification . permission !== "granted" ) {
81+ return ;
82+ }
83+
84+ let data = [ ] ;
85+ try {
86+ const response = await fetch ( this . options . url ) ;
87+ data = await response . json ( ) ;
11388 } catch ( e ) {
11489 logger . error (
11590 `Could not fetch from ${ this . options . url } on push-id ${ this . options . pushId } .`
11691 ) ;
11792 }
93+
94+ if ( ! data ?. length ) {
95+ return ;
96+ }
97+
98+ for ( const message of data ) {
99+ const notification = new Notification ( message . title , message ) ;
100+ if ( message . data ?. url ) {
101+ // If ``message.data`` contains an URL, open it.
102+ // If not, the browser will simply get the focus when
103+ // clicking on the desktop notification.
104+ notification . addEventListener ( "click" , ( ) => {
105+ window . open ( message . data . url , "_self" ) ;
106+
107+ // Opening a URL in a new tab from JavaScript is
108+ // equivalent to opening a popup window which is
109+ // blocked by default.
110+ // Therefore, we open the URL in the same tab and
111+ // rely on other mechanisms that the user's work
112+ // isn't lost (auto-save, form unload protection.)
113+
114+ // Don't focus the originating tab.
115+ //e.preventDefault();
116+ // Open in new window.
117+ //const new_window = window.open(message.data.url, "_blank");
118+ //try {
119+ // new_window.focus();
120+ //} catch (e) {
121+ // // Opening of new window (aka "popup") might
122+ // // have been blocked.
123+ // window.focus();
124+ //}
125+ } ) ;
126+ }
127+ }
118128 } ,
119129} ) ;
0 commit comments