jeudi 24 novembre 2016

Modular Pattern JavaScript

Attached code can catch situation when new images are created and it is checking that image request is related to particular domain. I want to write a similar JavaScript function which can catch Google's DFA(anything similar) would also do.Please guys help ASAP.Check the document.create function something similar approach to catch google's DFA or anything else will do.Please suggest

var test = (function(pixelHost) {
var _pixelHost = pixelHost || "google-analytics.com";
var lastClickedElement = null;
    (function(proxied) {
        document.createElement = function() {
            var x = proxied.apply(this, arguments);
            if ( typeof arguments !== "undefined" && typeof arguments[0] === "string" && arguments[0].toLowerCase() === "img") {
                onload = function() {
                    if (url_domain(x.src).indexOf(_pixelHost) >-1) {
                    console.log('image src:', x.src);
                    logItem(x.src);
                    }
                };
                // hell is freezing:
                setTimeout(onload, 10);
            }
            return x;
        };
    })(document.createElement);

    function url_domain(data) {
    if (typeof data ==="undefined" || data ==="" || data === null) {
        return null;
    }
    var a = document.createElement('a');
         a.href = data;
    return a.hostname;
}

    function getLogObject() {
        var logObject = sessionStorage.getItem('superTLogItems');
        if (logObject === null || logObject ==="null" ) {
            logObject = [];
        } else {
            try {
                logObject = JSON.parse(logObject);
            } catch (e) {
                console.log(e);
                logObject = [];
            }
        }
        return logObject;
    }

    function logItem(src) {
        var logItem = createLogItem(src);
        var logObject = getLogObject();
        logObject.push(logItem);
        var logString = JSON.stringify(logObject);
        sessionStorage.setItem('superTLogItems', logString);
    }

    function clearStorage() {
        sessionStorage.setItem('superTLogItems', null);
    }

    function createLogItem(src) {
        var delimeter = ";";
        var dlh = document.location.href;
        var dlc = document.cookie;
        return dlh + delimeter + dlc + delimeter + lastClickedElement + delimeter + src;
    }

    function exportToCsv() {
            var logObject = getLogObject();
            var myCSV = "";
            logObject.forEach(function (k,i) {
               myCSV += k + "\r\n";
            });
            window.open('data:text/csv;charset=utf-8,' + escape(myCSV));
        }

    function init() {
        function lastClickObject(event) {
            var obj = event.target.tagName + ":#" + event.target.getAttribute("id") + ":" + event.target.getAttribute("class");
            console.log('u clicked:', obj);
            lastClickedElement = obj;
        }


        document.addEventListener('click', lastClickObject);


    }

    init();

    return { clearStorage: clearStorage, exportToCsv:exportToCsv};

})();

Aucun commentaire:

Enregistrer un commentaire