@zoho_apptics/apptics-js-sdk 2.0.0-beta.1 → 2.0.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/CHANGELOG.md +127 -16
- package/README.md +357 -215
- package/dist/apptics-pn-sw.js +3 -0
- package/dist/apptics-pn-sw.js.map +1 -0
- package/dist/apptics.esm.min.js +2 -2
- package/dist/apptics.esm.min.js.map +1 -1
- package/dist/apptics.min.js +2 -2
- package/dist/apptics.min.js.map +1 -1
- package/dist/chunks/batch-Axq9sTzR.min.js +3 -0
- package/dist/chunks/batch-Axq9sTzR.min.js.map +1 -0
- package/dist/chunks/lifecycle-Cvg78jN_.min.js +3 -0
- package/dist/chunks/lifecycle-Cvg78jN_.min.js.map +1 -0
- package/dist/chunks/send-DBpesydB.min.js +3 -0
- package/dist/chunks/send-DBpesydB.min.js.map +1 -0
- package/dist/chunks/transport-B21X39p4.min.js +3 -0
- package/dist/chunks/transport-B21X39p4.min.js.map +1 -0
- package/dist/cjs/core/attempts.js +1 -1
- package/dist/cjs/core/batch.js +4 -4
- package/dist/cjs/core/consent.d.ts +17 -4
- package/dist/cjs/core/consent.js +81 -14
- package/dist/cjs/core/console-call.d.ts +23 -0
- package/dist/cjs/core/console-call.js +63 -0
- package/dist/cjs/core/constants.d.ts +66 -17
- package/dist/cjs/core/constants.js +76 -19
- package/dist/cjs/core/db.js +31 -7
- package/dist/cjs/core/debug.d.ts +6 -4
- package/dist/cjs/core/debug.js +36 -12
- package/dist/cjs/core/env.d.ts +0 -3
- package/dist/cjs/core/env.js +1 -9
- package/dist/cjs/core/framework-id.d.ts +1 -1
- package/dist/cjs/core/framework-id.js +3 -3
- package/dist/cjs/core/guard.js +19 -4
- package/dist/cjs/core/hooks.d.ts +29 -0
- package/dist/cjs/core/hooks.js +12 -1
- package/dist/cjs/core/locks.js +1 -1
- package/dist/cjs/core/logger.js +1 -1
- package/dist/cjs/core/shield.d.ts +31 -0
- package/dist/cjs/core/shield.js +129 -0
- package/dist/cjs/core/state.d.ts +11 -5
- package/dist/cjs/core/state.js +4 -4
- package/dist/cjs/core/storage.d.ts +7 -0
- package/dist/cjs/core/storage.js +28 -9
- package/dist/cjs/core/stylesheet.js +1 -1
- package/dist/cjs/core/transport.d.ts +15 -4
- package/dist/cjs/core/transport.js +72 -25
- package/dist/cjs/core/utils.js +1 -1
- package/dist/cjs/core/validate.js +6 -5
- package/dist/cjs/device/appversion.d.ts +10 -1
- package/dist/cjs/device/appversion.js +26 -3
- package/dist/cjs/device/index.d.ts +17 -2
- package/dist/cjs/device/index.js +251 -35
- package/dist/cjs/engagement/api-rules.js +1 -1
- package/dist/cjs/engagement/auto-api.js +4 -3
- package/dist/cjs/engagement/auto-screen.js +10 -6
- package/dist/cjs/engagement/defined-events.d.ts +2 -3
- package/dist/cjs/engagement/defined-events.js +10 -16
- package/dist/cjs/engagement/send.js +7 -3
- package/dist/cjs/engagement/session.d.ts +17 -0
- package/dist/cjs/engagement/session.js +154 -23
- package/dist/cjs/engagement/track.d.ts +4 -4
- package/dist/cjs/engagement/track.js +23 -17
- package/dist/cjs/errors/send.js +2 -2
- package/dist/cjs/errors/track.d.ts +2 -1
- package/dist/cjs/errors/track.js +8 -5
- package/dist/cjs/feedback/controls.d.ts +36 -4
- package/dist/cjs/feedback/controls.js +302 -67
- package/dist/cjs/feedback/i18n/index.d.ts +31 -0
- package/dist/cjs/feedback/i18n/index.js +187 -0
- package/dist/cjs/feedback/i18n/strings.d.ts +9 -0
- package/dist/cjs/feedback/i18n/strings.js +8 -0
- package/dist/cjs/feedback/index.js +1 -1
- package/dist/cjs/feedback/logs.d.ts +3 -6
- package/dist/cjs/feedback/logs.js +12 -21
- package/dist/cjs/feedback/quartz.js +5 -2
- package/dist/cjs/feedback/send.d.ts +6 -3
- package/dist/cjs/feedback/send.js +59 -41
- package/dist/cjs/feedback/styles.js +2 -2
- package/dist/cjs/feedback/template.d.ts +4 -2
- package/dist/cjs/feedback/template.js +73 -44
- package/dist/cjs/feedback/ui.d.ts +47 -4
- package/dist/cjs/feedback/ui.js +351 -97
- package/dist/cjs/heatmap/send.d.ts +2 -0
- package/dist/cjs/heatmap/send.js +3 -5
- package/dist/cjs/heatmap/track.js +13 -9
- package/dist/cjs/index.d.ts +1 -2
- package/dist/cjs/index.js +55 -18
- package/dist/cjs/lifecycle.d.ts +13 -2
- package/dist/cjs/lifecycle.js +233 -118
- package/dist/cjs/pushnotification/display.d.ts +65 -0
- package/dist/cjs/pushnotification/display.js +112 -0
- package/dist/cjs/pushnotification/index.d.ts +20 -0
- package/dist/cjs/pushnotification/index.js +96 -0
- package/dist/cjs/pushnotification/kept-opens.d.ts +23 -0
- package/dist/cjs/pushnotification/kept-opens.js +64 -0
- package/dist/cjs/pushnotification/notify.d.ts +45 -0
- package/dist/cjs/pushnotification/notify.js +476 -0
- package/dist/cjs/pushnotification/payload.d.ts +65 -0
- package/dist/cjs/pushnotification/payload.js +279 -0
- package/dist/cjs/pushnotification/session.d.ts +46 -0
- package/dist/cjs/pushnotification/session.js +413 -0
- package/dist/cjs/pushnotification/stats.d.ts +45 -0
- package/dist/cjs/pushnotification/stats.js +244 -0
- package/dist/cjs/pushnotification/wms.d.ts +30 -0
- package/dist/cjs/pushnotification/wms.js +374 -0
- package/dist/cjs/remoteconfig/country.js +1 -1
- package/dist/cjs/remoteconfig/custom.js +4 -3
- package/dist/cjs/remoteconfig/evaluate.js +1 -1
- package/dist/cjs/remoteconfig/fetch.d.ts +17 -10
- package/dist/cjs/remoteconfig/fetch.js +146 -32
- package/dist/cjs/remoteconfig/index.d.ts +9 -5
- package/dist/cjs/remoteconfig/index.js +24 -32
- package/dist/cjs/remoteconfig/store.d.ts +15 -7
- package/dist/cjs/remoteconfig/store.js +68 -35
- package/dist/cjs/remoteconfig/tz-countries.js +1 -1
- package/dist/cjs/{core → remotelog}/callsite.d.ts +5 -4
- package/dist/cjs/{core → remotelog}/callsite.js +6 -5
- package/dist/cjs/remotelog/gate.js +1 -1
- package/dist/cjs/remotelog/logger.js +2 -2
- package/dist/cjs/remotelog/send.js +6 -5
- package/dist/cjs/remotelogger/index.js +1 -1
- package/dist/cjs/scheduler.d.ts +3 -1
- package/dist/cjs/scheduler.js +34 -25
- package/dist/cjs/sw/apptics-pn-sw.d.ts +83 -0
- package/dist/cjs/sw/timers.d.ts +44 -0
- package/dist/cjs/types.d.ts +90 -29
- package/dist/cjs/user/index.d.ts +7 -0
- package/dist/cjs/user/index.js +25 -3
- package/dist/esm/core/attempts.js +1 -1
- package/dist/esm/core/batch.js +4 -4
- package/dist/esm/core/consent.d.ts +17 -4
- package/dist/esm/core/consent.js +84 -20
- package/dist/esm/core/console-call.d.ts +23 -0
- package/dist/esm/core/console-call.js +60 -0
- package/dist/esm/core/constants.d.ts +66 -17
- package/dist/esm/core/constants.js +73 -20
- package/dist/esm/core/db.js +31 -7
- package/dist/esm/core/debug.d.ts +6 -4
- package/dist/esm/core/debug.js +37 -13
- package/dist/esm/core/env.d.ts +0 -3
- package/dist/esm/core/env.js +2 -9
- package/dist/esm/core/framework-id.d.ts +1 -1
- package/dist/esm/core/framework-id.js +3 -3
- package/dist/esm/core/guard.js +19 -4
- package/dist/esm/core/hooks.d.ts +29 -0
- package/dist/esm/core/hooks.js +12 -1
- package/dist/esm/core/locks.js +1 -1
- package/dist/esm/core/logger.js +1 -1
- package/dist/esm/core/shield.d.ts +31 -0
- package/dist/esm/core/shield.js +119 -0
- package/dist/esm/core/state.d.ts +11 -5
- package/dist/esm/core/state.js +4 -4
- package/dist/esm/core/storage.d.ts +7 -0
- package/dist/esm/core/storage.js +28 -11
- package/dist/esm/core/stylesheet.js +1 -1
- package/dist/esm/core/transport.d.ts +15 -4
- package/dist/esm/core/transport.js +73 -28
- package/dist/esm/core/utils.js +1 -1
- package/dist/esm/core/validate.js +6 -5
- package/dist/esm/device/appversion.d.ts +10 -1
- package/dist/esm/device/appversion.js +26 -4
- package/dist/esm/device/index.d.ts +17 -2
- package/dist/esm/device/index.js +255 -42
- package/dist/esm/engagement/api-rules.js +1 -1
- package/dist/esm/engagement/auto-api.js +4 -3
- package/dist/esm/engagement/auto-screen.js +10 -6
- package/dist/esm/engagement/defined-events.d.ts +2 -3
- package/dist/esm/engagement/defined-events.js +10 -16
- package/dist/esm/engagement/send.js +7 -3
- package/dist/esm/engagement/session.d.ts +17 -0
- package/dist/esm/engagement/session.js +152 -24
- package/dist/esm/engagement/track.d.ts +4 -4
- package/dist/esm/engagement/track.js +24 -18
- package/dist/esm/errors/send.js +2 -2
- package/dist/esm/errors/track.d.ts +2 -1
- package/dist/esm/errors/track.js +8 -5
- package/dist/esm/feedback/controls.d.ts +36 -4
- package/dist/esm/feedback/controls.js +298 -68
- package/dist/esm/feedback/i18n/index.d.ts +31 -0
- package/dist/esm/feedback/i18n/index.js +178 -0
- package/dist/esm/feedback/i18n/strings.d.ts +9 -0
- package/dist/esm/feedback/i18n/strings.js +4 -0
- package/dist/esm/feedback/index.js +1 -1
- package/dist/esm/feedback/logs.d.ts +3 -6
- package/dist/esm/feedback/logs.js +12 -21
- package/dist/esm/feedback/quartz.js +5 -2
- package/dist/esm/feedback/send.d.ts +6 -3
- package/dist/esm/feedback/send.js +61 -44
- package/dist/esm/feedback/styles.js +2 -2
- package/dist/esm/feedback/template.d.ts +4 -2
- package/dist/esm/feedback/template.js +73 -44
- package/dist/esm/feedback/ui.d.ts +47 -4
- package/dist/esm/feedback/ui.js +353 -99
- package/dist/esm/heatmap/send.d.ts +2 -0
- package/dist/esm/heatmap/send.js +3 -5
- package/dist/esm/heatmap/track.js +13 -9
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +58 -21
- package/dist/esm/lifecycle.d.ts +13 -2
- package/dist/esm/lifecycle.js +238 -125
- package/dist/esm/pushnotification/display.d.ts +65 -0
- package/dist/esm/pushnotification/display.js +95 -0
- package/dist/esm/pushnotification/index.d.ts +20 -0
- package/dist/esm/pushnotification/index.js +91 -0
- package/dist/esm/pushnotification/kept-opens.d.ts +23 -0
- package/dist/esm/pushnotification/kept-opens.js +62 -0
- package/dist/esm/pushnotification/notify.d.ts +45 -0
- package/dist/esm/pushnotification/notify.js +469 -0
- package/dist/esm/pushnotification/payload.d.ts +65 -0
- package/dist/esm/pushnotification/payload.js +272 -0
- package/dist/esm/pushnotification/session.d.ts +46 -0
- package/dist/esm/pushnotification/session.js +402 -0
- package/dist/esm/pushnotification/stats.d.ts +45 -0
- package/dist/esm/pushnotification/stats.js +238 -0
- package/dist/esm/pushnotification/wms.d.ts +30 -0
- package/dist/esm/pushnotification/wms.js +368 -0
- package/dist/esm/remoteconfig/country.js +1 -1
- package/dist/esm/remoteconfig/custom.js +4 -3
- package/dist/esm/remoteconfig/evaluate.js +1 -1
- package/dist/esm/remoteconfig/fetch.d.ts +17 -10
- package/dist/esm/remoteconfig/fetch.js +147 -33
- package/dist/esm/remoteconfig/index.d.ts +9 -5
- package/dist/esm/remoteconfig/index.js +24 -32
- package/dist/esm/remoteconfig/store.d.ts +15 -7
- package/dist/esm/remoteconfig/store.js +68 -36
- package/dist/esm/remoteconfig/tz-countries.js +1 -1
- package/dist/esm/{core → remotelog}/callsite.d.ts +5 -4
- package/dist/esm/{core → remotelog}/callsite.js +6 -5
- package/dist/esm/remotelog/gate.js +1 -1
- package/dist/esm/remotelog/logger.js +2 -2
- package/dist/esm/remotelog/send.js +6 -5
- package/dist/esm/remotelogger/index.js +1 -1
- package/dist/esm/scheduler.d.ts +3 -1
- package/dist/esm/scheduler.js +35 -27
- package/dist/esm/sw/apptics-pn-sw.d.ts +83 -0
- package/dist/esm/sw/timers.d.ts +44 -0
- package/dist/esm/types.d.ts +90 -29
- package/dist/esm/user/index.d.ts +7 -0
- package/dist/esm/user/index.js +26 -5
- package/dist/feedback.esm.min.js +2 -2
- package/dist/feedback.esm.min.js.map +1 -1
- package/dist/pushnotification.esm.min.js +3 -0
- package/dist/pushnotification.esm.min.js.map +1 -0
- package/dist/remoteconfig.esm.min.js +3 -0
- package/dist/remoteconfig.esm.min.js.map +1 -0
- package/dist/remotelogger.esm.min.js +3 -0
- package/dist/remotelogger.esm.min.js.map +1 -0
- package/package.json +17 -3
- package/dist/chunks/lifecycle-VYr43KcB.min.js +0 -3
- package/dist/chunks/lifecycle-VYr43KcB.min.js.map +0 -1
- package/dist/cjs/auto-init.d.ts +0 -8
- package/dist/cjs/auto-init.js +0 -32
- package/dist/esm/auto-init.d.ts +0 -8
- package/dist/esm/auto-init.js +0 -29
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
/*! @zoho_apptics/apptics-js-sdk v2.0.0 | (c) Zoho Corporation Private Limited | see LICENSE */
|
|
2
|
+
import { Console, warnAlways } from '../core/logger.js';
|
|
3
|
+
import '../core/state.js';
|
|
4
|
+
import { webNotificationFrom } from './payload.js';
|
|
5
|
+
import { recordStat, STATUS, awaitConversion } from './stats.js';
|
|
6
|
+
import { CLICK_MESSAGE, HANDOFF_MESSAGE, isOurs, CLOSE_MESSAGE, STEP_MESSAGE, stepIndex, titleFor, optionsFor, STOP_TIMERS_MESSAGE, TIMERS_TAKEN, TIMERS_MESSAGE } from './display.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The browser notification shown for a push message, while a tab of the site is open.
|
|
10
|
+
*
|
|
11
|
+
* With the site's /apptics-pn-sw.js (a one-line file that imports apptics-pn-sw.js from the CDN),
|
|
12
|
+
* it is shown through the worker: action buttons, the image carousel with Prev / Next, and clicks
|
|
13
|
+
* handled even after the tab has closed. What is timed (auto-scroll, the countdown, auto-dismiss)
|
|
14
|
+
* is handed to the worker, which runs it while this tab is in the background or closed. A worker
|
|
15
|
+
* that doesn't take it within TAKE_TIMEOUT_MS (an older one), and what the worker gives back after
|
|
16
|
+
* its few minutes, the page runs itself while it is open.
|
|
17
|
+
*
|
|
18
|
+
* Without one (not configured, unsupported, or it failed to register), a plain notification
|
|
19
|
+
* shows: the title, text, icon, first image, the countdown as it was when shown, auto-dismiss,
|
|
20
|
+
* stay-on-screen and silent; no buttons.
|
|
21
|
+
*
|
|
22
|
+
* The SDK never asks for permission by itself, since browsers ignore a prompt without a user
|
|
23
|
+
* gesture and visitors deny the ones that come unasked: the app calls requestPermission() from
|
|
24
|
+
* its own button. Without permission, no notification shows (stats are still recorded).
|
|
25
|
+
*/
|
|
26
|
+
const LABEL = 'AppticsPushNotification'; // no i18n
|
|
27
|
+
var clickListeners = [];
|
|
28
|
+
var warnedConstructor = false;
|
|
29
|
+
var warnedWorker = false;
|
|
30
|
+
var workerPromise = null;
|
|
31
|
+
var listening = false;
|
|
32
|
+
/** The timers of each notification on screen, by tag. */
|
|
33
|
+
var running = {};
|
|
34
|
+
/** How a url on this site is opened: a seam, since a test page cannot navigate. */
|
|
35
|
+
const _openUrlForTest = {
|
|
36
|
+
assign: function (url) {
|
|
37
|
+
window.location.assign(url);
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
function api() {
|
|
41
|
+
return typeof Notification === 'function' ? Notification : undefined; // no i18n
|
|
42
|
+
}
|
|
43
|
+
/** Asks the browser for permission to show notifications; resolves with its answer. */
|
|
44
|
+
function requestPermission() {
|
|
45
|
+
var N = api();
|
|
46
|
+
if (!N || typeof N.requestPermission !== 'function') { // no i18n
|
|
47
|
+
return Promise.resolve('unsupported'); // no i18n
|
|
48
|
+
}
|
|
49
|
+
return new Promise(function (resolve) {
|
|
50
|
+
try {
|
|
51
|
+
// older Safari answers through a callback rather than a Promise
|
|
52
|
+
var asked = N.requestPermission(function (answer) { resolve(answer); });
|
|
53
|
+
if (asked && typeof asked.then === 'function') { // no i18n
|
|
54
|
+
asked.then(resolve, function () { resolve(N.permission); });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (err) {
|
|
58
|
+
resolve(N.permission);
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A notification was clicked (a button's action id, or '' for the body): the opened stat, and the
|
|
64
|
+
* conversion event it waits for from now.
|
|
65
|
+
*/
|
|
66
|
+
function opened(data, action, run) {
|
|
67
|
+
var view = webNotificationFrom(data);
|
|
68
|
+
var target = { id: view.id, number: view.number, run: run || undefined };
|
|
69
|
+
recordStat(target, STATUS.OPENED, action || undefined);
|
|
70
|
+
if (view.conversion) {
|
|
71
|
+
awaitConversion(target, view.conversion);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function reportClick(click) {
|
|
75
|
+
clickListeners.slice().forEach(function (fn) {
|
|
76
|
+
try {
|
|
77
|
+
fn(click);
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
Console.warn(LABEL + ': a notification click listener threw', err); // no i18n
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function container() {
|
|
85
|
+
var nav = typeof navigator !== 'undefined' ? navigator : undefined; // no i18n
|
|
86
|
+
return nav && nav.serviceWorker ? nav.serviceWorker : undefined;
|
|
87
|
+
}
|
|
88
|
+
/** What the worker posts to this tab: clicks for the app, and closes and steps that stop timers. */
|
|
89
|
+
function onWorkerMessage(event) {
|
|
90
|
+
var m = event && event.data;
|
|
91
|
+
if (!m || typeof m !== 'object' || typeof m.type !== 'string') { // no i18n
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
if (m.type === CLICK_MESSAGE) {
|
|
95
|
+
stop(String(m.tag || '')); // no i18n
|
|
96
|
+
opened(m.data && typeof m.data === 'object' ? m.data : {}, typeof m.action === 'string' ? m.action : '', typeof m.run === 'string' ? m.run : undefined); // no i18n
|
|
97
|
+
reportClick({
|
|
98
|
+
action: typeof m.action === 'string' ? m.action : '', // no i18n
|
|
99
|
+
appticsPnID: typeof m.appticsPnID === 'string' ? m.appticsPnID : String(m.appticsPnID || ''), // no i18n
|
|
100
|
+
additional: m.additional && typeof m.additional === 'object' ? m.additional : {}, // no i18n
|
|
101
|
+
data: m.data && typeof m.data === 'object' ? m.data : {}, // no i18n
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
else if (m.type === HANDOFF_MESSAGE && isOurs(m.shown)) {
|
|
105
|
+
// what the worker could not finish in its time: this tab runs the rest
|
|
106
|
+
var rest = m.shown;
|
|
107
|
+
serviceWorkerReady().then(function (registration) {
|
|
108
|
+
if (registration) {
|
|
109
|
+
runPageTimers(registration, rest);
|
|
110
|
+
}
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
else if (m.type === CLOSE_MESSAGE) {
|
|
114
|
+
stop(String(m.tag || '')); // no i18n
|
|
115
|
+
}
|
|
116
|
+
else if (m.type === STEP_MESSAGE) {
|
|
117
|
+
var tag = String(m.tag || ''); // no i18n
|
|
118
|
+
stopScroll(tag);
|
|
119
|
+
// the slide the visitor moved to: the countdown's next tick shows that one
|
|
120
|
+
var r = running[tag];
|
|
121
|
+
if (r && typeof m.index === 'number' && m.index >= 0 && m.index < r.shown.view.images.length) { // no i18n
|
|
122
|
+
r.shown = { apptics: 1, view: r.shown.view, index: m.index, tag: tag, message: r.shown.message, run: r.shown.run };
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Listens to the worker from the start: a click on a notification shown by an earlier tab reaches
|
|
128
|
+
* whichever tab is open now, including the one the click opened.
|
|
129
|
+
*/
|
|
130
|
+
function listen() {
|
|
131
|
+
var c = container();
|
|
132
|
+
if (listening || !c || typeof c.addEventListener !== 'function') { // no i18n
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
listening = true;
|
|
136
|
+
c.addEventListener('message', onWorkerMessage); // no i18n
|
|
137
|
+
}
|
|
138
|
+
/** The Apptics service worker: the site's /apptics-pn-sw.js, which imports apptics-pn-sw.js. */
|
|
139
|
+
const SERVICE_WORKER = '/apptics-pn-sw.js'; // no i18n
|
|
140
|
+
/** Whether a notification can go through a worker here: the browser has service workers. */
|
|
141
|
+
function workerWanted() {
|
|
142
|
+
var c = container();
|
|
143
|
+
return !!c && typeof c.register === 'function'; // no i18n
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The site's /apptics-pn-sw.js registration, or null when there is none: the browser has no service
|
|
147
|
+
* workers, the site has no /apptics-pn-sw.js (a plain notification shows; only logged, as many sites
|
|
148
|
+
* won't have it), or another worker of the site's controls its root. Registered once.
|
|
149
|
+
*/
|
|
150
|
+
function serviceWorkerReady() {
|
|
151
|
+
var c = container();
|
|
152
|
+
if (!c || typeof c.register !== 'function') { // no i18n
|
|
153
|
+
return Promise.resolve(null);
|
|
154
|
+
}
|
|
155
|
+
if (workerPromise) {
|
|
156
|
+
return workerPromise;
|
|
157
|
+
}
|
|
158
|
+
var path = SERVICE_WORKER;
|
|
159
|
+
listen();
|
|
160
|
+
var failed = function (reason) {
|
|
161
|
+
if (!warnedWorker) {
|
|
162
|
+
warnedWorker = true;
|
|
163
|
+
Console.log(LABEL + ': the service worker ' + path + ' ' + reason + ', so notifications show without buttons or a carousel'); // no i18n
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
166
|
+
};
|
|
167
|
+
workerPromise = findRegistrations(c, path).then(function (found) {
|
|
168
|
+
// the site's own registration of this file is used as it is: registering it again would
|
|
169
|
+
// change its scope or its update settings
|
|
170
|
+
if (found.ours) {
|
|
171
|
+
return found.ours;
|
|
172
|
+
}
|
|
173
|
+
// a worker at the root replaces whatever worker controlled it: never the site's own
|
|
174
|
+
if (found.taken) {
|
|
175
|
+
return failed('was not registered, since another of the site\'s service workers controls its root'); // no i18n
|
|
176
|
+
}
|
|
177
|
+
return c.register(path);
|
|
178
|
+
}).then(function (registration) {
|
|
179
|
+
return registration ? activated(registration) : null;
|
|
180
|
+
}).then(function (registration) {
|
|
181
|
+
return registration && typeof registration.showNotification === 'function' ? registration : failed('did not activate'); // no i18n
|
|
182
|
+
}, function (err) {
|
|
183
|
+
return failed('did not register (' + String(err && err.message || err) + ')'); // no i18n
|
|
184
|
+
});
|
|
185
|
+
return workerPromise;
|
|
186
|
+
}
|
|
187
|
+
/** How long a new worker may take to install and activate before notifications show without it. */
|
|
188
|
+
const ACTIVATE_TIMEOUT_MS = 10000;
|
|
189
|
+
/** The site's registration of `path`, and whether another worker already holds the root scope. */
|
|
190
|
+
function findRegistrations(c, path) {
|
|
191
|
+
if (typeof c.getRegistrations !== 'function') { // no i18n
|
|
192
|
+
return Promise.resolve({ ours: null, taken: false });
|
|
193
|
+
}
|
|
194
|
+
var wanted = new URL(path, window.location.href).href;
|
|
195
|
+
var root = new URL('/', window.location.href).href; // no i18n
|
|
196
|
+
var isOurs = function (r) {
|
|
197
|
+
return [r.active, r.waiting, r.installing].some(function (w) { return !!w && w.scriptURL === wanted; });
|
|
198
|
+
};
|
|
199
|
+
return c.getRegistrations().then(function (list) {
|
|
200
|
+
var ours = list.filter(isOurs)[0] || null;
|
|
201
|
+
var taken = !ours && list.some(function (r) { return r.scope === root && !isOurs(r); });
|
|
202
|
+
return { ours: ours, taken: taken };
|
|
203
|
+
}, function () { return { ours: null, taken: false }; });
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* `registration` once its worker is active: the one register() gave, not navigator.serviceWorker
|
|
207
|
+
* .ready, which answers with whichever worker controls this page and may be the site's own.
|
|
208
|
+
*/
|
|
209
|
+
function activated(registration) {
|
|
210
|
+
if (!registration) {
|
|
211
|
+
return Promise.resolve(null);
|
|
212
|
+
}
|
|
213
|
+
if (registration.active) {
|
|
214
|
+
return Promise.resolve(registration);
|
|
215
|
+
}
|
|
216
|
+
var worker = registration.installing || registration.waiting;
|
|
217
|
+
if (!worker) {
|
|
218
|
+
return Promise.resolve(null);
|
|
219
|
+
}
|
|
220
|
+
return new Promise(function (resolve) {
|
|
221
|
+
var done = false;
|
|
222
|
+
var finish = function (value) {
|
|
223
|
+
if (!done) {
|
|
224
|
+
done = true;
|
|
225
|
+
resolve(value);
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
worker.addEventListener('statechange', function () {
|
|
229
|
+
if (worker.state === 'activated') { // no i18n
|
|
230
|
+
finish(registration);
|
|
231
|
+
}
|
|
232
|
+
else if (worker.state === 'redundant') { // no i18n
|
|
233
|
+
finish(null);
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
setTimeout(function () { finish(registration.active ? registration : null); }, ACTIVATE_TIMEOUT_MS);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function stopScroll(tag) {
|
|
240
|
+
var r = running[tag];
|
|
241
|
+
if (r && r.scroll !== null) {
|
|
242
|
+
clearInterval(r.scroll);
|
|
243
|
+
r.scroll = null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
/** Stops every timer of the notification `tag`. */
|
|
247
|
+
function stop(tag) {
|
|
248
|
+
var r = running[tag];
|
|
249
|
+
if (!r) {
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
stopScroll(tag);
|
|
253
|
+
r.timers.forEach(function (t) { clearTimeout(t); });
|
|
254
|
+
r.intervals.forEach(function (t) { clearInterval(t); });
|
|
255
|
+
delete running[tag];
|
|
256
|
+
}
|
|
257
|
+
/** Stops the timers of every notification, the worker's too. Disconnecting (and reset) calls it. */
|
|
258
|
+
function stopAllNotifications() {
|
|
259
|
+
Object.keys(running).forEach(stop);
|
|
260
|
+
if (workerPromise) {
|
|
261
|
+
workerPromise.then(function (registration) {
|
|
262
|
+
var w = registration && registration.active;
|
|
263
|
+
if (w && typeof w.postMessage === 'function') { // no i18n
|
|
264
|
+
try {
|
|
265
|
+
w.postMessage({ type: STOP_TIMERS_MESSAGE });
|
|
266
|
+
}
|
|
267
|
+
catch (err) {
|
|
268
|
+
// a worker going away has no timers left to stop
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}, function () { });
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
/** How long the worker has to answer that it takes a notification's timers. */
|
|
275
|
+
const TAKE_TIMEOUT_MS = 1000;
|
|
276
|
+
function hasTimers(shown) {
|
|
277
|
+
var view = shown.view;
|
|
278
|
+
return view.autoScrollSeconds > 0 || view.countdownTo > now() || view.autoDismissSeconds > 0;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Asks the worker to run the timers of `shown`. Resolves true once it answers that it does; false
|
|
282
|
+
* when it can't be asked, or doesn't answer in TAKE_TIMEOUT_MS (an older worker without timers).
|
|
283
|
+
*/
|
|
284
|
+
function handToWorker(registration, shown) {
|
|
285
|
+
var w = registration.active;
|
|
286
|
+
if (!w || typeof w.postMessage !== 'function' || typeof MessageChannel !== 'function') { // no i18n
|
|
287
|
+
return Promise.resolve(false);
|
|
288
|
+
}
|
|
289
|
+
return new Promise(function (resolve) {
|
|
290
|
+
var channel = new MessageChannel();
|
|
291
|
+
var done = false;
|
|
292
|
+
var timer;
|
|
293
|
+
var finish = function (taken) {
|
|
294
|
+
if (done) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
done = true;
|
|
298
|
+
clearTimeout(timer);
|
|
299
|
+
channel.port1.onmessage = null;
|
|
300
|
+
try {
|
|
301
|
+
channel.port1.close();
|
|
302
|
+
}
|
|
303
|
+
catch (e) {
|
|
304
|
+
// already closed
|
|
305
|
+
}
|
|
306
|
+
resolve(taken);
|
|
307
|
+
};
|
|
308
|
+
channel.port1.onmessage = function (event) {
|
|
309
|
+
finish(!!event.data && event.data.type === TIMERS_TAKEN);
|
|
310
|
+
};
|
|
311
|
+
timer = setTimeout(function () { finish(false); }, TAKE_TIMEOUT_MS);
|
|
312
|
+
try {
|
|
313
|
+
w.postMessage({ type: TIMERS_MESSAGE, shown: shown }, [channel.port2]);
|
|
314
|
+
}
|
|
315
|
+
catch (err) {
|
|
316
|
+
finish(false);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function now() {
|
|
321
|
+
return Date.now();
|
|
322
|
+
}
|
|
323
|
+
/** Shows `shown` through the worker, then hands its timers to the worker, or runs them here. */
|
|
324
|
+
function showWithWorker(registration, shown) {
|
|
325
|
+
stop(shown.tag);
|
|
326
|
+
registration.showNotification(titleFor(shown.view, shown.index), optionsFor(shown, false, now())).catch(function (err) {
|
|
327
|
+
Console.warn(LABEL + ': the service worker did not show the notification', err); // no i18n
|
|
328
|
+
});
|
|
329
|
+
if (!hasTimers(shown)) {
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
handToWorker(registration, shown).then(function (taken) {
|
|
333
|
+
if (!taken) {
|
|
334
|
+
runPageTimers(registration, shown);
|
|
335
|
+
}
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
/** Runs the timers of `shown`, already on screen, in this page while it is open. */
|
|
339
|
+
function runPageTimers(registration, shown) {
|
|
340
|
+
var tag = shown.tag;
|
|
341
|
+
stop(tag);
|
|
342
|
+
var r = { shown: shown, timers: [], intervals: [], scroll: null };
|
|
343
|
+
running[tag] = r;
|
|
344
|
+
// each show waits for the one before, and auto-dismiss for the last: a show landing after the
|
|
345
|
+
// close would bring the notification back
|
|
346
|
+
var last = Promise.resolve();
|
|
347
|
+
var show = function (update) {
|
|
348
|
+
var title = titleFor(r.shown.view, r.shown.index);
|
|
349
|
+
var options = optionsFor(r.shown, update, now());
|
|
350
|
+
last = last.then(function () {
|
|
351
|
+
// stopped while the one before was landing: closed, clicked or dismissed, so not again
|
|
352
|
+
if (running[tag] !== r) {
|
|
353
|
+
return undefined;
|
|
354
|
+
}
|
|
355
|
+
return registration.showNotification(title, options);
|
|
356
|
+
}).catch(function (err) {
|
|
357
|
+
Console.warn(LABEL + ': the service worker did not show the notification', err); // no i18n
|
|
358
|
+
});
|
|
359
|
+
return last;
|
|
360
|
+
};
|
|
361
|
+
var view = shown.view;
|
|
362
|
+
if (view.autoScrollSeconds > 0) {
|
|
363
|
+
r.scroll = setInterval(function () {
|
|
364
|
+
r.shown = { apptics: 1, view: view, index: stepIndex(view, r.shown.index, 1), tag: tag, message: r.shown.message, run: r.shown.run };
|
|
365
|
+
show(true);
|
|
366
|
+
}, view.autoScrollSeconds * 1000);
|
|
367
|
+
}
|
|
368
|
+
if (view.countdownTo > now()) {
|
|
369
|
+
var tick = setInterval(function () {
|
|
370
|
+
show(true);
|
|
371
|
+
if (now() >= view.countdownTo) {
|
|
372
|
+
clearInterval(tick);
|
|
373
|
+
}
|
|
374
|
+
}, 1000);
|
|
375
|
+
r.intervals.push(tick);
|
|
376
|
+
}
|
|
377
|
+
if (view.autoDismissSeconds > 0) {
|
|
378
|
+
r.timers.push(setTimeout(function () {
|
|
379
|
+
stop(tag);
|
|
380
|
+
last.then(function () { return registration.getNotifications({ tag: tag }); }).then(function (list) {
|
|
381
|
+
list.forEach(function (n) { n.close(); });
|
|
382
|
+
}, function () { });
|
|
383
|
+
}, view.autoDismissSeconds * 1000));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
/** A click on a plain notification: focus this tab, open the url, tell the app. */
|
|
387
|
+
function plainClicked(shown) {
|
|
388
|
+
try {
|
|
389
|
+
window.focus();
|
|
390
|
+
}
|
|
391
|
+
catch (e) {
|
|
392
|
+
// a focus the browser refuses changes nothing else
|
|
393
|
+
}
|
|
394
|
+
var url = shown.view.url;
|
|
395
|
+
if (url !== '') { // no i18n
|
|
396
|
+
if (new URL(url).origin === window.location.origin) {
|
|
397
|
+
_openUrlForTest.assign(url);
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
window.open(url, '_blank', 'noopener'); // no i18n
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
opened(shown.message, '', shown.run); // no i18n
|
|
404
|
+
reportClick({ action: '', appticsPnID: shown.view.id, additional: shown.view.extra, data: shown.message }); // no i18n
|
|
405
|
+
}
|
|
406
|
+
/** A plain notification, where there is no service worker: no buttons, the first image. */
|
|
407
|
+
function showPlain(N, shown) {
|
|
408
|
+
var options = optionsFor(shown, false, now());
|
|
409
|
+
delete options.actions;
|
|
410
|
+
delete options.data;
|
|
411
|
+
try {
|
|
412
|
+
var n = new N(titleFor(shown.view, 0), options);
|
|
413
|
+
n.onclick = function (event) {
|
|
414
|
+
if (event && typeof event.preventDefault === 'function') { // no i18n
|
|
415
|
+
event.preventDefault();
|
|
416
|
+
}
|
|
417
|
+
n.close();
|
|
418
|
+
plainClicked(shown);
|
|
419
|
+
};
|
|
420
|
+
if (shown.view.autoDismissSeconds > 0) {
|
|
421
|
+
setTimeout(function () { n.close(); }, shown.view.autoDismissSeconds * 1000);
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
catch (err) {
|
|
425
|
+
// Chrome on Android shows notifications only through a service worker
|
|
426
|
+
if (!warnedConstructor) {
|
|
427
|
+
warnedConstructor = true;
|
|
428
|
+
warnAlways(LABEL + ': this browser shows notifications only through a service worker, so its messages show no notification'); // no i18n
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function text(value) {
|
|
433
|
+
return typeof value === 'string' ? value : (typeof value === 'number' ? String(value) : ''); // no i18n
|
|
434
|
+
}
|
|
435
|
+
/**
|
|
436
|
+
* Shows the notification for a message's `data`, when the visitor has granted permission. It is
|
|
437
|
+
* tagged by addInfo.appticsPnID, else the envelope's `timestamp`, so the same message shown by two
|
|
438
|
+
* tabs appears once.
|
|
439
|
+
*/
|
|
440
|
+
function showNotification(data, timestamp) {
|
|
441
|
+
var N = api();
|
|
442
|
+
if (!N || N.permission !== 'granted') { // no i18n
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
var view = webNotificationFrom(data);
|
|
446
|
+
if (!view.title) {
|
|
447
|
+
view.title = (typeof document !== 'undefined' && document.title) || 'Notification'; // no i18n
|
|
448
|
+
}
|
|
449
|
+
var key = view.id || text(timestamp) || String(now()); // no i18n
|
|
450
|
+
var shown = { apptics: 1, view: view, index: 0, tag: 'apptics-push-' + key, message: data }; // no i18n
|
|
451
|
+
if (text(timestamp) !== '') { // no i18n
|
|
452
|
+
shown.run = text(timestamp);
|
|
453
|
+
}
|
|
454
|
+
if (!workerWanted()) {
|
|
455
|
+
showPlain(N, shown);
|
|
456
|
+
return;
|
|
457
|
+
}
|
|
458
|
+
serviceWorkerReady().then(function (registration) {
|
|
459
|
+
if (registration) {
|
|
460
|
+
showWithWorker(registration, shown);
|
|
461
|
+
}
|
|
462
|
+
else {
|
|
463
|
+
showPlain(N, shown);
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
}
|
|
467
|
+
listen();
|
|
468
|
+
|
|
469
|
+
export { SERVICE_WORKER, _openUrlForTest, requestPermission, serviceWorkerReady, showNotification, stopAllNotifications };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web push payload, as the server sends it to browsers (the WMS message's `data`):
|
|
3
|
+
*
|
|
4
|
+
* title, msg (the console's markdown), icon, image, images[], autoScroll, timer (seconds),
|
|
5
|
+
* msgTimer (epoch ms), autoDismiss (seconds), requireInteraction, silent, and
|
|
6
|
+
* addInfo: { appticsPnID, clickAction, additional: {...}, actionButton: [{ action, label,
|
|
7
|
+
* clickAction, icon }] }
|
|
8
|
+
*
|
|
9
|
+
* webNotificationFrom() turns it into what the browser shows, as the console's web preview shows
|
|
10
|
+
* it. The flat keys sent before this format (message, body, imageUrl, url) are still read.
|
|
11
|
+
* Every string is read with the HTML entities the server adds decoded, and only http(s) URLs are
|
|
12
|
+
* kept. Nothing here touches the DOM, so the service worker shares it.
|
|
13
|
+
*/
|
|
14
|
+
type Dict = Record<string, unknown>;
|
|
15
|
+
export interface WebButton {
|
|
16
|
+
action: string;
|
|
17
|
+
label: string;
|
|
18
|
+
url: string;
|
|
19
|
+
icon: string;
|
|
20
|
+
}
|
|
21
|
+
export interface WebNotification {
|
|
22
|
+
/** addInfo.appticsPnID, '' when absent: tags the notification. */
|
|
23
|
+
id: string;
|
|
24
|
+
title: string;
|
|
25
|
+
text: string;
|
|
26
|
+
icon: string;
|
|
27
|
+
images: string[];
|
|
28
|
+
/** Opened on a click on the notification's body. */
|
|
29
|
+
url: string;
|
|
30
|
+
buttons: WebButton[];
|
|
31
|
+
/** Seconds between carousel slides; 0 for a manual carousel or none. */
|
|
32
|
+
autoScrollSeconds: number;
|
|
33
|
+
/** The countdown's end, epoch ms; 0 for none. */
|
|
34
|
+
countdownTo: number;
|
|
35
|
+
autoDismissSeconds: number;
|
|
36
|
+
requireInteraction: boolean;
|
|
37
|
+
silent: boolean;
|
|
38
|
+
/** addInfo.additional: the console's key-values for the app. */
|
|
39
|
+
extra: Dict;
|
|
40
|
+
/** addInfo.notificationNumber: the run of a recurring notification; null when absent. */
|
|
41
|
+
number: number | null;
|
|
42
|
+
/** addInfo.conversionEvent: the event that counts as this message's conversion. */
|
|
43
|
+
conversion: {
|
|
44
|
+
name: string;
|
|
45
|
+
group: string;
|
|
46
|
+
} | null;
|
|
47
|
+
}
|
|
48
|
+
/** Browsers show at most two action buttons (the console's MAX_WEB_ACTION_BUTTONS). */
|
|
49
|
+
export declare const MAX_BUTTONS = 2;
|
|
50
|
+
/** `text` with HTML entities (named, &#n; and &#xh;) turned back into their characters. */
|
|
51
|
+
export declare function decodeEntities(text: string): string;
|
|
52
|
+
/** `value` as an absolute http(s) URL, '' for anything else. */
|
|
53
|
+
export declare function safeUrl(value: unknown): string;
|
|
54
|
+
/** What the browser shows for a push message's `data`. */
|
|
55
|
+
export declare function webNotificationFrom(data: Dict): WebNotification;
|
|
56
|
+
/**
|
|
57
|
+
* The console's markdown (*bold*, _italic_, __underline__, ~strike~, `quote`, {text}(#color)) as
|
|
58
|
+
* the plain text a browser notification shows: bold and italic as Unicode styled letters,
|
|
59
|
+
* underline and strike as combining marks, quotes as “…”, colour dropped. The same output as the
|
|
60
|
+
* console's web preview (markdownToWebText in its pushNotificationUtils.ts).
|
|
61
|
+
*/
|
|
62
|
+
export declare function markdownToWebText(md: string): string;
|
|
63
|
+
/** The time left until `targetMs` as HH:MM:SS, 00:00:00 once it has passed. */
|
|
64
|
+
export declare function formatCountdown(targetMs: number, nowMs: number): string;
|
|
65
|
+
export {};
|