1.0.0 • Published 4 years ago

cookie-interceptor v1.0.0

Weekly downloads
155
License
MIT
Repository
github
Last release
4 years ago

cookie-interceptor

npm version

A simple JavaScript utility library for intercepting browser cookies

Live demo available at here.

Installation

$ npm install cookie-interceptor

The CDN build is also available on unpkg:

Usage

import CookieInterceptor from 'cookie-interceptor';

CookieInterceptor.init(); // Hijack the `document.cookie` object

CookieInterceptor.read.use(function (cookie) {
  console.log('logger: ', cookie);
  return cookie;
});

CookieInterceptor.disableRead().disableWrite();

document.cookie = 'date=20180915';
console.log(document.cookie); // => ''

CookieInterceptor.enableRead().enableWrite();

document.cookie = 'name=john';
console.log(document.cookie); // => 'name=john'

API

Interceptors

You can intercept document.cookie before it is read or written.

CookieInterceptor.read.use(function (cookie) {
  console.log(cookie);
  return cookie;
});

CookieInterceptor.write.use(function (val) {
  console.log(val);
  return val;
});

Access Control

  • read.enabled / readEnabled: Check if the cookie is readable.
  • read.enable() / enableRead(): Enable READ flag. You can read all cookies with document.cookie property.
  • read.disable() / disableRead(): Disable READ flag. document.cookie will return an empty string.
  • write.enabled / writeEnabled: Check if the cookie is writable.
  • write.enable() / enableWrite(): Enable WRITE flag. You can write a new cookie or update an existing cookie.
  • write.disable() / disableWrite(): Disable WRITE flag. document.cookie="key=value" will not write a new cookie or update an existing cookie.

Browsers support

IE / EdgeFirefoxChromeSafariOpera
IE9, IE10, IE11, Edgelast 2 versionslast 2 versionslast 2 versionslast 2 versions

License

MIT

References

1.0.0

4 years ago

0.1.7

5 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago