-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathTips.js
224 lines (212 loc) · 8.25 KB
/
Tips.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* The settings/constraints for a tip, represented in an object.
*
* @public
* @typedef {Object} TipObject
* @property {string} id just some ID
* @property {integer|null} requiredShowCount Shows the message x times; set
* to `null` to show infinitively. This is the maximum value.
* @property {bool} [allowDismiss=true] set to false to disallow dismissing
* the message. This likely makes no sense for any tip, so the default is true.
* @property {bool|integer} [requireDismiss=false] show the message, if it is
* not, at least, dismissed for x times. Alternatively set to true to require
* that message is dismissed the exact same number as requiredShowCount states,
* i.e. only dismissed count as "tip shown".
* @property {integer|null} [maximumDismiss=null] hides the message, if it
* has been dismissed x times.
* @property {integer} [requiredTriggers=10] require some displays ("triggers")
* of (any) add-on page before showing tip. This is effectively just a minimum
* limit, so it is not shown too "early".
* @property {Object.<string, integer>} [showInContext] a key-value object with
* context -> num to require the tip to be shown in a specific context for the
* given number of times. See {@link RandomTips.setContext}.
* @property {Object.<string, integer>} [maximumInContest] a key-value object with
* context -> num to only show the tip in a specific context at most for the
* given number of times. See {@link RandomTips.setContext}.
* @property {bool|integer} [randomizeDisplay=false] Randomizes the display with a
* chance of 50% by default (when set to `true`). You can override that percentage
* by specifying an integer < 1 (e.g. 0.2 for a 20% chance) as a propbability.
* The chance is the chance that the tip *is shown*, i.e. the tip is shown for
* <= p and not shown for < p if p is the propbability you pass.
* Note that the tip message display in general is already randomized
* with a chance of 20%, see {@link RandomTips.GLOBAL_RANDOMIZE}.
* @property {string} text The text to actually show. It is passed to the
* {@link MessageHandler}, so you can (& should) use a translatable string here.
* @property {Object} [actionButton] adds an action button to the message, is
* passed as-is to the {@link MessageHandler} module.
* @property {string} actionButton.text the text to use for the button
* @property {string|function} actionButton.action the link or function to
* execute on click
* @property {callbackShowTip} [showTip] evaluate via a custom function
* whether to show or not to show the tip
*/
/**
* Custom function to check whether a tip shall be shown or not.
*
* **Attention:** This is evaluated before all other constraints were evaluated.
* Be careful before returning `true`, you may actually want to return `null`.
*
* * If you return `true`, it means you **force**(!) the tip to be shown. This
* overrides the usual conditions and shows the tip immediately.
* * If you return `false`, it means you disallow the tip to be shown.
* * If `null` is returned, the usual (other) conditions that determinate whether
* to show the tip are evaluated.
* If you throw something, this cancels the whole tip selection process.
*
* Note that you can actually _modify_ the values of the first two parameters, i.e.
* `tipSpec` and `thisTipConfig`. If you modify the config, it will be saved
* automatically.
* The difference between `tipSpec` and `tipSpecOrig` is, that in `tipSpec` the
* default values of the tips have been added/modified, if needed and that
* `tipSpecOrig` is the original as you have specified it in {@link TipObject}.
*
* For stylistic reasons, it is recommend to not hardcode the function into the
* this file, but use a separate file/module.
*
* @public
* @callback callbackShowTip
* @param {TipObject} tipSpec the TipObject, merged with default values
* @param {module:RandomTips~TipConfigObject} thisTipConfig the settings of the tip
* @param {TipObject} tipSpecOrig the original, unmodified (frozen) TipObject
* of the tip
* @param {module:RandomTips~ModuleConfig} moduleConfig the whole config of this module
* @return {boolean|null}
*/
import {isMobile} from "../MobileHelper.js";
import {userSpeaksLocaleNotYetTranslated} from "../LanguageHelper.js";
const DEFAULT_POPUP_HOT_KEY = "Ctrl+Shift+F10";
/**
* An array of all tips.
*
* @private
* @const
* @type {Array.<TipObject>}
*/
const tipArray = [
{
id: "likeAddon",
requiredShowCount: 3,
requireDismiss: 1,
maximumDismiss: 2,
requiredTriggers: 10,
showInContext: {
"popup": 1
},
randomizeDisplay: false,
text: "tipYouLikeAddon",
actionButton: {
text: "tipYouLikeAddonButton",
action: "https://addons.mozilla.org/firefox/addon/offline-qr-code-generator/reviews/"
}
},
{
id: "saveQr",
requiredShowCount: 5,
requireDismiss: 1,
maximumDismiss: 2,
requiredTriggers: 5,
showInContext: {
"popup": 1
},
randomizeDisplay: false,
text: "tipSaveQrCode",
actionButton: {
text: "tipLearnMore",
action: "tipSaveQrCodeLink"
}
},
// {
// id: "donate",
// requiredShowCount: 4,
// requireDismiss: 1,
// maximumDismiss: 2,
// requiredTriggers: 50,
// // do not show on options page as Firefox already displays a donate button there
// maximumInContest: {
// "options": 0
// },
// randomizeDisplay: 0.4,
// text: "tipDonate",
// actionButton: {
// text: "tipDonateButton",
// action: "https://liberapay.com/rugk/"
// }
// },
{
id: "qrCodeHotkey",
requiredShowCount: 3,
maximumDismiss: 1,
requiredTriggers: 2,
randomizeDisplay: false,
text: "tipQrCodeHotkey",
showTip: async () => {
// do not show if user is on Android
if (await isMobile()) {
return false;
}
// find command
const allCommands = await browser.commands.getAll();
const popupOpenCommand = allCommands.find((command) => command.name === "_execute_browser_action");
// if shortcut is modified, do not show tip
if (popupOpenCommand.shortcut !== DEFAULT_POPUP_HOT_KEY) {
return false;
}
return null; // continue as normal
}
},
{
id: "androidQrReader",
requiredShowCount: 3,
maximumDismiss: 1,
requiredTriggers: 6,
randomizeDisplay: false,
text: "tipAndroidQrReader",
actionButton: {
text: "tipHowToUse",
action: "tipAndroidQrReaderLink"
},
showTip: async () => {
// only not show if user is on Android
if (!(await isMobile())) {
return false;
}
return null; // continue as normal
}
},
{
id: "translateAddon",
requiredShowCount: 5,
maximumDismiss: 1,
requiredTriggers: 10,
randomizeDisplay: false,
text: "tipTranslateAddon",
actionButton: {
text: "tipLearnMore",
action: "tipTranslateAddonLink"
},
showTip: async (tipSpec) => {
// do not show tip if add-on is already translated into a locale the
// user speaks
if (!(await userSpeaksLocaleNotYetTranslated())) {
// Instead of returning false, we "just" make it unlikely that
// the tip is shown.
// This means we can be sure the tip is shown anyway to some
// users, who may speak a language we already have the add-on
// translated into it, as they can still improve translations etc.
tipSpec.randomizeDisplay = 0.10; // 10%
return null;
}
return null; // continue as normal
}
}
];
// freeze it all, this is strongly recommend
tipArray.forEach((object) => Object.freeze(object));
/**
* The list of all tips. (now exported)
*
* @public
* @const
* @type {Array.<TipObject>}
*/
export const tips = Object.freeze(tipArray);