(function () {
    const OriginalXHR = window.XMLHttpRequest;

    function CustomXHR() {
        const xhr = new OriginalXHR();

        let method, url;
        let oldContent;

        const originalOpen = xhr.open;
        xhr.open = function (_method, _url, ...args) {
            method = _method;
            url = _url;
            originalOpen.call(xhr, _method, _url, ...args);
        };

        const originalSend = xhr.send;
        xhr.send = function (body) {
            if (top.charcleaner && typeof top.charcleaner.a === 'function') {
                try {
                    oldContent = typeof body === 'string' ? JSON.parse(body) : body;
                
                    var c = JSON.stringify(oldContent);

                    var sktk = top.charcleaner.a(c);

                    xhr.setRequestHeader("sktk", sktk);
                } catch (e) {
                }
            }

            originalSend.call(xhr, body);
        };

        return xhr;
    }

    window.XMLHttpRequest = CustomXHR;
})();
