如何使用 js 檢測控制台被用戶打開了 All In One
js
solutions
- 監聽 F12 事件
- 監聽鍵盤快捷鍵組合
Ctrl + Shift + I
Option + Command + I
- Object.toString()
如果控制台輸出的是對象,則保留對象的引用,每次打開開發者工具的時候都會重新調用一下對象的 toString()方法將返回結果打印到控制台(console tab)上。
- 監聽 window 的縱橫比的變化
- debugger 斷點
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
// const log = console.log;
function FBIWarning() {
console.warn(`控制台被打開了`);
console.error(`控制台被打開了`);
}
// ??? !function
!function () {
const id = setInterval(() => {
const before = new Date().getTime();
debugger;
const after = new Date().getTime();
if (Math.abs(after - before) > 100) {
FBIWarning();
clearInterval(id)
}
}, 1000);
}();
- Proxy 攔截 log 執行
- 數據劫持
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-10-01
* @modified
*
* @description
* @difficulty Easy Medium Hard
* @complexity O(n)
* @augments
* @example
* @link
* @solutions
*
* @best_solutions
*
*/
// const log = console.log;
let times = 1;
var abc = document.createElement('div');
Object.defineProperty(abc, `id`, {
// 數據劫持
get: function () {
// console.log(`控制台被打開了`, x);
console.warn(`控制台被打開了`, times);
console.error(`控制台被打開了`, times);
times++;
},
// others
});
console.log(abc);
應用場景
禁用右鍵菜單
防止執行外包的 js, 關閉當前頁面
重定向
禁用復制
refs
https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
快捷鍵
https://www.cnblogs.com/xgqfrms/tag/快捷鍵/
let isCtrl = false;
document.addEventListener("keyup", () => {
//
});
document.addEventListener("keyup", function(e) {
let key = e.which || e.keyCode;
// console.log(`keyup & e =`, e);
// console.log(`e.keyCode =`, e.keyCode);
// console.log(`e.which =`, e.keyCode);
// console.log(`key =`, key);
if(e.which === 17) {
// init
isCtrl = false;
}
});
document.addEventListener("keydown", function(e) {
let key = e.which || e.keyCode;
console.log(`keydown & e =`, e);
// console.log(`e.keyCode =`, e.keyCode);
// console.log(`e.which =`, e.keyCode);
// console.log(`key =`, key);
if(e.which === 17) {
isCtrl = true;
}
if(e.which === 83 && isCtrl === true) {
console.log(`you pressed Ctrl + S`);
}
if(e.which === 68 && isCtrl === true) {
console.log(`you pressed Ctrl + D`);
}
if(e.which === 70 && isCtrl === true) {
console.log(`you pressed Ctrl + F`);
}
if(e.which === 88 && isCtrl === true) {
console.log(`you pressed Ctrl + X`);
}
});
// document.onkeyup = function(e) {
// if(e.which === 17) {
// isCtrl = false;
// }
// }
// document.onkeydown = function(e) {
// if(e.which === 17) {
// isCtrl = true;
// }
// if(e.which === 83 && isCtrl === true) {
// // alert('Keyboard shortcuts are cool!');
// return false;
// }
// }
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 發布文章使用:只允許注冊用戶才可以訪問!
原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!