errorbuttons 1.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/README.md +3 -0
- package/index.js +115 -0
- package/package.json +11 -0
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import buttonizer from 'buttonizer';
|
|
2
|
+
|
|
3
|
+
const defaultDefaultStyles = {
|
|
4
|
+
borderRadius: '8px',
|
|
5
|
+
border: '1px solid black',
|
|
6
|
+
padding: '10px 12px',
|
|
7
|
+
fontSize: '14px',
|
|
8
|
+
fontWeight: 'bold',
|
|
9
|
+
backgroundColor: '#fdd',
|
|
10
|
+
cursor: 'pointer',
|
|
11
|
+
outlineOffset: '4px',
|
|
12
|
+
userSelect: 'none',
|
|
13
|
+
fontFamily: 'sans-serif',
|
|
14
|
+
pointerEvents: 'auto',
|
|
15
|
+
textAlign: 'right',
|
|
16
|
+
display: 'flex',
|
|
17
|
+
flexDirection: 'column',
|
|
18
|
+
gap: '4px',
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const defaultHoverStyles = {
|
|
22
|
+
backgroundColor: '#fbb',
|
|
23
|
+
outline: '2px solid #fbb',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const defaultFocusStyles = { outline: '2px solid #f99' };
|
|
27
|
+
|
|
28
|
+
const defaultPressedStyles = { backgroundColor: '#f99' };
|
|
29
|
+
|
|
30
|
+
const defaultOptionalStyles = {
|
|
31
|
+
hover: defaultHoverStyles,
|
|
32
|
+
focus: defaultFocusStyles,
|
|
33
|
+
default: defaultDefaultStyles,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const defaultTsStyles = {
|
|
37
|
+
fontWeight: 'normal',
|
|
38
|
+
fontSize: '12px'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const defaultHolderStyles = {
|
|
42
|
+
position: 'fixed',
|
|
43
|
+
top: 0,
|
|
44
|
+
bottom: 0,
|
|
45
|
+
right: 0,
|
|
46
|
+
left: 0,
|
|
47
|
+
padding: '10px 12px',
|
|
48
|
+
display: 'flex',
|
|
49
|
+
flexDirection: 'row-reverse',
|
|
50
|
+
flexWrap: 'wrap-reverse',
|
|
51
|
+
justifyContent: 'end',
|
|
52
|
+
alignContent: 'end',
|
|
53
|
+
alignItems: 'end',
|
|
54
|
+
gap: '12px',
|
|
55
|
+
pointerEvents: 'none'
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const defaultSpanStyles = {
|
|
59
|
+
maxWidth: '400px',
|
|
60
|
+
wordBreak: 'break-word'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const setupErrorButtons = (myError, options={}) => {
|
|
64
|
+
const pressedStyles=options.pressedStyles || defaultPressedStyles;
|
|
65
|
+
const optionalStyles=options.optionalStyles || defaultOptionalStyles;
|
|
66
|
+
const tsStyles=options.tsStyles || defaultTsStyles;
|
|
67
|
+
const holderStyles=options.holderStyles || defaultHolderStyles;
|
|
68
|
+
const idValue=options.idValue || 'statusbuttonerrholder';
|
|
69
|
+
const title=options.title || `Copy to clipboard and dismiss`;
|
|
70
|
+
const spanStyles =options.spanStyles || defaultSpanStyles;
|
|
71
|
+
let errHolder = document.getElementById(idValue);
|
|
72
|
+
if (!errHolder) {
|
|
73
|
+
errHolder = document.createElement('div');
|
|
74
|
+
errHolder.id = idValue;
|
|
75
|
+
Object.assign(errHolder.style, holderStyles);
|
|
76
|
+
document.body.appendChild(errHolder);
|
|
77
|
+
}
|
|
78
|
+
let statusButton = document.createElement('button');
|
|
79
|
+
statusButton.type = 'button';
|
|
80
|
+
statusButton.title = title;
|
|
81
|
+
let errUrlSpan = document.createElement('span');
|
|
82
|
+
errUrlSpan.textContent = myError instanceof Response ? myError.url : myError instanceof ErrorEvent ? `${myError.filename} - ${myError.lineno}:${myError.colno}` : myError instanceof Error ? `${myError.name}` : myError instanceof PromiseRejectionEvent ? `Unhandled Promise Rejection` : Array.isArray(myError) ? myError[0] : myError instanceof string ? myError : myError.toString();
|
|
83
|
+
let errStatusSpan = document.createElement('span');
|
|
84
|
+
errStatusSpan.textContent = myError instanceof Response ? `${myError.status} - ${myError.statusText}` : myError instanceof ErrorEvent ? `${myError.message}` : myError instanceof Error ? `${myError.message}` : myError instanceof PromiseRejectionEvent ? `${myError.reason}` : Array.isArray(myError) ? myError[1] : myError instanceof string ? myError : myError.toString();
|
|
85
|
+
let errTimestamp = document.createElement('span');
|
|
86
|
+
Object.assign(errUrlSpan.style, spanStyles);
|
|
87
|
+
Object.assign(errStatusSpan.style, spanStyles);
|
|
88
|
+
Object.assign(errTimestamp.style, spanStyles);
|
|
89
|
+
Object.assign(errTimestamp.style, tsStyles);
|
|
90
|
+
errTimestamp.textContent = `${new Date().toLocaleString()}`;
|
|
91
|
+
statusButton.appendChild(errUrlSpan);
|
|
92
|
+
statusButton.appendChild(errStatusSpan);
|
|
93
|
+
statusButton.appendChild(errTimestamp);
|
|
94
|
+
statusButton.addEventListener('click', async function (e) {
|
|
95
|
+
const parsedError = {
|
|
96
|
+
url: myError.url,
|
|
97
|
+
filename: myError.filename,
|
|
98
|
+
colno: myError.colno,
|
|
99
|
+
lineno: myError.lineno,
|
|
100
|
+
name: myError.name,
|
|
101
|
+
status: myError.status,
|
|
102
|
+
statusText: myError.statusText,
|
|
103
|
+
message: myError.message,
|
|
104
|
+
reason: myError.reason,
|
|
105
|
+
timestamp: errTimestamp.textContent
|
|
106
|
+
}
|
|
107
|
+
navigator.clipboard.writeText(JSON.stringify(parsedError, null, 2));
|
|
108
|
+
statusButton.remove();
|
|
109
|
+
});
|
|
110
|
+
buttonizer(statusButton, pressedStyles, optionalStyles, false, false);
|
|
111
|
+
errHolder.appendChild(statusButton);
|
|
112
|
+
return statusButton;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export default setupErrorButtons;
|