Skip to content

Commit 6d820f1

Browse files
authored
Merge pull request #35 from hcodes/onefile
Injected CSS to JS
2 parents 63a7fa2 + 5367454 commit 6d820f1

29 files changed

+5074
-629
lines changed

.eslintignore

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.*
2-
coverage/**
3-
dist/*.js
4-
node_modules/**
5-
tests/**
2+
dist
3+
node_modules
4+
tests

.eslintrc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
{
2-
"rules": {
3-
"semi": [2, "always"],
4-
"quotes": [2, "single"]
5-
},
2+
"parser": "@typescript-eslint/parser",
3+
"plugins": ["@typescript-eslint"],
4+
"extends": [
5+
"eslint:recommended",
6+
"plugin:@typescript-eslint/eslint-recommended",
7+
"plugin:@typescript-eslint/recommended"
8+
],
69
"env": {
710
"browser": true,
811
"node": true,
@@ -14,6 +17,5 @@
1417
},
1518
"globals": {
1619
"showJSError": true
17-
},
18-
"extends": "eslint:recommended"
20+
}
1921
}

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## v4.0.0
4+
- Fixed package.json properties.
5+
- Injected CSS to JS file.
6+
- Removed default export.
7+
- Fixes for Safari.
8+
39
## v3.0.0
410
- Dropped support for old browsers.
511
- Dropped default exports.

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Detail:<br>![Detail](https://raw.githubusercontent.com/hcodes/show-js-error/mast
1717
- Unhandled rejections
1818
- CSP errors
1919
- [Small size](https://bundlephobia.com/package/show-js-error)
20+
- Only one JavaScript file
2021
- No dependencies
2122
- Short and detailed mode
2223
- Links with error hints on MDN and StackOverflow
@@ -40,12 +41,10 @@ npm install show-js-error --save-dev
4041
### Browser
4142
With default settings:
4243
```html
43-
<link rel="stylesheet" href="https://unpkg.com/show-js-error/dist/show-js-error.css" />
4444
<script src="https://unpkg.com/show-js-error/dist/show-js-error.js"></script>
4545
```
4646
or with own settings:
4747
```html
48-
<link rel="stylesheet" href="https://unpkg.com/show-js-error/dist/show-js-error.css" />
4948
<script src="https://unpkg.com/show-js-error/dist/show-js-error.js"></script>
5049
```
5150
```js
@@ -55,23 +54,18 @@ window.showJSError.setSettings({
5554
```
5655

5756
### ES6 or TypeScript
58-
```html
59-
<link rel="stylesheet" href="./node_modules/show-js-error/dist/show-js-error.css" />
60-
```
61-
6257
With default settings:
6358
```js
64-
import { ShowJSError } from 'show-js-error';
65-
const showJSError = new ShowJSError();
59+
import 'show-js-error';
6660
```
6761
or with own settings:
6862
```js
69-
import { ShowJSError } from 'show-js-error';
70-
71-
const showJSError = new ShowJSError();
63+
import { showJSError } from 'show-js-error';
7264
showJSError.setSettings({
7365
reportUrl: 'https://github.com/hcodes/show-js-error/issues/new?title={title}&body={body}'
7466
});
67+
68+
showJSError.show(new Error('error'));
7569
```
7670

7771
## API
@@ -83,6 +77,7 @@ Set settings for error panel.
8377
showJSError.setSettings({
8478
reportUrl: 'https://github.com/hcodes/show-js-error/issues/new?title={title}&body={body}', // Default: ""
8579
templateDetailedMessage: 'My title\n{message}',
80+
size: 'big' // for smart TV
8681
})
8782
```
8883

dist/ShowJSError.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ExtendedError } from './helpers/error';
22
export interface ShowJSErrorSettings {
33
reportUrl?: string;
44
templateDetailedMessage?: string;
5+
size?: 'big' | 'normal';
56
}
67
export interface ShowJSErrorElems {
78
actions: HTMLDivElement;
@@ -28,6 +29,7 @@ export declare class ShowJSError {
2829
private elems;
2930
private settings;
3031
private state;
32+
private styleNode?;
3133
constructor();
3234
destruct(): void;
3335
setSettings(settings: ShowJSErrorSettings): void;

dist/helpers/dom.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export declare function getScreenSize(): string;
22
export declare function getScreenOrientation(): string;
33
export declare function copyTextToClipboard(text: string): void;
4+
export declare function injectStyle(style: string): HTMLStyleElement;

dist/index.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ declare global {
44
showJSError: ShowJSError;
55
}
66
}
7-
declare const showJSError: ShowJSError;
8-
export default showJSError;
7+
export declare const showJSError: ShowJSError;

dist/index.esm.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

dist/show-js-error.esm.js

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/*! show-js-error | © 2023 Denis Seleznev | MIT License | https://github.com/hcodes/show-js-error/ */
12
function getScreenSize() {
23
return [screen.width, screen.height, screen.colorDepth].join('×');
34
}
45
function getScreenOrientation() {
56
return typeof screen.orientation === 'string' ? screen.orientation : screen.orientation.type;
67
}
78
function copyTextToClipboard(text) {
8-
var textarea = document.createElement('textarea');
9+
const textarea = document.createElement('textarea');
910
textarea.value = text;
1011
document.body.appendChild(textarea);
1112
try {
@@ -17,6 +18,12 @@ function copyTextToClipboard(text) {
1718
}
1819
document.body.removeChild(textarea);
1920
}
21+
function injectStyle(style) {
22+
const styleNode = document.createElement('style');
23+
document.body.appendChild(styleNode);
24+
styleNode.textContent = style;
25+
return styleNode;
26+
}
2027

2128
function createElem(data) {
2229
const elem = document.createElement(data.tag || 'div');
@@ -78,6 +85,7 @@ function getFilenameWithPosition(error) {
7885
return text;
7986
}
8087

88+
const STYLE = '.show-js-error{background:#ffc1cc;bottom:15px;color:#000;font-family:Arial,sans-serif;font-size:13px;left:15px;max-width:90vw;min-width:15em;opacity:1;position:fixed;transition:opacity .2s ease-out;transition-delay:0s;visibility:visible;z-index:10000000}.show-js-error_size_big{transform:scale(2) translate(25%,-25%)}.show-js-error_hidden{opacity:0;transition:opacity .3s,visibility 0s linear .3s;visibility:hidden}.show-js-error__title{background:#f66;color:#fff;font-weight:700;padding:4px 30px 4px 7px}.show-js-error__title_no-errors{background:#6b6}.show-js-error__message{cursor:pointer;display:inline}.show-js-error__message:before{background-color:#eee;border-radius:10px;content:"+";display:inline-block;font-size:10px;height:10px;line-height:10px;margin-bottom:2px;margin-right:5px;text-align:center;vertical-align:middle;width:10px}.show-js-error__body_detailed .show-js-error__message:before{content:"-"}.show-js-error__body_no-stack .show-js-error__message:before{display:none}.show-js-error__body_detailed .show-js-error__filename{display:block}.show-js-error__body_no-stack .show-js-error__filename{display:none}.show-js-error__close{color:#fff;cursor:pointer;font-size:20px;line-height:20px;padding:3px;position:absolute;right:2px;top:0}.show-js-error__body{line-height:19px;padding:5px 8px}.show-js-error__body_hidden{display:none}.show-js-error__filename{background:#ffe1ec;border:1px solid #faa;display:none;margin:3px 0 3px -2px;max-height:15em;overflow-y:auto;padding:5px;white-space:pre-wrap}.show-js-error__actions{border-top:1px solid #faa;margin-top:5px;padding:5px 0 3px}.show-js-error__actions_hidden{display:none}.show-js-error__arrows{margin-left:8px;white-space:nowrap}.show-js-error__arrows_hidden{display:none}.show-js-error__copy,.show-js-error__next,.show-js-error__num,.show-js-error__prev,.show-js-error__report{font-size:12px}.show-js-error__report_hidden{display:none}.show-js-error__next{margin-left:1px}.show-js-error__num{margin-left:5px;margin-right:5px}.show-js-error__copy,.show-js-error__report{margin-right:3px}.show-js-error input{padding:1px 2px}.show-js-error a,.show-js-error a:visited{color:#000;text-decoration:underline}.show-js-error a:hover{text-decoration:underline}';
8189
class ShowJSError {
8290
constructor() {
8391
this.elems = {};
@@ -88,7 +96,8 @@ class ShowJSError {
8896
errorBuffer: [],
8997
};
9098
this.onerror = (event) => {
91-
const error = event.error;
99+
const error = event.error ? event.error : event;
100+
console.log(1, event);
92101
this.pushError({
93102
title: 'JavaScript Error',
94103
message: error.message,
@@ -120,6 +129,7 @@ class ShowJSError {
120129
this.appendToBody = () => {
121130
document.removeEventListener('DOMContentLoaded', this.appendToBody, false);
122131
if (this.elems.container) {
132+
this.styleNode = injectStyle(STYLE);
123133
document.body.appendChild(this.elems.container);
124134
}
125135
};
@@ -129,6 +139,7 @@ class ShowJSError {
129139
document.addEventListener('securitypolicyviolation', this.onsecuritypolicyviolation, false);
130140
}
131141
destruct() {
142+
var _a;
132143
window.removeEventListener('error', this.onerror, false);
133144
window.removeEventListener('unhandledrejection', this.onunhandledrejection, false);
134145
document.removeEventListener('securitypolicyviolation', this.onsecuritypolicyviolation, false);
@@ -138,6 +149,10 @@ class ShowJSError {
138149
}
139150
this.state.errorBuffer = [];
140151
this.elems = {};
152+
if (this.styleNode) {
153+
(_a = this.styleNode.parentNode) === null || _a === void 0 ? void 0 : _a.removeChild(this.styleNode);
154+
this.styleNode = undefined;
155+
}
141156
}
142157
setSettings(settings) {
143158
this.settings = this.prepareSettings(settings);
@@ -168,6 +183,7 @@ class ShowJSError {
168183
hide() {
169184
if (this.elems.container) {
170185
this.elems.container.className = buildElemClass('', {
186+
size: this.settings.size,
171187
hidden: true
172188
});
173189
}
@@ -190,6 +206,7 @@ class ShowJSError {
190206
prepareSettings(rawSettings) {
191207
const settings = rawSettings || {};
192208
return {
209+
size: settings.size || 'normal',
193210
reportUrl: settings.reportUrl || '',
194211
templateDetailedMessage: settings.templateDetailedMessage || '',
195212
};
@@ -201,7 +218,9 @@ class ShowJSError {
201218
}
202219
appendUI() {
203220
const container = document.createElement('div');
204-
container.className = buildElemClass('');
221+
container.className = buildElemClass('', {
222+
size: this.settings.size,
223+
});
205224
this.elems.container = container;
206225
this.elems.close = createElem({
207226
name: 'close',
@@ -241,6 +260,7 @@ class ShowJSError {
241260
this.createActions(body);
242261
if (document.body) {
243262
document.body.appendChild(container);
263+
this.styleNode = injectStyle(STYLE);
244264
}
245265
else {
246266
document.addEventListener('DOMContentLoaded', this.appendToBody, false);
@@ -349,7 +369,9 @@ class ShowJSError {
349369
}
350370
showUI() {
351371
if (this.elems.container) {
352-
this.elems.container.className = buildElemClass('');
372+
this.elems.container.className = buildElemClass('', {
373+
size: this.settings.size,
374+
});
353375
}
354376
}
355377
hasStack() {
@@ -431,4 +453,7 @@ class ShowJSError {
431453
}
432454
}
433455

434-
export { ShowJSError };
456+
const showJSError = new ShowJSError();
457+
window.showJSError = showJSError;
458+
459+
export { showJSError };

0 commit comments

Comments
 (0)