0

I have a code to avoid users opening devtools menu, the code works perfectly for me! But sometimes, when you watch a simple video at my website and put it on fullscreen, the script below returns false positive.

// chrome
var element = new Image;
var devtoolsOpen = false;
element.__defineGetter__("id", function() {
  devtoolsOpen = true; // This only executes when devtools is open.
});
setInterval(function() {
  devtoolsOpen = false;
  //console.log(element);
  if (devtoolsOpen && devtoolsOpen == true) {
    window.location = "https://mywebsite.com/denied/";
  }
}, 1000);

// all
var _interval = 200;
(function() {
  'use strict';
  var devtools = {
    open: false,
    orientation: null
  };
  var threshold = 160;
  var emitEvent = function(state, orientation) {
    window.dispatchEvent(new CustomEvent('devtoolschange', {
      detail: {
        open: state,
        orientation: orientation
      }
    }));
  };
  setInterval(function() {
    var widthThreshold = window.outerWidth - window.innerWidth > threshold;
    var heightThreshold = window.outerHeight - window.innerHeight > threshold;
    var orientation = widthThreshold ? 'vertical' : 'horizontal';
    if (!(heightThreshold && widthThreshold) &&
      ((window.Firebug && window.Firebug.chrome && window.Firebug.chrome.isInitialized) || widthThreshold || heightThreshold)) {
      if (!devtools.open || devtools.orientation !== orientation) {
        emitEvent(true, orientation);
      }
      devtools.open = true;
      devtools.orientation = orientation;
    } else {
      if (devtools.open) {
        emitEvent(false, null);
      }
      devtools.open = false;
      devtools.orientation = null;
    }
  }, _interval);
  if (typeof module !== 'undefined' && module.exports) {
    module.exports = devtools;
  } else {
    window.devtools = devtools;
  }
  setTimeout(function() {
    _interval = 500;
  }, 1000);
})();
// check if it's open
//console.log('is DevTools open?', window.devtools.open);
if (window.devtools.open && window.devtools.open == true) {
  window.location = "https://mywebsite.com/denied/";
}

// get notified when it's opened/closed or orientation changes
window.addEventListener('devtoolschange', function(e) {
  //console.log('is DevTools open?', e.detail.open);
  if (e.detail.open && e.detail.open == true) {
    window.location = "https://mywebsite.com/denied/";
  }
});
// clear
console.API;
if (typeof console._commandLineAPI !== 'undefined') {
  console.API = console._commandLineAPI; //chrome
} else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
  console.API = console._inspectorCommandLineAPI; //Safari
} else if (typeof console.clear !== 'undefined') {
  console.API = console;
}
console.API.clear();

I reviewed the code many times and I wasn't able to find why this is happening. Do you have any idea about how can I solve it? Thank you.

Barmar
  • 741,623
  • 53
  • 500
  • 612
mitrik
  • 21
  • 6

0 Answers0