Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made the DevTools to detect for mobile mode #46

Closed
wants to merge 14 commits into from
42 changes: 38 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,55 @@ MIT License
}));
};

// Function to check if the given device is a phone or ipad
function isPhone() {
return /android|ipad/i.test(navigator.userAgent);
}

// Check how long its takes for the given code to execute
// It return a value after execution
// If value is greater than 60 then dev tool is open
// If value is less than 60 then dev tool is not open
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds very fragile. Is this really reliable enough? The performance difference between devices can be huge. For example, maybe the new M1 Macbook's manage to do this in less than 60 even when DevTools is open.

// Since the time to execute cosole.log takes longer when dev tool is open
function checkPerformance() {
const start = performance.now();
for (let i = 0; i < 100; i++) {
console.log();
console.clear();
}

const end = performance.now();
return (end - start);
}

const main = ({emitEvents = true} = {}) => {
const widthThreshold = window.outerWidth - window.innerWidth > threshold;
const heightThreshold = window.outerHeight - window.innerHeight > threshold;
const orientation = widthThreshold ? 'vertical' : 'horizontal';

if (
!(heightThreshold && widthThreshold) &&
((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)
) {
if (!(heightThreshold && widthThreshold) && ((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do unrelated changes.

if ((!devtools.isOpen || devtools.orientation !== orientation) && emitEvents) {
emitEvent(true, orientation);
}

devtools.isOpen = true;
devtools.orientation = orientation;
} else if (isPhone()) {
if (parseInt(checkPerformance(), 0) > 60) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 => 10

if ((!devtools.isOpen || devtools.orientation !== orientation) && emitEvents) {
emitEvent(true, '');
}

devtools.isOpen = true;
devtools.orientation = '';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

} else {
if (devtools.isOpen && emitEvents) {
emitEvent(false, undefined);
}

devtools.isOpen = false;
devtools.orientation = undefined;
}
} else {
if (devtools.isOpen && emitEvents) {
emitEvent(false, undefined);
Expand Down