-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 添加阻止
键盘F12
、浏览器默认右键菜单
、页面元素选中
、图片默认可拖动
方法
- Loading branch information
1 parent
616703f
commit 620968d
Showing
1 changed file
with
28 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useEventListener } from "@vueuse/core"; | ||
|
||
/** 是否为`img`标签 */ | ||
function isImgElement(element) { | ||
return typeof HTMLImageElement !== "undefined" | ||
? element instanceof HTMLImageElement | ||
: element.tagName.toLowerCase() === "img"; | ||
} | ||
|
||
// 在 src/main.ts 引入并调用即可 import { addPreventDefault } from "@/utils/preventDefault"; addPreventDefault(); | ||
export const addPreventDefault = () => { | ||
// 阻止通过键盘F12快捷键打开浏览器开发者工具面板 | ||
useEventListener( | ||
window.document, | ||
"keydown", | ||
ev => ev.key === "F12" && ev.preventDefault() | ||
); | ||
// 阻止浏览器默认的右键菜单弹出(不会影响自定义右键事件) | ||
useEventListener(window.document, "contextmenu", ev => ev.preventDefault()); | ||
// 阻止页面元素选中 | ||
useEventListener(window.document, "selectstart", ev => ev.preventDefault()); | ||
// 浏览器中图片通常默认是可拖动的,并且可以在新标签页或窗口中打开,或者将其拖动到其他应用程序中,此处将其禁用,使其默认不可拖动 | ||
useEventListener( | ||
window.document, | ||
"dragstart", | ||
ev => isImgElement(ev?.target) && ev.preventDefault() | ||
); | ||
}; |