<commit_before>(function() {
    var config = phast.config['script-proxy-service'];
    var urlPattern = /^(https?:)?\/\//;
    var cacheMarker = Math.floor((new Date).getTime() / 1000 / config.urlRefreshTime);
    var id = 0;
    var whitelist = compileWhitelistPatterns(config.whitelist);

    overrideDOMMethod('appendChild');
    overrideDOMMethod('insertBefore');

    function compileWhitelistPatterns(patterns) {
        var re = /^(.)(.*)\1([a-z]*)$/i;
        var compiled = [];
        patterns.forEach(function (pattern) {
            var match = re.exec(pattern);
            if (!match) {
                window.console && window.console.log("Phast: Not a pattern:", pattern);
                return;
            }
            try {
                compiled.push(new RegExp(match[2], match[3]));
            } catch (e) {
                window.console && window.console.log("Phast: Failed to compile pattern:", pattern);
            }
        });
        return compiled;
    }

    function checkWhitelist(value) {
        for (var i = 0; i < whitelist.length; i++) {
            if (whitelist[i].exec(value)) {
                return true;
            }
        }
        return false;
    }

    function overrideDOMMethod(name) {
        var original = Element.prototype[name];
        Element.prototype[name] = function () {
            processNode(arguments[0]);
            return original.apply(this, arguments);
        };
    }

    function processNode(el) {
        if (!el) {
            return;
        }
        if (el.nodeType !== Node.ELEMENT_NODE) {
            return;
        }
        if (el.tagName !== 'SCRIPT') {
            return;
        }
        if (!urlPattern.test(el.src)) {
            return;
        }
        if (el.src.substr(0, config.serviceUrl.length) == config.serviceUrl) {
            return;
        }
        if (!checkWhitelist(el.src)) {
            return;
        }
        id++;
        el.setAttribute('data-phast-proxied-script', id);
        el.src = config.serviceUrl + '&src=' + escape(el.src) +
            '&cacheMarker=' + escape(cacheMarker) +
            '&id=d' + id;
    }
});
<commit_msg>ScriptsProxyService: Fix JS, use encodeURIComponent<commit_after>var config = phast.config['script-proxy-service'];
var urlPattern = /^(https?:)?\/\//;
var cacheMarker = Math.floor((new Date).getTime() / 1000 / config.urlRefreshTime);
var id = 0;
var whitelist = compileWhitelistPatterns(config.whitelist);

overrideDOMMethod('appendChild');
overrideDOMMethod('insertBefore');

function compileWhitelistPatterns(patterns) {
    var re = /^(.)(.*)\1([a-z]*)$/i;
    var compiled = [];
    patterns.forEach(function (pattern) {
        var match = re.exec(pattern);
        if (!match) {
            window.console && window.console.log("Phast: Not a pattern:", pattern);
            return;
        }
        try {
            compiled.push(new RegExp(match[2], match[3]));
        } catch (e) {
            window.console && window.console.log("Phast: Failed to compile pattern:", pattern);
        }
    });
    return compiled;
}

function checkWhitelist(value) {
    for (var i = 0; i < whitelist.length; i++) {
        if (whitelist[i].exec(value)) {
            return true;
        }
    }
    return false;
}

function overrideDOMMethod(name) {
    var original = Element.prototype[name];
    Element.prototype[name] = function () {
        processNode(arguments[0]);
        return original.apply(this, arguments);
    };
}

function processNode(el) {
    if (!el) {
        return;
    }
    if (el.nodeType !== Node.ELEMENT_NODE) {
        return;
    }
    if (el.tagName !== 'SCRIPT') {
        return;
    }
    if (!urlPattern.test(el.src)) {
        return;
    }
    if (el.src.substr(0, config.serviceUrl.length) == config.serviceUrl) {
        return;
    }
    if (!checkWhitelist(el.src)) {
        return;
    }
    id++;
    el.setAttribute('data-phast-proxied-script', 'd' + id);
    el.src = config.serviceUrl +
        '&src=' + encodeURIComponent(el.src) +
        '&cacheMarker=' + encodeURIComponent(cacheMarker) +
        '&id=d' + id;
}

{'fraction_non_alphanumeric': 0.1510164569215876, 'fraction_numerical': 0.005324298160696999, 'mean_word_length': 2.030058651026393, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7147355', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1241, 'n_words': 240}