Skip to content

Commit

Permalink
0.0.9 publish
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyun-J committed Apr 18, 2022
1 parent 885e159 commit 86edda3
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 32 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface _ConsoleDetectorSetting extends _DetectorSetting {
interface _Detector {
setting: _DetectorSetting;
readonly isDevtoolOpen: boolean;
readonly enable: boolean;
enable: boolean;
setEnable: (enable: boolean) => void;
}

Expand Down
2 changes: 1 addition & 1 deletion index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "devtool-detector",
"version": "0.0.8",
"version": "0.0.9",
"description": "Simple browser devtool detector",
"types": "index.d.ts",
"main": "index.js",
Expand Down
49 changes: 31 additions & 18 deletions src/detector.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ import DateDetector from './detectors/console-date'
import RegDetector from './detectors/console-reg'
import DebugDetector from './detectors/debug'

const userAgent = globalThis.navigator.userAgent
let _detector = null

const isChrome = typeof globalThis.window.chrome !== 'undefined' || /chrome/i.test(userAgent) || /CriOS/i.test(userAgent)
const isSafari = (typeof globalThis.window.safari !== 'undefined' && (globalThis.window.safari.pushNotification.toString() === '[object SafariRemoteNotification]')) || (/safari/i.test(userAgent) && !isChrome)
const isFirefox = 'InstallTrigger' in globalThis.window || /firefox/i.test(userAgent)
const userAgent = (() => {
if (globalThis.navigator) return globalThis.navigator.userAgent
else return ''
})()

const isChrome = typeof globalThis.chrome !== 'undefined' || /chrome/i.test(userAgent) || /CriOS/i.test(userAgent)
const isSafari = (/safari/i.test(userAgent) && !isChrome) || (typeof globalThis.safari !== 'undefined' && (globalThis.safari.pushNotification.toString() === '[object SafariRemoteNotification]'))
const isFirefox = 'InstallTrigger' in globalThis || /firefox/i.test(userAgent)
const isIE = /trident/i.test(userAgent) || /msie/i.test(userAgent)
const isEdgeLegacy = /edge/i.test(userAgent)
const isWebkit = /webkit/i.test(userAgent) && !isEdge

const getBrouserName = () => {
if (isIE) return 'ie'
if (isFirefox) return 'firefox'
if (isEdgeLegacy) return 'edge'
if (isSafari) return 'safari'
if (isChrome) return 'chrome'
if (isWebkit) return 'webkit'
return 'other'
}
const isWebkit = /webkit/i.test(userAgent) && !isEdgeLegacy

const BrowserDetectorConfig = {
chrome: 'debugger',
Expand All @@ -32,10 +27,19 @@ const BrowserDetectorConfig = {
default: 'debugger'
}

Object.freeze(BrowserDetectorConfig)
Object.seal(BrowserDetectorConfig)

export { BrowserDetectorConfig }
export const getDetector = () => {
const getBrouserName = () => {
if (isIE) return 'ie'
if (isFirefox) return 'firefox'
if (isEdgeLegacy) return 'edgeLegacy'
if (isSafari) return 'safari'
if (isChrome) return 'chrome'
if (isWebkit) return 'webkit'
return 'other'
}

const defineDetector = () => {
const detector = BrowserDetectorConfig[getBrouserName()]
switch (detector) {
case 'console-elem': return ElemDetector
Expand All @@ -44,4 +48,13 @@ export const getDetector = () => {
case 'debugger':
default: return DebugDetector
}
}

export { BrowserDetectorConfig }
export const getDetector = () => {
if (_detector) return _detector
else {
_detector = defineDetector()
return _detector
}
}
9 changes: 6 additions & 3 deletions src/detectors/console-date.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ const date = new Date()
date.toString = () => {
_isDetectOpen = true
return ''
};

}

const setting = {
waitConsole: 10,
Expand All @@ -25,7 +24,8 @@ const detectStart = () => nextScopeTimeout = setTimeout(detectEvent, setting.nex

const detectEvent = () => {
_isDetectOpen = false
console.log(date)
if (console.table) console.table({ date: date })
else console.log(date)
console.clear()
waitConsoleTimeout = setTimeout(() => {
if (_isDetectOpen !== _isDevtoolOpen) {
Expand All @@ -47,6 +47,9 @@ const Detector = {
get enable() {
return _isEnable
},
set enable(enable) {
_isEnable = enable
},
setEnable(enable) {
if (enable === _isEnable) return
if (enable) {
Expand Down
8 changes: 6 additions & 2 deletions src/detectors/console-elem.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Object.defineProperty(elem, 'id', {
get() {
_isDetectOpen = true
},
});
})

const setting = {
waitConsole: 10,
Expand All @@ -25,7 +25,8 @@ const detectStart = () => nextScopeTimeout = setTimeout(detectEvent, setting.nex

const detectEvent = () => {
_isDetectOpen = false
console.log(elem)
if (console.table) console.table({ elem: elem })
else console.log(elem)
console.clear()
waitConsoleTimeout = setTimeout(() => {
if (_isDetectOpen !== _isDevtoolOpen) {
Expand All @@ -47,6 +48,9 @@ const Detector = {
get enable() {
return _isEnable
},
set enable(enable) {
_isEnable = enable
},
setEnable(enable) {
if (enable === _isEnable) return
if (enable) {
Expand Down
6 changes: 5 additions & 1 deletion src/detectors/console-reg.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const detectStart = () => nextScopeTimeout = setTimeout(detectEvent, setting.nex

const detectEvent = () => {
_isDetectOpen = false
console.table({ reg: reg })
if (console.table) console.table({ reg: reg })
else console.log(reg)
console.clear()
waitConsoleTimeout = setTimeout(() => {
if (_isDetectOpen !== _isDevtoolOpen) {
Expand All @@ -45,6 +46,9 @@ const Detector = {
get enable() {
return _isEnable
},
set enable(enable) {
_isEnable = enable
},
setEnable(enable) {
if (enable === _isEnable) return
if (enable) {
Expand Down
3 changes: 3 additions & 0 deletions src/detectors/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ const Detector = {
get enable() {
return _isEnable
},
set enable(enable) {
_isEnable = enable
},
setEnable(enable) {
if (enable === _isEnable) return
if (enable) {
Expand Down
9 changes: 6 additions & 3 deletions src/listeners.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
let listeners = []
const listeners = []
export default listeners
export const addDetectListener = (listener) => { listeners.push(listener) }
export const removeDetectListener = (listener) => { listeners = this.listeners.filter(l => l !== listener) }
export const removeAllDetectListener = () => { listeners = [] }
export const removeDetectListener = (listener) => {
const index = listeners.indexOf(listener)
if (index !== -1) listeners.splice(index, 1)
}
export const removeAllDetectListener = () => { array.splice(0, listeners.length) }
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
requirejs(['../index.js'], ({ getDetector, addDetectListener }) => {
requirejs(['../index.js'], ({ getDetector, addDetectListener, BrowserDetectorConfig }) => {
// BrowserDetectorConfig.chrome = 'console-date'
const Detector = getDetector()
console.log(Detector)
Detector.setEnable(true)

addDetectListener((isDevtoolOpen) => {
Expand Down

0 comments on commit 86edda3

Please sign in to comment.