Add desktop notification permission granting setting and notice.

This commit is contained in:
Mayhem 2013-08-17 22:37:30 +02:00
parent 43fbf62da0
commit 5742405f67
5 changed files with 48 additions and 5 deletions

View File

@ -1,9 +1,21 @@
Polyfill =
init: ->
<% if (type === 'crx') { %>
Polyfill.toBlob()
Polyfill.visibility()
@notificationPermission()
@toBlob()
@visibility()
<% } %>
notificationPermission: ->
return if window.Notification and 'permission' of Notification
Object.defineProperty Notification, 'permission',
get: ->
switch webkitNotifications.checkPermission()
when 0
'granted'
when 1
'default'
when 2
'denied'
toBlob: ->
HTMLCanvasElement::toBlob or= (cb) ->
data = atob @toDataURL()[22..]

View File

@ -2,6 +2,7 @@ Config =
main:
'Miscellaneous':
'Enable 4chan\'s Extension': [false, 'Compatibility between <%= meta.name %> and 4chan\'s inline extension is NOT guaranteed.']
'Desktop Notifications': [true, 'Enables desktop notifications across various <%= meta.name %> features.']
'Announcement Hiding': [true, 'Add button to hide 4chan announcements.']
'404 Redirect': [true, 'Redirect dead threads and images.']
'Keybinds': [true, 'Bind actions to keyboard shortcuts.']

View File

@ -89,6 +89,8 @@ Header =
Header.setCatalogLinks Conf['Header catalog links']
$.sync 'Header catalog links', Header.setCatalogLinks
@enableDesktopNotifications()
setBoardList: ->
nav = $.id 'boardNavDesktop'
if a = $ "a[href*='/#{g.BOARD}/']", nav
@ -268,3 +270,31 @@ Header =
{type, content, lifetime, cb} = e.detail
notif = new Notice type, content, lifetime
cb notif if cb
areNotificationsEnabled: false
enableDesktopNotifications: ->
return unless window.Notification and Conf['Desktop Notifications']
switch Notification.permission
when 'granted'
Header.areNotificationsEnabled = true
return
when 'denied'
# requestPermission doesn't work if status is 'denied',
# but it'll still work if status is 'default'.
return
el = $.el 'span',
innerHTML: """
Desktop notification permissions are not granted:<br>
<button>Authorize</button> or <button>Disable</button>
"""
[authorize, disable] = $$ 'button', el
$.on authorize, 'click', ->
Notification.requestPermission (status) ->
Header.areNotificationsEnabled = status is 'granted'
return if status is 'default'
notice.close()
$.on disable, 'click', ->
$.set 'Desktop Notifications', false
notice.close()
notice = new Notice 'info', el

View File

@ -96,12 +96,12 @@ Unread =
Unread.openNotification post
return
openNotification: (post) ->
return unless d.hidden
return unless d.hidden and Header.areNotificationsEnabled
name = if Conf['Anonymize']
'Anonymous'
else
$('.nameBlock', post.nodes.info).textContent.trim()
notif = new Notification "#{name} replied to you.",
notif = new Notification "#{name} replied to you",
body: post.info.comment
icon: Favicon.logo
notif.onclick = ->

View File

@ -113,7 +113,7 @@ QR =
QR.captcha.nodes.input.focus()
notice = new Notice 'warning', el
QR.notifications.push notice
return unless d.hidden
return unless d.hidden and Header.areNotificationsEnabled
notif = new Notification 'Quick reply warning',
body: el.textContent
icon: Favicon.logo