react-cookie-kit
Version:
GDPR / CCPA Easy Cookie, Script, Do-Not-Sell, and Fingerprint Consent Management for Websites.
163 lines (127 loc) • 7.25 kB
JavaScript
function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Date.prototype.toString.call(Reflect.construct(Date, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from "react";
import PropTypes from "prop-types";
import { xbOrigin } from "./configs";
var BridgeCommunicator = /*#__PURE__*/function (_React$PureComponent) {
_inherits(BridgeCommunicator, _React$PureComponent);
var _super = _createSuper(BridgeCommunicator);
function BridgeCommunicator(props) {
var _this;
_classCallCheck(this, BridgeCommunicator);
_this = _super.call(this, props); // Register with XcooBee domain in iframe
_defineProperty(_assertThisInitialized(_this), "XcooBeeHandleResponse", function (event) {
var _this$props = _this.props,
onLoginStatusChange = _this$props.onLoginStatusChange,
onCookieOptionsLoad = _this$props.onCookieOptionsLoad,
handleBridgeError = _this$props.handleBridgeError;
if (event.origin === xbOrigin) {
if (!event.data) {
return;
}
var data = JSON.parse(event.data);
var action = Object.keys(data)[0];
if (action === "loginstatus") {
onLoginStatusChange(data[action]);
if (data[action]) {
_this.fetchCookieOptions();
}
} else if (action === "cookieoptions") {
onCookieOptionsLoad(data[action]);
} else if (action === "savecookieconsent") {// console.log("Cookie consents have been successfully saved.");
} else {
handleBridgeError(data[action]);
}
}
});
_defineProperty(_assertThisInitialized(_this), "checkLoginStatus", function () {
var _this$props2 = _this.props,
campaignReference = _this$props2.campaignReference,
onLoginStatusChange = _this$props2.onLoginStatusChange;
var myMsgObj = {
action: "loginstatus",
campaign: campaignReference,
domain: window.location.origin
};
if (campaignReference) {
_this.frameRef.contentWindow.postMessage(JSON.stringify(myMsgObj), xbOrigin);
} else {
onLoginStatusChange(false);
}
});
window.addEventListener("message", _this.XcooBeeHandleResponse, false);
return _this;
} // This function is called when we have a response from XcooBee Domain
_createClass(BridgeCommunicator, [{
key: "fetchCookieOptions",
value: function fetchCookieOptions() {
var campaignReference = this.props.campaignReference;
var myMsgObj = {
action: "cookieoptions",
action_params: {
type: "all"
},
campaign: campaignReference,
domain: window.location.origin
};
if (!campaignReference) {
return;
}
this.frameRef.contentWindow.postMessage(JSON.stringify(myMsgObj), xbOrigin);
}
}, {
key: "saveCookieConsents",
value: function saveCookieConsents(cookieConsents) {
var campaignReference = this.props.campaignReference;
var myMsgObj = {
action: "savecookieconsent",
action_params: {
categories: cookieConsents
},
campaign: campaignReference,
domain: window.location.origin
};
if (!campaignReference) {
return;
}
this.frameRef.contentWindow.postMessage(JSON.stringify(myMsgObj), xbOrigin);
}
}, {
key: "render",
value: function render() {
var _this2 = this;
return /*#__PURE__*/React.createElement("iframe", {
title: "cookieBridge",
ref: function ref(frameRef) {
_this2.frameRef = frameRef;
},
src: "".concat(xbOrigin, "/scripts/cookie-bridge/index.html"),
onLoad: this.checkLoginStatus,
height: "0",
width: "0",
frameBorder: "0"
});
}
}]);
return BridgeCommunicator;
}(React.PureComponent);
_defineProperty(BridgeCommunicator, "propTypes", {
campaignReference: PropTypes.string,
onLoginStatusChange: PropTypes.func.isRequired,
onCookieOptionsLoad: PropTypes.func.isRequired,
handleBridgeError: PropTypes.func.isRequired
});
_defineProperty(BridgeCommunicator, "defaultProps", {
campaignReference: null
});
export { BridgeCommunicator as default };