noinspect 0.0.1 → 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/CHANGELOG.md +20 -0
- package/LICENSE +21 -0
- package/README.md +193 -4
- package/dist/index.d.ts +49 -0
- package/dist/index.js +132 -0
- package/dist/noinspect.global.js +1 -0
- package/package.json +77 -4
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to noinspect are listed here. The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the project uses [semantic versioning](https://semver.org/).
|
|
4
|
+
|
|
5
|
+
## [1.0.0] - 2026-12-05
|
|
6
|
+
|
|
7
|
+
First public release.
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `protect(options?)`: blocks the DevTools and view-source shortcuts (F12, Ctrl+Shift+I/J/C/K, Ctrl+U, and Cmd+Option+I/J/C/U, Cmd+Shift+C and Cmd+U on a Mac), matched by physical key. Returns a function that turns it off.
|
|
12
|
+
- `contextmenu`: optionally blocks the right-click menu, except in form fields.
|
|
13
|
+
- Detection of DevTools docked to the side or bottom, on resize and on a timer: `blur`, `redirect`, `onOpen` and `onClose`. Zooming in is not mistaken for DevTools; detection is off inside iframes.
|
|
14
|
+
- `message` (announced to screen readers) and `onBlock`.
|
|
15
|
+
- `looksOpen()` and `shortcutOf(event)` helpers.
|
|
16
|
+
- Classic `<script>` build for any site, configured with `data-` attributes, with `data-manual` to call it yourself.
|
|
17
|
+
- Safe during server-side rendering. About 1.1 kB minified and gzipped, zero dependencies, typed.
|
|
18
|
+
- Tests in jsdom, and in real Chromium and WebKit.
|
|
19
|
+
|
|
20
|
+
[1.0.0]: https://github.com/ckgiri/noinspect/releases/tag/v1.0.0
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ckgiri
|
|
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
CHANGED
|
@@ -1,7 +1,196 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<picture>
|
|
4
|
+
<source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/ckgiri/noinspect/main/assets/logo-dark.svg">
|
|
5
|
+
<img src="https://raw.githubusercontent.com/ckgiri/noinspect/main/assets/logo.svg" alt="noinspect logo" width="96" height="96">
|
|
6
|
+
</picture>
|
|
7
|
+
|
|
1
8
|
# noinspect
|
|
2
9
|
|
|
3
|
-
|
|
10
|
+
### Discourage opening DevTools on your site
|
|
11
|
+
|
|
12
|
+
Make F12, Ctrl+Shift+I and "view source" do nothing, and notice when DevTools are open to blur the page or run your code.<br>
|
|
13
|
+
One script tag or one import. Typing, forms and accessibility keep working.
|
|
14
|
+
|
|
15
|
+
[](https://www.npmjs.com/package/noinspect)
|
|
16
|
+

|
|
17
|
+

|
|
18
|
+
[](LICENSE)
|
|
19
|
+
|
|
20
|
+
**F12** · **Ctrl+Shift+I/J/C** · **view source** · **Mac shortcuts** · **blur when open** · **one script tag** · **any framework**
|
|
21
|
+
|
|
22
|
+
[**Live demo**](https://ckgiri.github.io/noinspect/) · [Quick start](#-quick-start) · [What it does](#-what-it-does) · [Options](#%EF%B8%8F-options) · [Script tag](#-script-tag-options) · [Frameworks](#-frameworks) · [What it cannot do](#-what-it-cannot-do) · [FAQ](#-faq)
|
|
23
|
+
|
|
24
|
+
</div>
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
## 🚀 Quick start
|
|
29
|
+
|
|
30
|
+
**Plain HTML, WordPress, Shopify, Webflow, any site:** paste this into the `<head>`.
|
|
31
|
+
|
|
32
|
+
```html
|
|
33
|
+
<script src="https://cdn.jsdelivr.net/npm/noinspect@1"></script>
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
That's it: the DevTools and view-source shortcuts stop working on your page. Want more? Add [data attributes](#-script-tag-options):
|
|
37
|
+
|
|
38
|
+
```html
|
|
39
|
+
<script src="https://cdn.jsdelivr.net/npm/noinspect@1" data-blur data-contextmenu></script>
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
**With a bundler:**
|
|
43
|
+
|
|
44
|
+
```sh
|
|
45
|
+
npm install noinspect
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { protect } from 'noinspect';
|
|
50
|
+
|
|
51
|
+
protect(); // F12, Ctrl+Shift+I/J/C, Ctrl+U do nothing
|
|
52
|
+
protect({ blur: true }); // also blur the page while DevTools are open
|
|
53
|
+
protect({ onOpen: () => console.clear() }); // or run your own code
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
`protect()` returns a function that turns protection off again.
|
|
57
|
+
|
|
58
|
+
## 🎯 What it does
|
|
59
|
+
|
|
60
|
+
| Shortcut | Opens | Blocked |
|
|
61
|
+
| ---------------------------------------------------------- | --------------- | ------------------------ |
|
|
62
|
+
| `F12` | DevTools | ✅ |
|
|
63
|
+
| `Ctrl+Shift+I` · `Cmd+Option+I` | DevTools | ✅ |
|
|
64
|
+
| `Ctrl+Shift+J` · `Cmd+Option+J` · `Ctrl+Shift+K` (Firefox) | Console | ✅ |
|
|
65
|
+
| `Ctrl+Shift+C` · `Cmd+Option+C` · `Cmd+Shift+C` | Inspect element | ✅ |
|
|
66
|
+
| `Ctrl+U` · `Cmd+Option+U` · `Cmd+U` (Firefox on Mac) | View source | ✅ |
|
|
67
|
+
| Right-click → Inspect | Inspect element | with `contextmenu: true` |
|
|
68
|
+
|
|
69
|
+
Shortcuts are matched by physical key, so they work on any keyboard layout. Ordinary shortcuts (copy, paste, italic) and typing are never touched.
|
|
70
|
+
|
|
71
|
+
**Noticing open DevTools.** DevTools docked to the side or bottom of the window make the page much narrower or shorter than the window. noinspect checks for that when the window is resized and once a second, and then blurs the page, redirects, or calls your `onOpen`. When DevTools close, the blur goes away and `onClose` is called. Zooming in shrinks the page in both directions, so zoom is not mistaken for DevTools.
|
|
72
|
+
|
|
73
|
+
About 1.1 kB minified and gzipped, no dependencies, typed, safe during server-side rendering, and tested in real Chromium and WebKit on every change.
|
|
74
|
+
|
|
75
|
+
## ⚙️ Options
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
protect({
|
|
79
|
+
keys: true, // block the DevTools and view-source shortcuts
|
|
80
|
+
contextmenu: false, // block the right-click menu (it has "Inspect"); form fields keep theirs
|
|
81
|
+
blur: false, // blur the page while DevTools are open
|
|
82
|
+
redirect: '/please-close-devtools', // go to this URL when DevTools open
|
|
83
|
+
onOpen: () => {}, // called when DevTools open
|
|
84
|
+
onClose: () => {}, // called when they close again
|
|
85
|
+
interval: 1000, // how often to check, in milliseconds
|
|
86
|
+
threshold: 160, // pixels between the window and the page that count as DevTools
|
|
87
|
+
message: 'Developer tools are disabled', // shown briefly when a shortcut is blocked
|
|
88
|
+
onBlock: (shortcut, event) => {}, // 'devtools' | 'console' | 'elements' | 'view-source' | 'contextmenu'
|
|
89
|
+
});
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Detection only runs when `blur`, `redirect`, `onOpen` or `onClose` is set.
|
|
93
|
+
|
|
94
|
+
Two helpers are exported too:
|
|
95
|
+
|
|
96
|
+
```js
|
|
97
|
+
import { looksOpen, shortcutOf } from 'noinspect';
|
|
98
|
+
|
|
99
|
+
looksOpen(); // true if DevTools look open right now
|
|
100
|
+
shortcutOf(event); // 'devtools', 'console', 'elements', 'view-source' or undefined for a keydown event
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## 🏷️ Script tag options
|
|
104
|
+
|
|
105
|
+
The script tag reads the same options from `data-` attributes. A flag is on when present, and off with `="false"`.
|
|
106
|
+
|
|
107
|
+
| Attribute | Same as |
|
|
108
|
+
| --------------------------------------------- | ----------------------------------- |
|
|
109
|
+
| `data-blur` `data-contextmenu` | `blur: true`, `contextmenu: true` |
|
|
110
|
+
| `data-keys="false"` | `keys: false` |
|
|
111
|
+
| `data-redirect="/closed.html"` | `redirect` |
|
|
112
|
+
| `data-message="Developer tools are disabled"` | `message` |
|
|
113
|
+
| `data-interval="500"` `data-threshold="200"` | `interval`, `threshold` |
|
|
114
|
+
| `data-manual` | don't protect yet; call it yourself |
|
|
115
|
+
|
|
116
|
+
With `data-manual`, the script only defines `window.noinspect`:
|
|
117
|
+
|
|
118
|
+
```html
|
|
119
|
+
<script src="https://cdn.jsdelivr.net/npm/noinspect@1" data-manual></script>
|
|
120
|
+
<script>
|
|
121
|
+
noinspect.protect({ onOpen: () => (document.title = '👀') });
|
|
122
|
+
</script>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## 🧩 Frameworks
|
|
126
|
+
|
|
127
|
+
**React**
|
|
128
|
+
|
|
129
|
+
```jsx
|
|
130
|
+
import { useEffect } from 'react';
|
|
131
|
+
import { protect } from 'noinspect';
|
|
132
|
+
|
|
133
|
+
export function App() {
|
|
134
|
+
useEffect(() => protect({ blur: true }), []);
|
|
135
|
+
return <Main />;
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
**Vue**
|
|
140
|
+
|
|
141
|
+
```js
|
|
142
|
+
import { onMounted, onUnmounted } from 'vue';
|
|
143
|
+
import { protect } from 'noinspect';
|
|
144
|
+
|
|
145
|
+
let off;
|
|
146
|
+
onMounted(() => (off = protect()));
|
|
147
|
+
onUnmounted(() => off());
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Svelte**
|
|
151
|
+
|
|
152
|
+
```svelte
|
|
153
|
+
<script>
|
|
154
|
+
import { onMount } from 'svelte';
|
|
155
|
+
import { protect } from 'noinspect';
|
|
156
|
+
onMount(() => protect());
|
|
157
|
+
</script>
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
**Next.js / Nuxt / Astro:** call `protect()` in client code (an effect, `onMounted`, or a `<script>`). On the server it does nothing.
|
|
161
|
+
|
|
162
|
+
## 🙏 What it cannot do
|
|
163
|
+
|
|
164
|
+
Be honest with yourself and your clients: **this is a deterrent, not security.**
|
|
165
|
+
|
|
166
|
+
- DevTools can always be opened from the browser's menu, and some browsers don't let pages block every shortcut.
|
|
167
|
+
- DevTools in a separate window are not noticed, and nothing works with JavaScript turned off.
|
|
168
|
+
- Everything the browser downloads can be read without DevTools: view-source: URLs, saving the page, a proxy, `curl`.
|
|
169
|
+
|
|
170
|
+
noinspect stops the curious visitor who presses F12, which is most of them. Secrets (API keys, prices you haven't announced, answers to a quiz) must never be sent to the browser at all; check them on your server.
|
|
171
|
+
|
|
172
|
+
Don't use it on sites where developers are your audience: it will annoy them, and it gets in the way of people debugging problems on their own machine.
|
|
173
|
+
|
|
174
|
+
## ♿ Accessibility
|
|
175
|
+
|
|
176
|
+
- Typing, keyboard navigation, screen readers and browser zoom are never touched. Only the shortcuts in the table above are blocked.
|
|
177
|
+
- With `contextmenu: true`, inputs, text areas, selects and editable areas keep their right-click menu, for paste and spell-check.
|
|
178
|
+
- The `message` is a `role="status"` element, so screen readers announce it.
|
|
179
|
+
|
|
180
|
+
## ❓ FAQ
|
|
181
|
+
|
|
182
|
+
**Will the page blur for a visitor who isn't using DevTools?**
|
|
183
|
+
It shouldn't. A normal window, a narrow window, zooming in or out and most browser sidebars don't trigger it. A browser sidebar wider than 160 pixels, or tall toolbars plus an info bar, could; raise `threshold` if you see that. Inside iframes detection is turned off, because the window size there belongs to the parent page.
|
|
184
|
+
|
|
185
|
+
**Does it work on phones?**
|
|
186
|
+
Phones don't have DevTools shortcuts or docked DevTools, so there's nothing to block. It does no harm there.
|
|
187
|
+
|
|
188
|
+
**Does it hurt SEO?**
|
|
189
|
+
No. Search engines read your HTML, and noinspect changes nothing on the page unless DevTools are open.
|
|
190
|
+
|
|
191
|
+
**Can I also stop right-click, copying and image saving?**
|
|
192
|
+
Use [norightclick](https://www.npmjs.com/package/norightclick), the companion package. Both work together.
|
|
193
|
+
|
|
194
|
+
## 📄 License
|
|
4
195
|
|
|
5
|
-
|
|
6
|
-
- Detects when DevTools opens and lets you react: a message, a redirect, or hiding content
|
|
7
|
-
- Tiny, zero dependencies, plain HTML or any framework
|
|
196
|
+
[MIT](LICENSE)
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* noinspect: discourage visitors from opening the browser's developer tools on your page.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { protect } from 'noinspect';
|
|
6
|
+
*
|
|
7
|
+
* protect(); // F12, Ctrl+Shift+I/J/C, view source shortcuts do nothing
|
|
8
|
+
* protect({ blur: true }); // also blur the page while DevTools look open
|
|
9
|
+
* protect({ onOpen: () => alert('Please close DevTools') });
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* This deters casual visitors only. DevTools can always be opened from the browser menu, and anything the browser
|
|
13
|
+
* shows can be read some other way. Detection is a heuristic: it notices DevTools docked to the side or bottom.
|
|
14
|
+
*/
|
|
15
|
+
/** A blocked shortcut, passed to `onBlock`. */
|
|
16
|
+
export type Shortcut = 'devtools' | 'console' | 'elements' | 'view-source' | 'contextmenu';
|
|
17
|
+
export interface Options {
|
|
18
|
+
/** Block the DevTools and view-source keyboard shortcuts. Default `true`. */
|
|
19
|
+
keys?: boolean;
|
|
20
|
+
/** Block the right-click menu (it has "Inspect"). Default `false`; use norightclick for finer control. */
|
|
21
|
+
contextmenu?: boolean;
|
|
22
|
+
/** Blur the page while DevTools look open. Default `false`. */
|
|
23
|
+
blur?: boolean;
|
|
24
|
+
/** Go to this URL when DevTools look open. */
|
|
25
|
+
redirect?: string;
|
|
26
|
+
/** Called when DevTools look open. */
|
|
27
|
+
onOpen?: () => void;
|
|
28
|
+
/** Called when DevTools look closed again. */
|
|
29
|
+
onClose?: () => void;
|
|
30
|
+
/** How often to check, in milliseconds. Default `1000`. Size changes are also checked as they happen. */
|
|
31
|
+
interval?: number;
|
|
32
|
+
/** Pixels between the window and the page that count as DevTools. Default `160`. */
|
|
33
|
+
threshold?: number;
|
|
34
|
+
/** A short message shown when a shortcut is blocked, e.g. "Developer tools are disabled". */
|
|
35
|
+
message?: string;
|
|
36
|
+
/** Called when a shortcut is blocked. */
|
|
37
|
+
onBlock?: (shortcut: Shortcut, event: Event) => void;
|
|
38
|
+
}
|
|
39
|
+
/** Which DevTools or view-source shortcut a key press is, if any. */
|
|
40
|
+
export declare function shortcutOf(event: KeyboardEvent): Shortcut | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Whether DevTools look open: docked DevTools make the page much narrower or much shorter than the window. Zooming in
|
|
43
|
+
* shrinks the page in both directions, so a big gap on both sides counts as zoom, not DevTools. Undocked DevTools are
|
|
44
|
+
* not noticed. Always false inside iframes and where the browser reports no window size (some mobile browsers).
|
|
45
|
+
*/
|
|
46
|
+
export declare function looksOpen(threshold?: number): boolean;
|
|
47
|
+
/** Turns on protection. Returns a function that turns it off again. */
|
|
48
|
+
export declare function protect(options?: Options): () => void;
|
|
49
|
+
export default protect;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* noinspect: discourage visitors from opening the browser's developer tools on your page.
|
|
3
|
+
*
|
|
4
|
+
* ```js
|
|
5
|
+
* import { protect } from 'noinspect';
|
|
6
|
+
*
|
|
7
|
+
* protect(); // F12, Ctrl+Shift+I/J/C, view source shortcuts do nothing
|
|
8
|
+
* protect({ blur: true }); // also blur the page while DevTools look open
|
|
9
|
+
* protect({ onOpen: () => alert('Please close DevTools') });
|
|
10
|
+
* ```
|
|
11
|
+
*
|
|
12
|
+
* This deters casual visitors only. DevTools can always be opened from the browser menu, and anything the browser
|
|
13
|
+
* shows can be read some other way. Detection is a heuristic: it notices DevTools docked to the side or bottom.
|
|
14
|
+
*/
|
|
15
|
+
/** Shortcuts by physical key (`event.code`), so they work on any keyboard layout and with Option on a Mac. */
|
|
16
|
+
const LETTERS = { KeyI: 'devtools', KeyJ: 'console', KeyC: 'elements', KeyK: 'console' };
|
|
17
|
+
/** Which DevTools or view-source shortcut a key press is, if any. */
|
|
18
|
+
export function shortcutOf(event) {
|
|
19
|
+
if (event.key === 'F12')
|
|
20
|
+
return 'devtools';
|
|
21
|
+
const mac = event.metaKey && event.altKey; // Cmd+Option+I/J/C/U
|
|
22
|
+
const other = event.ctrlKey && event.shiftKey; // Ctrl+Shift+I/J/C/K
|
|
23
|
+
if ((mac || other) && LETTERS[event.code])
|
|
24
|
+
return LETTERS[event.code];
|
|
25
|
+
if (event.metaKey && event.shiftKey && event.code === 'KeyC')
|
|
26
|
+
return 'elements'; // Cmd+Shift+C in Chrome on a Mac
|
|
27
|
+
// Ctrl+U, Cmd+U (Firefox on Mac), Cmd+Option+U (Safari and Chrome on Mac)
|
|
28
|
+
if (event.code === 'KeyU' && (event.ctrlKey || event.metaKey) && !event.shiftKey)
|
|
29
|
+
return 'view-source';
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether DevTools look open: docked DevTools make the page much narrower or much shorter than the window. Zooming in
|
|
34
|
+
* shrinks the page in both directions, so a big gap on both sides counts as zoom, not DevTools. Undocked DevTools are
|
|
35
|
+
* not noticed. Always false inside iframes and where the browser reports no window size (some mobile browsers).
|
|
36
|
+
*/
|
|
37
|
+
export function looksOpen(threshold = 160) {
|
|
38
|
+
if (typeof window === 'undefined' || window.top !== window.self || !window.outerWidth)
|
|
39
|
+
return false;
|
|
40
|
+
const side = window.outerWidth - window.innerWidth > threshold;
|
|
41
|
+
const bottom = window.outerHeight - window.innerHeight > threshold;
|
|
42
|
+
return side !== bottom;
|
|
43
|
+
}
|
|
44
|
+
let toast;
|
|
45
|
+
let toastTimer;
|
|
46
|
+
function showMessage(text) {
|
|
47
|
+
const doc = window.document;
|
|
48
|
+
if (!toast) {
|
|
49
|
+
toast = doc.createElement('div');
|
|
50
|
+
toast.setAttribute('role', 'status');
|
|
51
|
+
toast.style.cssText =
|
|
52
|
+
'position:fixed;left:50%;bottom:24px;transform:translateX(-50%);z-index:2147483647;max-width:90vw;' +
|
|
53
|
+
'padding:10px 16px;border-radius:8px;background:#111;color:#fff;font:14px/1.4 system-ui,sans-serif;' +
|
|
54
|
+
'box-shadow:0 4px 16px rgba(0,0,0,.25);pointer-events:none';
|
|
55
|
+
}
|
|
56
|
+
toast.textContent = text;
|
|
57
|
+
if (!toast.isConnected)
|
|
58
|
+
doc.body.appendChild(toast);
|
|
59
|
+
clearTimeout(toastTimer);
|
|
60
|
+
toastTimer = setTimeout(() => toast?.remove(), 2000);
|
|
61
|
+
}
|
|
62
|
+
/** Turns on protection. Returns a function that turns it off again. */
|
|
63
|
+
export function protect(options = {}) {
|
|
64
|
+
if (typeof window === 'undefined' || !window.document)
|
|
65
|
+
return () => undefined;
|
|
66
|
+
const doc = window.document;
|
|
67
|
+
const { keys = true, contextmenu = false, blur = false, redirect, onOpen, onClose, interval = 1000, threshold = 160, message, onBlock, } = options;
|
|
68
|
+
const block = (shortcut, event) => {
|
|
69
|
+
event.preventDefault();
|
|
70
|
+
if (message)
|
|
71
|
+
showMessage(message);
|
|
72
|
+
onBlock?.(shortcut, event);
|
|
73
|
+
};
|
|
74
|
+
const onKey = (event) => {
|
|
75
|
+
const shortcut = shortcutOf(event);
|
|
76
|
+
if (shortcut)
|
|
77
|
+
block(shortcut, event);
|
|
78
|
+
};
|
|
79
|
+
const onMenu = (event) => {
|
|
80
|
+
const el = event.target instanceof window.Element ? event.target : null;
|
|
81
|
+
// Form fields keep their menu, for paste and spell-check.
|
|
82
|
+
if (!el?.closest('input,textarea,select,[contenteditable]:not([contenteditable="false"])')) {
|
|
83
|
+
block('contextmenu', event);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
if (keys)
|
|
87
|
+
window.addEventListener('keydown', onKey, true);
|
|
88
|
+
if (contextmenu)
|
|
89
|
+
window.addEventListener('contextmenu', onMenu, true);
|
|
90
|
+
// Detection only runs when something should happen.
|
|
91
|
+
const detect = blur || redirect || onOpen || onClose;
|
|
92
|
+
let open = false;
|
|
93
|
+
let style;
|
|
94
|
+
const check = () => {
|
|
95
|
+
const now = looksOpen(threshold);
|
|
96
|
+
if (now === open)
|
|
97
|
+
return;
|
|
98
|
+
open = now;
|
|
99
|
+
if (open) {
|
|
100
|
+
if (blur) {
|
|
101
|
+
style = doc.createElement('style');
|
|
102
|
+
style.textContent = 'body{filter:blur(16px)!important;pointer-events:none!important}';
|
|
103
|
+
doc.head.appendChild(style);
|
|
104
|
+
}
|
|
105
|
+
onOpen?.();
|
|
106
|
+
if (redirect)
|
|
107
|
+
window.location.href = redirect;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
style?.remove();
|
|
111
|
+
onClose?.();
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
let timer;
|
|
115
|
+
if (detect) {
|
|
116
|
+
window.addEventListener('resize', check);
|
|
117
|
+
timer = setInterval(check, interval);
|
|
118
|
+
check();
|
|
119
|
+
}
|
|
120
|
+
let active = true;
|
|
121
|
+
return () => {
|
|
122
|
+
if (!active)
|
|
123
|
+
return;
|
|
124
|
+
active = false;
|
|
125
|
+
window.removeEventListener('keydown', onKey, true);
|
|
126
|
+
window.removeEventListener('contextmenu', onMenu, true);
|
|
127
|
+
window.removeEventListener('resize', check);
|
|
128
|
+
clearInterval(timer);
|
|
129
|
+
style?.remove();
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
export default protect;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";(()=>{var T={KeyI:"devtools",KeyJ:"console",KeyC:"elements",KeyK:"console"};function m(e){if(e.key==="F12")return"devtools";let t=e.metaKey&&e.altKey,s=e.ctrlKey&&e.shiftKey;if((t||s)&&T[e.code])return T[e.code];if(e.metaKey&&e.shiftKey&&e.code==="KeyC")return"elements";if(e.code==="KeyU"&&(e.ctrlKey||e.metaKey)&&!e.shiftKey)return"view-source"}function w(e=160){if(typeof window=="undefined"||window.top!==window.self||!window.outerWidth)return!1;let t=window.outerWidth-window.innerWidth>e,s=window.outerHeight-window.innerHeight>e;return t!==s}var o,L;function H(e){let t=window.document;o||(o=t.createElement("div"),o.setAttribute("role","status"),o.style.cssText="position:fixed;left:50%;bottom:24px;transform:translateX(-50%);z-index:2147483647;max-width:90vw;padding:10px 16px;border-radius:8px;background:#111;color:#fff;font:14px/1.4 system-ui,sans-serif;box-shadow:0 4px 16px rgba(0,0,0,.25);pointer-events:none"),o.textContent=e,o.isConnected||t.body.appendChild(o),clearTimeout(L),L=setTimeout(()=>o==null?void 0:o.remove(),2e3)}function y(e={}){if(typeof window=="undefined"||!window.document)return()=>{};let t=window.document,{keys:s=!0,contextmenu:d=!1,blur:x=!1,redirect:f,onOpen:c,onClose:u,interval:S=1e3,threshold:C=160,message:v,onBlock:a}=e,b=(n,r)=>{r.preventDefault(),v&&H(v),a==null||a(n,r)},E=n=>{let r=m(n);r&&b(r,n)},g=n=>{let r=n.target instanceof window.Element?n.target:null;r!=null&&r.closest('input,textarea,select,[contenteditable]:not([contenteditable="false"])')||b("contextmenu",n)};s&&window.addEventListener("keydown",E,!0),d&&window.addEventListener("contextmenu",g,!0);let M=x||f||c||u,p=!1,i,l=()=>{let n=w(C);n!==p&&(p=n,p?(x&&(i=t.createElement("style"),i.textContent="body{filter:blur(16px)!important;pointer-events:none!important}",t.head.appendChild(i)),c==null||c(),f&&(window.location.href=f)):(i==null||i.remove(),u==null||u()))},K;M&&(window.addEventListener("resize",l),K=setInterval(l,S),l());let k=!0;return()=>{k&&(k=!1,window.removeEventListener("keydown",E,!0),window.removeEventListener("contextmenu",g,!0),window.removeEventListener("resize",l),clearInterval(K),i==null||i.remove())}}function O(e){let t={};for(let s of["keys","contextmenu","blur"]){let d=e[s];d!==void 0&&(t[s]=d!=="false")}return e.redirect&&(t.redirect=e.redirect),e.message&&(t.message=e.message),e.interval&&(t.interval=Number(e.interval)),e.threshold&&(t.threshold=Number(e.threshold)),t}var h=typeof document=="undefined"?null:document.currentScript,I={protect:y,looksOpen:w,shortcutOf:m,optionsFromDataset:O};window.noinspect=I;h&&h.dataset.manual===void 0&&y(O(h.dataset));})();
|
package/package.json
CHANGED
|
@@ -1,8 +1,81 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "noinspect",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Discourage opening browser DevTools: block F12, Ctrl+Shift+I and view-source shortcuts, and notice docked DevTools to blur the page or run your code. One script tag or one import; tiny, zero dependencies.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"devtools",
|
|
7
|
+
"disable-devtools",
|
|
8
|
+
"disable-f12",
|
|
9
|
+
"f12",
|
|
10
|
+
"inspect-element",
|
|
11
|
+
"disable-inspect",
|
|
12
|
+
"view-source",
|
|
13
|
+
"devtools-detect",
|
|
14
|
+
"detect-devtools",
|
|
15
|
+
"content-protection",
|
|
16
|
+
"anti-debug",
|
|
17
|
+
"wordpress"
|
|
18
|
+
],
|
|
19
|
+
"homepage": "https://github.com/ckgiri/noinspect#readme",
|
|
20
|
+
"bugs": {
|
|
21
|
+
"url": "https://github.com/ckgiri/noinspect/issues"
|
|
22
|
+
},
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/ckgiri/noinspect.git"
|
|
26
|
+
},
|
|
27
|
+
"funding": "https://github.com/sponsors/ckgiri",
|
|
5
28
|
"license": "MIT",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
29
|
+
"type": "module",
|
|
30
|
+
"sideEffects": [
|
|
31
|
+
"./dist/noinspect.global.js"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"default": "./dist/index.js"
|
|
37
|
+
},
|
|
38
|
+
"./global": "./dist/noinspect.global.js",
|
|
39
|
+
"./package.json": "./package.json"
|
|
40
|
+
},
|
|
41
|
+
"main": "./dist/index.js",
|
|
42
|
+
"types": "./dist/index.d.ts",
|
|
43
|
+
"unpkg": "./dist/noinspect.global.js",
|
|
44
|
+
"jsdelivr": "./dist/noinspect.global.js",
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"README.md",
|
|
48
|
+
"CHANGELOG.md",
|
|
49
|
+
"LICENSE"
|
|
50
|
+
],
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "node scripts/clean.mjs && tsc -p tsconfig.json && node scripts/build-global.mjs",
|
|
53
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
54
|
+
"lint": "eslint .",
|
|
55
|
+
"format": "prettier --write .",
|
|
56
|
+
"format:check": "prettier --check .",
|
|
57
|
+
"test": "npm run build && node --test \"test/*.test.js\"",
|
|
58
|
+
"test:coverage": "npm run build && node --test --experimental-test-coverage --test-coverage-include=\"dist/**\" --test-coverage-lines=95 --test-coverage-branches=90 --test-coverage-functions=100 \"test/*.test.js\"",
|
|
59
|
+
"test:browser": "npm run build && node test/browser/run.mjs",
|
|
60
|
+
"size": "npm run build && node scripts/size.mjs",
|
|
61
|
+
"check:package": "npm run build && publint --strict && attw --pack . --profile esm-only --exclude-entrypoints global",
|
|
62
|
+
"check": "npm run lint && npm run format:check && npm run typecheck && npm run test:coverage && npm run size && npm run check:package",
|
|
63
|
+
"prepublishOnly": "npm run check",
|
|
64
|
+
"docs": "npm run build && node scripts/docs.mjs"
|
|
65
|
+
},
|
|
66
|
+
"publishConfig": {
|
|
67
|
+
"access": "public"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@arethetypeswrong/cli": "^0.18.5",
|
|
71
|
+
"@eslint/js": "^10.0.1",
|
|
72
|
+
"esbuild": "^0.28.2",
|
|
73
|
+
"eslint": "^10.16.0",
|
|
74
|
+
"jsdom": "^31.0.1",
|
|
75
|
+
"playwright-core": "^1.65.2",
|
|
76
|
+
"prettier": "^3.10.2",
|
|
77
|
+
"publint": "^0.3.25",
|
|
78
|
+
"typescript": "^6.0.3",
|
|
79
|
+
"typescript-eslint": "^8.74.0"
|
|
80
|
+
}
|
|
8
81
|
}
|