nodebb-plugin-web-push 0.6.2 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/library.js +6 -0
- package/package.json +2 -2
- package/plugin.json +5 -1
- package/static/web-push.js +56 -0
- package/static/samplefile.html +0 -5
package/library.js
CHANGED
|
@@ -45,6 +45,12 @@ plugin.appendConfig = async (config) => {
|
|
|
45
45
|
return config;
|
|
46
46
|
};
|
|
47
47
|
|
|
48
|
+
plugin.registerServiceWorker = async (data) => {
|
|
49
|
+
const { scripts } = data;
|
|
50
|
+
scripts.add(`nodebb-plugin-web-push/static/web-push.js`);
|
|
51
|
+
return data;
|
|
52
|
+
};
|
|
53
|
+
|
|
48
54
|
async function assertVapidConfiguration() {
|
|
49
55
|
let { publicKey, privateKey } = await meta.settings.get('web-push');
|
|
50
56
|
if (!publicKey || !privateKey) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nodebb-plugin-web-push",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "A starter kit for quickly creating NodeBB plugins",
|
|
5
5
|
"main": "library.js",
|
|
6
6
|
"repository": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"readmeFilename": "README.md",
|
|
33
33
|
"nbbpm": {
|
|
34
|
-
"compatibility": "^
|
|
34
|
+
"compatibility": "^3.10.x"
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@commitlint/cli": "19.5.0",
|
package/plugin.json
CHANGED
|
@@ -9,12 +9,16 @@
|
|
|
9
9
|
{ "hook": "filter:config.get", "method": "appendConfig" },
|
|
10
10
|
{ "hook": "action:notification.pushed", "method": "onNotificationPush" },
|
|
11
11
|
{ "hook": "static:notifications.rescind", "method": "onNotificationRescind" },
|
|
12
|
-
{ "hook": "filter:user.profileMenu", "method": "addProfileItem" }
|
|
12
|
+
{ "hook": "filter:user.profileMenu", "method": "addProfileItem" },
|
|
13
|
+
{ "hook": "filter:service-worker.scripts", "method": "registerServiceWorker" }
|
|
13
14
|
],
|
|
14
15
|
"languages": "public/languages",
|
|
15
16
|
"modules": {
|
|
16
17
|
"../client/account/web-push.js": "./public/lib/settings.js",
|
|
17
18
|
"../admin/plugins/web-push.js": "./public/lib/admin.js"
|
|
18
19
|
},
|
|
20
|
+
"staticDirs": {
|
|
21
|
+
"static": "./static"
|
|
22
|
+
},
|
|
19
23
|
"templates": "templates"
|
|
20
24
|
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/* eslint-disable no-undef */
|
|
2
|
+
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
// Register event listener for the 'push' event.
|
|
6
|
+
self.addEventListener('push', (event) => {
|
|
7
|
+
// Keep the service worker alive until the notification is created.
|
|
8
|
+
const { title, body, tag, data } = event.data.json();
|
|
9
|
+
|
|
10
|
+
if (title && body) {
|
|
11
|
+
const { icon } = data;
|
|
12
|
+
delete data.icon;
|
|
13
|
+
const { badge } = data;
|
|
14
|
+
delete data.badge;
|
|
15
|
+
|
|
16
|
+
event.waitUntil(
|
|
17
|
+
self.registration.showNotification(title, { body, tag, data, icon, badge })
|
|
18
|
+
);
|
|
19
|
+
} else if (tag) {
|
|
20
|
+
event.waitUntil(
|
|
21
|
+
self.registration.getNotifications({ tag }).then((notifications) => {
|
|
22
|
+
notifications.forEach((notification) => {
|
|
23
|
+
notification.close();
|
|
24
|
+
});
|
|
25
|
+
})
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
self.addEventListener('notificationclick', (event) => {
|
|
31
|
+
event.notification.close();
|
|
32
|
+
let target;
|
|
33
|
+
if (event.notification.data && event.notification.data.url) {
|
|
34
|
+
target = new URL(event.notification.data.url);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// This looks to see if the current is already open and focuses if it is
|
|
38
|
+
event.waitUntil(
|
|
39
|
+
self.clients
|
|
40
|
+
.matchAll({ type: 'window' })
|
|
41
|
+
.then((clientList) => {
|
|
42
|
+
// eslint-disable-next-line no-restricted-syntax
|
|
43
|
+
for (const client of clientList) {
|
|
44
|
+
const { hostname } = new URL(client.url);
|
|
45
|
+
if (target && hostname === target.hostname && 'focus' in client) {
|
|
46
|
+
client.postMessage({
|
|
47
|
+
action: 'ajaxify',
|
|
48
|
+
url: target.pathname,
|
|
49
|
+
});
|
|
50
|
+
return client.focus();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (self.clients.openWindow) return self.clients.openWindow(target.pathname);
|
|
54
|
+
})
|
|
55
|
+
);
|
|
56
|
+
});
|