@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Ouroboros Coding
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,61 @@
1
+ # @ouroboros/browser
2
+
3
+ [![npm version](https://img.shields.io/npm/v/@ouroboros/browser.svg)](https://www.npmjs.com/package/@ouroboros/browser) ![MIT License](https://img.shields.io/npm/l/@ouroboros/browser.svg)
4
+
5
+ A library of classes to manage functionality in browsers.
6
+
7
+ ## Installation
8
+ npm
9
+ ```bash
10
+ npm install @ouroboros/browser
11
+ ```
12
+
13
+ ## Getting Started
14
+
15
+ Import clipboard into your code
16
+
17
+ ```javascript
18
+ import clipboard from '@ouroboros/browser/clipboard';
19
+ ```
20
+
21
+ or
22
+
23
+ ```javascript
24
+ import { clipboard } from '@ouroboros/browser';
25
+ ```
26
+
27
+ Import Cookies into your code
28
+
29
+ ```javascript
30
+ import cookies from '@ouroboros/browser/cookies';
31
+ ```
32
+
33
+ or
34
+
35
+ ```javascript
36
+ import { cookies } from '@ouroboros/browser';
37
+ ```
38
+
39
+ Import hash into your code
40
+
41
+ ```javascript
42
+ import hash from '@ouroboros/browser/hash';
43
+ ```
44
+
45
+ or
46
+
47
+ ```javascript
48
+ import { hash } from '@ouroboros/browser';
49
+ ```
50
+
51
+ Import pageVisibility into your code
52
+
53
+ ```javascript
54
+ import pageVisibility from '@ouroboros/browser/pageVisibility';
55
+ ```
56
+
57
+ or
58
+
59
+ ```javascript
60
+ import { pageVisibility } from '@ouroboros/browser';
61
+ ```
package/clipboard.d.ts ADDED
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Clipboard
3
+ *
4
+ * Handles adding text to the clipboard
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+ /**
11
+ * Copy
12
+ *
13
+ * Attempts to add text to the clipboard
14
+ *
15
+ * @name copy
16
+ * @access public
17
+ * @param {string} text The text to add to the clipboard
18
+ * @return {Promise}
19
+ */
20
+ export declare function copy(text: string): Promise<any>;
21
+ declare const clipboard: {
22
+ copy: typeof copy;
23
+ };
24
+ export default clipboard;
package/clipboard.js ADDED
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Clipboard
3
+ *
4
+ * Handles adding text to the clipboard
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+ /**
11
+ * Copy
12
+ *
13
+ * Attempts to add text to the clipboard
14
+ *
15
+ * @name copy
16
+ * @access public
17
+ * @param {string} text The text to add to the clipboard
18
+ * @return {Promise}
19
+ */
20
+ export function copy(text) {
21
+ // If we find the api
22
+ if (navigator.clipboard) {
23
+ return navigator.clipboard.writeText(text);
24
+ }
25
+ // Else, fallback to copying from a textarea we create ourselves
26
+ else {
27
+ return new Promise((resolve, reject) => {
28
+ // Create a text area to place the text in then select it
29
+ const oEl = document.createElement('textarea');
30
+ oEl.value = text;
31
+ oEl.style.top = "0";
32
+ oEl.style.left = "0";
33
+ oEl.style.position = "fixed";
34
+ document.body.appendChild(oEl);
35
+ oEl.focus();
36
+ oEl.select();
37
+ // Try to copy the text to the clipboard
38
+ let bRes = false;
39
+ let mErr = null;
40
+ try {
41
+ bRes = document.execCommand('copy');
42
+ }
43
+ catch (err) {
44
+ mErr = err;
45
+ }
46
+ // Remove the textarea and return the result
47
+ document.body.removeChild(oEl);
48
+ // Resolve or reject
49
+ if (mErr) {
50
+ reject(mErr);
51
+ }
52
+ else {
53
+ resolve(bRes);
54
+ }
55
+ });
56
+ }
57
+ }
58
+ // Default export
59
+ const clipboard = { copy };
60
+ export default clipboard;
package/clipboard.ts ADDED
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Clipboard
3
+ *
4
+ * Handles adding text to the clipboard
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+
11
+ /**
12
+ * Copy
13
+ *
14
+ * Attempts to add text to the clipboard
15
+ *
16
+ * @name copy
17
+ * @access public
18
+ * @param {string} text The text to add to the clipboard
19
+ * @return {Promise}
20
+ */
21
+ export function copy(text: string): Promise<any> {
22
+
23
+ // If we find the api
24
+ if(navigator.clipboard) {
25
+ return navigator.clipboard.writeText(text);
26
+ }
27
+
28
+ // Else, fallback to copying from a textarea we create ourselves
29
+ else {
30
+ return new Promise((resolve, reject) => {
31
+
32
+ // Create a text area to place the text in then select it
33
+ const oEl = document.createElement('textarea');
34
+ oEl.value = text;
35
+ oEl.style.top = "0";
36
+ oEl.style.left = "0";
37
+ oEl.style.position = "fixed";
38
+ document.body.appendChild(oEl);
39
+ oEl.focus();
40
+ oEl.select();
41
+
42
+ // Try to copy the text to the clipboard
43
+ let bRes = false;
44
+ let mErr = null;
45
+ try {
46
+ bRes = document.execCommand('copy');
47
+ } catch(err) {
48
+ mErr = err;
49
+ }
50
+
51
+ // Remove the textarea and return the result
52
+ document.body.removeChild(oEl);
53
+
54
+ // Resolve or reject
55
+ if(mErr) {
56
+ reject(mErr);
57
+ } else {
58
+ resolve(bRes);
59
+ }
60
+ });
61
+ }
62
+ }
63
+
64
+ // Default export
65
+ const clipboard = { copy };
66
+ export default clipboard;
package/cookies.d.ts ADDED
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Cookies
3
+ *
4
+ * JS Library to deal with Cookies
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+ /**
11
+ * Get
12
+ *
13
+ * Gets a cookie or returns the default. Set no name to get all
14
+ *
15
+ * @name get
16
+ * @access public
17
+ * @param {string} name The name of the cookie to fetch
18
+ * @param {string} defaulReturn The default value to return if no cookie is found
19
+ * @return {string | object | null}
20
+ */
21
+ declare function get(name: string, defaulReturn: string | null | undefined): string | object | null;
22
+ /**
23
+ * Remove
24
+ *
25
+ * Deletes a cookie
26
+ *
27
+ * @name remove
28
+ * @access public
29
+ * @param {string} name The name of the cookie to delete
30
+ * @param {string?} domain The domain of the cookie
31
+ * @param {string?} path The path of the cookie
32
+ * @return {void}
33
+ */
34
+ declare function remove(name: string, domain?: string, path?: string): void;
35
+ /**
36
+ * Set
37
+ *
38
+ * Sets a cookie
39
+ *
40
+ * @name set
41
+ * @access public
42
+ * @param {string} name The name of the cookie
43
+ * @param {string} value The value to store
44
+ * @param {number} expires The number of seconds before the cookie expires
45
+ * @param {string?} domain The optional domain to set the cookie on
46
+ * @param {string?} path The optional path of the cookie
47
+ * @return {void}
48
+ */
49
+ declare function set(name: string, value: string, expires: number, domain?: string, path?: string): void;
50
+ declare const cookies: {
51
+ get: typeof get;
52
+ remove: typeof remove;
53
+ set: typeof set;
54
+ };
55
+ export default cookies;
package/cookies.js ADDED
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Cookies
3
+ *
4
+ * JS Library to deal with Cookies
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+ /**
11
+ * Get
12
+ *
13
+ * Gets a cookie or returns the default. Set no name to get all
14
+ *
15
+ * @name get
16
+ * @access public
17
+ * @param {string} name The name of the cookie to fetch
18
+ * @param {string} defaulReturn The default value to return if no cookie is found
19
+ * @return {string | object | null}
20
+ */
21
+ function get(name, defaulReturn) {
22
+ // Set the default if no value is passed
23
+ if (typeof defaulReturn === 'undefined') {
24
+ defaulReturn = null;
25
+ }
26
+ // Parse all cookies
27
+ const oCookies = {};
28
+ const lCookies = document.cookie.split(';');
29
+ for (const s of lCookies) {
30
+ const l = s.split('=');
31
+ oCookies[l[0].trimStart()] = decodeURIComponent(l[1]);
32
+ }
33
+ // If there's no name, return all
34
+ if (typeof name === 'undefined') {
35
+ return oCookies;
36
+ }
37
+ // If the cookie exists return it, else return the default
38
+ return (name in oCookies) ? oCookies[name] : defaulReturn;
39
+ }
40
+ /**
41
+ * Remove
42
+ *
43
+ * Deletes a cookie
44
+ *
45
+ * @name remove
46
+ * @access public
47
+ * @param {string} name The name of the cookie to delete
48
+ * @param {string?} domain The domain of the cookie
49
+ * @param {string?} path The path of the cookie
50
+ * @return {void}
51
+ */
52
+ function remove(name, domain, path) {
53
+ set(name, '', -1, domain, path);
54
+ }
55
+ /**
56
+ * Set
57
+ *
58
+ * Sets a cookie
59
+ *
60
+ * @name set
61
+ * @access public
62
+ * @param {string} name The name of the cookie
63
+ * @param {string} value The value to store
64
+ * @param {number} expires The number of seconds before the cookie expires
65
+ * @param {string?} domain The optional domain to set the cookie on
66
+ * @param {string?} path The optional path of the cookie
67
+ * @return {void}
68
+ */
69
+ function set(name, value, expires, domain, path) {
70
+ // Generate the expires time
71
+ const d = new Date();
72
+ d.setTime(d.getTime() + (expires * 1000));
73
+ // Generate the cookie string
74
+ let s = name + '=' + encodeURIComponent(value) +
75
+ '; expires=' + d.toUTCString() + ';';
76
+ if (domain) {
77
+ s += ' domain=' + domain + ';';
78
+ }
79
+ if (path) {
80
+ s += ' path=' + path + ';';
81
+ }
82
+ // Set the cookie
83
+ document.cookie = s;
84
+ }
85
+ // Default export
86
+ const cookies = { get, remove, set };
87
+ export default cookies;
package/cookies.ts ADDED
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Cookies
3
+ *
4
+ * JS Library to deal with Cookies
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-11-24
9
+ */
10
+
11
+ /**
12
+ * Get
13
+ *
14
+ * Gets a cookie or returns the default. Set no name to get all
15
+ *
16
+ * @name get
17
+ * @access public
18
+ * @param {string} name The name of the cookie to fetch
19
+ * @param {string} defaulReturn The default value to return if no cookie is found
20
+ * @return {string | object | null}
21
+ */
22
+ function get(name: string, defaulReturn: string | null | undefined): string | object | null {
23
+
24
+ // Set the default if no value is passed
25
+ if(typeof defaulReturn === 'undefined') {
26
+ defaulReturn = null;
27
+ }
28
+
29
+ // Parse all cookies
30
+ const oCookies: Record<string, string> = {};
31
+ const lCookies = document.cookie.split(';');
32
+ for(const s of lCookies) {
33
+ const l = s.split('=');
34
+ oCookies[l[0].trimStart()] = decodeURIComponent(l[1]);
35
+ }
36
+
37
+ // If there's no name, return all
38
+ if(typeof name === 'undefined') {
39
+ return oCookies;
40
+ }
41
+
42
+ // If the cookie exists return it, else return the default
43
+ return (name in oCookies) ? oCookies[name] : defaulReturn;
44
+ }
45
+
46
+ /**
47
+ * Remove
48
+ *
49
+ * Deletes a cookie
50
+ *
51
+ * @name remove
52
+ * @access public
53
+ * @param {string} name The name of the cookie to delete
54
+ * @param {string?} domain The domain of the cookie
55
+ * @param {string?} path The path of the cookie
56
+ * @return {void}
57
+ */
58
+ function remove(name: string, domain?: string, path?: string): void {
59
+ set(name, '', -1, domain, path);
60
+ }
61
+
62
+ /**
63
+ * Set
64
+ *
65
+ * Sets a cookie
66
+ *
67
+ * @name set
68
+ * @access public
69
+ * @param {string} name The name of the cookie
70
+ * @param {string} value The value to store
71
+ * @param {number} expires The number of seconds before the cookie expires
72
+ * @param {string?} domain The optional domain to set the cookie on
73
+ * @param {string?} path The optional path of the cookie
74
+ * @return {void}
75
+ */
76
+ function set(name: string, value: string, expires: number, domain?: string, path?: string): void {
77
+
78
+ // Generate the expires time
79
+ const d = new Date();
80
+ d.setTime(d.getTime() + (expires*1000))
81
+
82
+ // Generate the cookie string
83
+ let s = name + '=' + encodeURIComponent(value) +
84
+ '; expires=' + d.toUTCString() + ';';
85
+ if(domain) {
86
+ s += ' domain=' + domain + ';';
87
+ }
88
+ if(path) {
89
+ s += ' path=' + path + ';';
90
+ }
91
+
92
+ // Set the cookie
93
+ document.cookie = s;
94
+ }
95
+
96
+ // Default export
97
+ const cookies = { get, remove, set };
98
+ export default cookies;
package/hash.d.ts ADDED
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Hash
3
+ *
4
+ * JS Library to manage hash values
5
+ *
6
+ * @author Chris Nasr <chris@ouroboroscoding.com>
7
+ * @copyright Ouroboros Coding Inc.
8
+ * @created 2018-12-09
9
+ */
10
+ type Callback = (value: string | null) => {};
11
+ /**
12
+ * Init
13
+ *
14
+ * Initialises the internal hash by fetching and parsing the current
15
+ * location hash
16
+ *
17
+ * @name init
18
+ * @access public
19
+ * @return {void}
20
+ */
21
+ declare function init(): void;
22
+ /**
23
+ * Get
24
+ *
25
+ * Returns a hash name
26
+ *
27
+ * @name get
28
+ * @access public
29
+ * @static
30
+ * @param {string} name The name to look for
31
+ * @param {string?} defaultReturn The value to return if the name isn't found
32
+ * @return {string | null}
33
+ */
34
+ declare function get(name: string, defaultReturn?: string): string | null;
35
+ /**
36
+ * Set
37
+ *
38
+ * Sets a specific name
39
+ *
40
+ * @name set
41
+ * @access public
42
+ * @param {string | object} name The name to set, or an object of name/value pairs
43
+ * @param {string?} value The value to set the name to
44
+ */
45
+ declare function set(name: string | object, value?: string): void;
46
+ /**
47
+ * Subscribe
48
+ *
49
+ * Adds a callback for a specific name which will be called if the name is
50
+ * added, deleted, or changed
51
+ *
52
+ * @name subscribe
53
+ * @access public
54
+ * @param {string} name The name of the value to watch
55
+ * @param {function} callback The function to call when the value changes
56
+ * @return {void}
57
+ */
58
+ declare function subscribe(name: string, callback: Callback): void;
59
+ /**
60
+ * Unsubscribe
61
+ *
62
+ * Removes a callback from the dCallbacks
63
+ *
64
+ * @name unsubscribe
65
+ * @access public
66
+ * @param {string} name The name of the value to stop watching
67
+ * @param {function} callback The callback to remove
68
+ * @return {void}
69
+ */
70
+ declare function unsubscribe(name: string, callback: Callback): void;
71
+ declare const hash: {
72
+ init: typeof init;
73
+ get: typeof get;
74
+ set: typeof set;
75
+ subscribe: typeof subscribe;
76
+ unsubscribe: typeof unsubscribe;
77
+ };
78
+ export default hash;