-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnvironmentDetector.js
274 lines (246 loc) · 7.51 KB
/
EnvironmentDetector.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* Caching wrapper for AddonSettings API that does save and load settings.
*
* @module EnvironmentDetector
*/
import * as Helper from "./internal/Helper.js";
export const POPUP_TYPE = {
OVERFLOW: Symbol("overflow menu popup"),
USUAL: Symbol("usual browser button popup"),
NEW_PAGE: Symbol("full page, opened in new tab, e.g. on Android"),
};
export const POPUP_SIZE = {
MAX: Symbol("maximum value")
};
export const SIZE = {
WIDTH: Symbol("width"),
HEIGHT: Symbol("height"),
};
const MAX_POPUP_SIZE = {
[POPUP_TYPE.USUAL]: {
WIDTH: 800,
HEIGHT: 600,
},
[POPUP_TYPE.OVERFLOW]: {
WIDTH: 348,
HEIGHT: null,
}
};
// cache/retrieve value for OS directly at startup
let cachedOs;
browser.runtime.getPlatformInfo().then((platformInfo) => {
cachedOs = platformInfo.os;
});
/**
* Returns the popup environment this is running in.
*
* @public
* @returns {Object|null}
*/
export function getPopupType() {
let overflowWidth;
// https://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime/PlatformOs
switch (cachedOs) {
case "android":
return POPUP_TYPE.NEW_PAGE;
case "win":
overflowWidth = 348;
break;
default:
overflowWidth = 425;
}
if (
window.innerWidth === overflowWidth
) {
return POPUP_TYPE.OVERFLOW;
} else {
return POPUP_TYPE.USUAL;
}
}
/**
* Returns the size of the popup.
*
* Only really useful if POPUP_TYPE.USUAL, so you can find out whether the
* popup has been maximum sized.
*
* @public
* @returns {Object|null}
*/
export function getPopupSize() {
const popupType = getPopupType();
return {
width: (window.innerWidth === MAX_POPUP_SIZE[popupType].WIDTH) ? POPUP_SIZE.MAX : window.innerWidth,
height: (window.innerHeight === MAX_POPUP_SIZE[popupType].HEIGHT) ? POPUP_SIZE.MAX : window.innerHeight,
};
}
/**
* Waits until the popup has completly opened.
*
* It may happen that all the other function return wrong/bad values, if the
* popup is not opened yet, if you are requesting these values very fast at the
* side loading.
* (still "folding" up)
* To prevent this, call this function asyncronously, i.e. e.g. await the Promise.
*
* By default it retries 10x in 50ms intervals, i.e. 0.5 seconds.
*
* @public
* @param {number} [retries=10] how often to retry at most
* @param {number} [delay=10] how many ms to re-check
* @returns {Promise}
*/
export async function waitForPopupOpen(retries = 10, delay = 50) {
const wait = (ms) => new Promise((func) => setTimeout(func, ms));
// values are correctly loaded
if (window.innerWidth !== 0 && window.innerHeight !== 0) {
return Promise.resolve();
}
if (retries <= 0) {
// should usually never™ happen, https://xkcd.com/2200/
throw new TypeError("no re-tries left, popup is not opened"); // will be converted into rejected promise
}
await wait(delay);
// retry
return waitForPopupOpen(retries - 1, delay);
}
/**
* Returns whether we are running in a popup.
*
* @todo needs testing
* @public
* @returns {boolean}
*/
export function isPopup() {
// window.outer* is the size of the main Firfox window
// If window.inner* is significantly smaller, this may be a popup.
if (getPopupType() === POPUP_TYPE.OVERFLOW) {
return true;
}
if (cachedOs === "android") {
// Android never has popups
return false;
}
// If it is the same as window size, our main window is likely maximized
const outerIsAsScreenSize = ((
pageOverflows(SIZE.WIDTH) ?
Helper.isSameSizeWithScrollbar(screen.availWidth, window.outerWidth) :
screen.availWidth === window.outerWidth
) && (
pageOverflows(SIZE.HEIGHT) ?
Helper.isSameSizeWithScrollbar(screen.availHeight, window.outerHeight) :
screen.availHeight === window.outerHeight
));
return outerIsAsScreenSize;
}
/**
* Return debug data.
*
* @public
* @returns {Object}
*/
export function getDebugData() {
const getDocumentElement = (element) => {
return {
clientRects: element.getClientRects(),
boundingClientRects: element.getBoundingClientRect(),
clientWidth: element.clientWidth,
clientHeight: element.clientHeight,
offsetWidth: element.offsetWidth,
offsetHeight: element.offsetHeight,
scrollWidth: element.scrollWidth,
scrollHeight: element.scrollHeight
};
};
return {
inner: {
innerWidth: window.innerWidth,
innerHeight: window.innerHeight
},
outer: {
outerWidth: window.outerWidth,
outerHeight: window.outerHeight
},
body: getDocumentElement(document.body),
html: getDocumentElement(document.documentElement),
scroll: getDocumentElement(document.scrollingElement),
screen: {
width: window.screen.width,
height: window.screen.height,
left: window.screen.left,
top: window.screen.top,
availWidth: window.screen.availWidth,
availHeight: window.screen.availHeight,
availLeft: window.screen.availLeft,
availTop: window.screen.availTop,
mozOrientation: window.screen.mozOrientation,
orientation: {
angle: window.screen.orientation.angle,
type: window.screen.orientation.type,
},
pixelDepth: window.screen.pixelDepth,
}
};
}
/**
* Returns whether the page overlows.
*
* @public
* @param {SIZE|null} size which size to check (or null for both)
* @returns {boolean}
*/
export function pageOverflows(size) {
let doesOverflow = false;
if (!size || size === SIZE.WIDTH) {
doesOverflow = doesOverflow || (getOverflowInPixels(SIZE.WIDTH) > 0);
}
if (!size || size === SIZE.HEIGHT) {
doesOverflow = doesOverflow || (getOverflowInPixels(SIZE.HEIGHT) > 0);
}
return doesOverflow;
}
/**
* Returns how many pixels the page overflows in one direction.
*
* @public
* @param {SIZE} size which size to check
* @returns {number}
*/
export function getOverflowInPixels(size) {
switch (size) {
case SIZE.WIDTH: {
const overflow = document.documentElement.scrollWidth - document.documentElement.clientWidth;
if (overflow > 0) {
return overflow;
}
// underflow
return document.body.clientWidth - document.documentElement.clientWidth;
} case SIZE.HEIGHT: {
const overflow = document.documentElement.scrollHeight - document.documentElement.clientHeight;
if (overflow > 0) {
return overflow;
}
// underflow
return document.body.clientWidth - document.documentElement.clientWidth;
} default:
throw new TypeError("invalid size paramater passed");
}
}
/**
* Returns the percentage of the overflow in one direction.
*
* @public
* @param {SIZE} size which size to check
* @returns {number}
*/
export function getOverflowInPercentage(size) {
switch (size) {
case SIZE.WIDTH: {
const overflow = getOverflowInPixels(SIZE.WIDTH);
return overflow / document.documentElement.scrollWidth * 100;
} case SIZE.HEIGHT: {
const overflow = getOverflowInPixels(SIZE.HEIGHT);
return overflow / document.documentElement.scrollHeight * 100;
} default:
throw new TypeError("invalid size paramater passed");
}
}