Giter Site home page Giter Site logo

maykonn / js-detect-incognito-private-browsing-paywall Goto Github PK

View Code? Open in Web Editor NEW
43.0 11.0 14.0 1.43 MB

Javascript Code to Detect Private or Incognito Browsing (with Bot detector) and Paywall

Home Page: https://www.npmjs.com/package/js-detect-incognito-private-browsing

JavaScript 85.14% HTML 14.86%
incognito private browsing incognito-mode bot-detector javascript js private-browsing incognito-browsing bot

js-detect-incognito-private-browsing-paywall's Introduction

Private or Incognito Browsing Detector / Paywall

Javascript code to Detect Private and Incognito Browsing Mode. You can implement a Paywall System by detecting private or incognito browsing.

Implementing a Paywall System for Private or Incognito Browsing Mode

You can implement a Paywall System with Javascript blocking the Private or Incognito browsing in your Website using this code.

This detector can identify Bots Browsing your application and you can turn on/turn off this detection.
With Bot detector on, when Googlebot visit your page you don't block it!

How to use

Installation

You can include the /dist/BrowsingModeDetector.js or /dist/BrowsingModeDetector.gz via <script> tag direct on your application or via npm:

npm i js-detect-incognito-private-browsing -S

Implementing behaviour based on Browsing Mode:

Passing a callback to BrowsingModeDetector.do() method you can implement the needed behaviour testing the returned value:

var myCallback = function (browsingInIncognitoMode, BrowsingModeDetectorInstance) {
    console.log('Is private?', browsingInIncognitoMode);
    console.log('Browsing Mode:', BrowsingModeDetectorInstance.getBrowsingMode());
  
    if (browsingInIncognitoMode) {
        // Incognito, Private mode detected
        return;
    }
  
    // Normal mode detected
};
  
var BrowsingModeDetector = new BrowsingModeDetector();
BrowsingModeDetector.do(myCallback);

Advanced use of callbacks:

You can use a default callback combined with specific callbacks for each browsing method:

var callbackWhenNormalMode = function () {
    console.log('callbackWhenNormalMode called');
};
  
var callbackWhenIncognitoOrPrivateMode = function () {
    console.log('callbackWhenIncognitoOrPrivateMode');
};
  
var defaultCallback = function (browsingInIncognitoMode) {
    console.log('This callback will be called either private or normal mode detected, optional though. Is private or incognito?', browsingInIncognitoMode);
};
  
var BrowsingModeDetector = new BrowsingModeDetector();
BrowsingModeDetector
    .setCallbackForNormalMode(callbackWhenNormalMode)
    .setCallbackForIncognitoOrPrivateMode(callbackWhenIncognitoOrPrivateMode)
    .do(defaultCallback); // optional if callbacks are given for normal and private modes

Possible callbacks arrangements:

  • Only Default Callback using .do(callback) method
  • Callback for Normal Mode .setCallbackForNormalMode(callback) combined with Private Mode .setCallbackForIncognitoOrPrivateMode(callback)
  • Default Callback combined with Normal Mode and Private/Incognito Mode Callbacks

On callbacks you can use the .getBrowsingMode() if necessary, because a instance of BrowsingModeDetector is passed for all callback functions:

var callbackWhenNormalMode = function (BrowsingModeDetectorInstance) {
    console.log('callbackWhenNormalMode called when', BrowsingModeDetectorInstance.getBrowsingMode());
};
  
var callbackWhenIncognitoOrPrivateMode = function (BrowsingModeDetectorInstance) {
    console.log('callbackWhenIncognitoOrPrivateMode called when', BrowsingModeDetectorInstance.getBrowsingMode());
};
  
var defaultCallback = function (browsingInIncognitoMode, BrowsingModeDetectorInstance) {
    console.log('Is private or incognito?', browsingInIncognitoMode);
  
    if (BrowsingModeDetectorInstance.getBrowsingMode() === BrowsingModeDetectorInstance.BROWSING_NORMAL_MODE) {
        console.log('Do something if is NORMAL_MODE!');
    } else {
        console.log('Do something if is INCOGNITO_PRIVATE_MODE!');
    }
};

Note that will be passed a BrowsingModeDetector instance for each user defined callback and for default callback to .do(callback) method will be passed as first param of callback a boolean value meaning browsing in incognito mode when true and a BrowsingModeDetector instance as second param of callback.

Ignoring (or not) browsing mode for Bots:

If you have a Website, probably you want to ignore if Bots are browsing:

var BrowsingModeDetector = new BrowsingModeDetector();
BrowsingModeDetector.do(myCallback); 
// or BrowsingModeDetector.ignoringBots().do(myCallback);

Or testing for Bots too(e.g: if Googlebot browse your site, the detector will return that browsing mode is private):

var BrowsingModeDetector = new BrowsingModeDetector();
BrowsingModeDetector.notIgnoringBots().do(myCallback);

Bot Browsing Detection:

You can test if is a Bot browsing in two ways:

var isBot = BrowsingModeDetector.isBotBrowsing();
console.log('Is Bot:', isBot);

Or passing a callback to isBotBrowsing method:

var isBot = BrowsingModeDetector.isBotBrowsing(function(isBot) {
    console.log('Is Bot:', isBot);
});

Examples:

For a complete example with JS and HTML see the /example/index.html file.

Compiling the code:

Clone this repo and you can use npm and webpack to compile the code. Install the requirements:

git clone https://github.com/Maykonn/js-detect-incognito-private-browsing-paywall
npm install

In your dev environment you can run (will compile the code and open the example app at localhost:8080/):

npm run start

Build the code to production at /dist directory (minify, uglify, remove comments, logs, etc):

npm run build

The npm run build command will generate two file at /dist directory, BrowsingModeDetector.gz and BrowsingModeDetector.js.

Contributing:

You can contribute cloning this repository from github clicking here. So, you just need to create a new branch using a name related to the new functionality which you'll create.
And after finish your code, you just need to create a pull request which will be revised, merged to master(if the code doesn't break the lib) branch and published a new release of this library.

js-detect-incognito-private-browsing-paywall's People

Contributors

feelinc avatar maykonn avatar shoestar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-detect-incognito-private-browsing-paywall's Issues

Issue with webpack

After installing Browsing Mode Detector library as Dev Dependency I'm getting this error on the JS Console:

Uncaught TypeError: js_detect_incognito_private_browsing__WEBPACK_IMPORTED_MODULE_2___default.a is not a constructor

What I did was just, install the library using npm:

npm i -D js-detect-incognito-private-browsing

Then, import it as module in my index.js file:

import BrowsingModeDetector from 'js-detect-incognito-private-browsing'

What am I missing? Am I importing wrong the library?

Help to implement latest Safari & iOS detector

Hi,

Tested in Safari 13 and iOS 13.2

function tryFillLocalStorage (chunkSize) {
  var size = chunkSize / 4; // chunk will be repetition of 4B
  var content = (new Array(size+1)).join('aใ‚');
  var blob = {
    size:     chunkSize,
    payload:  content
  };
  // now is the generated time
  blob.lastModifiedDate = new Date();
  // Name it random so key won't conflict
  blob.name = (~~(Math.random()*100000)+100000)+'.txt';

  try {
    localStorage.setItem(blob.name, JSON.stringify(blob));
  } catch(e) {
    try {
      localStorage.removeItem(blob.name);
    } catch(e) {}

    return false;
  }

  try {
    localStorage.removeItem(blob.name);
  } catch(e) {}

  return true;
}

// normal mode having max 5 MB in localStorage space,
// yes NORMAL MODE having max 5 MB
var LOCAL_STORAGE_DATA_SIZE = 1024 * 1024 * 5; // 5MB

if (tryFillLocalStorage(LOCAL_STORAGE_DATA_SIZE)) {
  // IN PRIVATE MODE
}

Please if you can help implement and optimize that, because I'm currently using a whole different code structure, which support strict mode code.

Also please check this line of code, because you have a typo there.

Thanks.

Store Browsing Mode in a variable

Hi there. I'm developing a library for handle cookies and comply with the European Laws. So, I'm using this the detect incognito library for detecting when a user is navigating on incognito mode. So, after this introduction, I'd like to know if it is possible to store the browsing mode in a variable outside the callback function.
My code looks like:

var browsingModeCallback = function (browsingInIncognitoMode) {
            if (browsingInIncognitoMode && 'true' != Cookies.get( 'ez_accepted' ) ) {
                // Incognito, Private mode detected
                $( '.ez-waiting-img' ).hide();
                $( '.ez-waiting-message' ).hide();
                $( '.ez-waiting-optout' ).show();
                disableCookies();
                return;
            }
        };
        var BrowsingModeDetectorInstance = new BrowsingModeDetector();
        BrowsingModeDetectorInstance.do(browsingModeCallback);

But I need to know the value of browsingInIncognitoMode outside that scope. It is possible? if so, how can I do it?

Thanks.

False positive on Mozila Firefox 65.0.2

False positive private browsing on Mozila Firefox 65.0.2.
If I test the library on a localhost it works as it should but if I test it on a hosted app it always returns true (private browsing) even if the browser is in normal mode.

Issue on Firefox

I implemented this yesterday and it was working fine in Firefox. Cue an update overnight and now it doesn't appear to be. Version of Firefox I'm using is 63.0. Pls advise.

Does it still work with Chrome?

Does this work in Chrome 78? I just tried it for the first time and while it detects a private session in Firefox, it does not in Chrome.

I also saw a post saying the following,

When asking Google about these two new detection methods, Google has told BleepingComputer that they stand by their statement that they will "work to remedy any other current or future means of Incognito Mode detection."

By the way, it would be convenient if this could be imported like other modules. I'm not sure what the window.BrowsingModeDetector syntax does, though if I change it to export function BrowsingModeDetector()... it appears to import normally, and this is how I tested it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.