@ouroboros/browser 0.1.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/LICENSE +21 -0
- package/README.md +61 -0
- package/clipboard.d.ts +24 -0
- package/clipboard.js +60 -0
- package/clipboard.ts +66 -0
- package/cookies.d.ts +55 -0
- package/cookies.js +87 -0
- package/cookies.ts +98 -0
- package/hash.d.ts +78 -0
- package/hash.js +195 -0
- package/hash.ts +239 -0
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/index.ts +4 -0
- package/package.json +38 -0
- package/pageVisibility.d.ts +48 -0
- package/pageVisibility.js +132 -0
- package/pageVisibility.ts +156 -0
- package/tsconfig.json +16 -0
- package/tslint.json +10 -0
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Page Visibility
|
|
3
|
+
*
|
|
4
|
+
* Library/Namespace containing various functions to track page visibility
|
|
5
|
+
*
|
|
6
|
+
* @author Chris Nasr <chris@ouroboroscoding.com>
|
|
7
|
+
* @copyright Ouroboros Coding Inc.
|
|
8
|
+
* @created 2018-08-01
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
// Callback type
|
|
12
|
+
type Callback = (...args: any[]) => {};
|
|
13
|
+
|
|
14
|
+
// Init the visibility property name and event values
|
|
15
|
+
let bVis = false;
|
|
16
|
+
const dVis = {
|
|
17
|
+
property: 'hidden',
|
|
18
|
+
event: 'visibilitychange',
|
|
19
|
+
state: 'visibilityState'
|
|
20
|
+
};
|
|
21
|
+
const lCallbacks: Callback[] = []
|
|
22
|
+
|
|
23
|
+
// Figure out the name of the visibility property and event
|
|
24
|
+
// Valid browser prefixes
|
|
25
|
+
const lPrefixes = ['moz', 'ms', 'o', 'webkit'];
|
|
26
|
+
|
|
27
|
+
// If a prefix is required
|
|
28
|
+
if('hidden' in document) {
|
|
29
|
+
bVis = true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Else, loop through the prefixes
|
|
33
|
+
else {
|
|
34
|
+
for(const prefix of lPrefixes) {
|
|
35
|
+
|
|
36
|
+
// If the prefixes version exists
|
|
37
|
+
if((prefix + 'Hidden') in document) {
|
|
38
|
+
dVis.property = prefix + 'Hidden';
|
|
39
|
+
dVis.event = prefix + 'visibilitychange';
|
|
40
|
+
dVis.state = prefix + 'VisibilityState';
|
|
41
|
+
bVis = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Track
|
|
49
|
+
*
|
|
50
|
+
* The actual function passed to the event so that we only have one event listener
|
|
51
|
+
*
|
|
52
|
+
* @name track
|
|
53
|
+
* @access private
|
|
54
|
+
* @return {void}
|
|
55
|
+
*/
|
|
56
|
+
function track(): void {
|
|
57
|
+
|
|
58
|
+
// Call the callbacks and pass them bool and state values
|
|
59
|
+
for(const f of lCallbacks) {
|
|
60
|
+
f(document[dVis.property as keyof Document], document[dVis.state as keyof Document]);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Get
|
|
66
|
+
*
|
|
67
|
+
* Returns the current state of visibility
|
|
68
|
+
*
|
|
69
|
+
* @name get
|
|
70
|
+
* @access public
|
|
71
|
+
* @return {object | false}
|
|
72
|
+
*/
|
|
73
|
+
export function get(): object | false {
|
|
74
|
+
|
|
75
|
+
// If the library is available
|
|
76
|
+
if(bVis) {
|
|
77
|
+
return {
|
|
78
|
+
property: document[dVis.property as keyof Document],
|
|
79
|
+
state: document[dVis.state as keyof Document]
|
|
80
|
+
}
|
|
81
|
+
} else {
|
|
82
|
+
console.error('Page Visibility API not available');
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Subscribe
|
|
89
|
+
*
|
|
90
|
+
* Track changes on page visibility
|
|
91
|
+
*
|
|
92
|
+
* @name subscribe
|
|
93
|
+
* @access public
|
|
94
|
+
* @param {function} callback Function to call on visibility changes
|
|
95
|
+
* @return {boolean}
|
|
96
|
+
*/
|
|
97
|
+
export function subscribe(callback: Callback): boolean {
|
|
98
|
+
|
|
99
|
+
// If the Page Visibility API is not available
|
|
100
|
+
if(bVis === false) {
|
|
101
|
+
console.error('Page Visibility API not available');
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Save the callback for later
|
|
106
|
+
lCallbacks.push(callback);
|
|
107
|
+
|
|
108
|
+
// Attach an event listener if this is our first event
|
|
109
|
+
if(lCallbacks.length === 1) {
|
|
110
|
+
document.addEventListener(dVis.event, track);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Return ok
|
|
114
|
+
return true;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Ubsubscribe
|
|
119
|
+
*
|
|
120
|
+
* Stop tracking changes in page visibility
|
|
121
|
+
*
|
|
122
|
+
* @name unsubscribe
|
|
123
|
+
* @access public
|
|
124
|
+
* @param {function} callback
|
|
125
|
+
* @return {boolean}
|
|
126
|
+
*/
|
|
127
|
+
export function unsubscribe(callback: Callback): boolean {
|
|
128
|
+
|
|
129
|
+
// If the Page Visibility API is not available
|
|
130
|
+
if(bVis === false) {
|
|
131
|
+
console.error('Page Visibility API not available');
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Find the index of the callback
|
|
136
|
+
const iIndex = lCallbacks.indexOf(callback);
|
|
137
|
+
|
|
138
|
+
// If it exists
|
|
139
|
+
if(iIndex > -1) {
|
|
140
|
+
|
|
141
|
+
// Remove the callback
|
|
142
|
+
lCallbacks.splice(iIndex, 1);
|
|
143
|
+
|
|
144
|
+
// Remove event listener if we have no more callbacks
|
|
145
|
+
if(lCallbacks.length === 0) {
|
|
146
|
+
document.removeEventListener(dVis.event, track);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Return ok
|
|
151
|
+
return true;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Default export
|
|
155
|
+
const pageVisibility = { get, subscribe, unsubscribe };
|
|
156
|
+
export default pageVisibility;
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"allowJs": true,
|
|
4
|
+
"allowSyntheticDefaultImports": true,
|
|
5
|
+
"declaration": true,
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"isolatedModules": true,
|
|
8
|
+
"lib": ["es2019", "dom", "dom.iterable"],
|
|
9
|
+
"moduleResolution": "node",
|
|
10
|
+
"noEmit": false,
|
|
11
|
+
"strict": true,
|
|
12
|
+
"target": "esnext"
|
|
13
|
+
},
|
|
14
|
+
"include": ["./"],
|
|
15
|
+
"exclude": []
|
|
16
|
+
}
|