cookie-controller
v1.1.1
Published
A simple JavaScript utility library for intercepting and controlling browser cookies operations
Downloads
30
Maintainers
Readme
cookie-interceptor
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:
- unpkg.com/cookie-interceptor/dist/cookie-interceptor.umd.js
- unpkg.com/cookie-interceptor/dist/cookie-interceptor.umd.min.js
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()
: EnableREAD
flag. You can read all cookies withdocument.cookie
property.read.disable()
/disableRead()
: DisableREAD
flag.document.cookie
will return an empty string.write.enabled
/writeEnabled
: Check if the cookie is writable.write.enable()
/enableWrite()
: EnableWRITE
flag. You can write a new cookie or update an existing cookie.write.disable()
/disableWrite()
: DisableWRITE
flag.document.cookie="key=value"
will not write a new cookie or update an existing cookie.
Browsers support
| IE / Edge | Firefox | Chrome | Safari | Opera | | --------- | --------- | --------- | --------- | --------- | | IE9, IE10, IE11, Edge| last 2 versions| last 2 versions| last 2 versions| last 2 versions
License
MIT