<commit_before>﻿//
// Copyright (C) Microsoft. All rights reserved.
//

/// <reference path="Interfaces.d.ts"/>
module F12.Proxy {
    "use strict";

    declare var host: any; //todo: create some interface for host
    declare var request: any; //todo: create some interface for request

    declare var browser: IBrowser;

    class BrowserHandler {
        externalObj: any;
        constructor() {
            this.externalObj = external; //todo: Make a better interface for external
            this.externalObj.addEventListener("message", (e: any) => this.messageHandler(e));
        }

        private PostResponse(id: number, value: any) {
            // Send the response back over the websocket
            var response: IWebKitResponse = Common.CreateResponse(id, value);
            this.externalObj.sendMessage("postMessage", JSON.stringify(response));
        }

        private PostNotification(method: any, params: any) {
            var notification = {
                method: method,
                params: params
            }

        this.externalObj.sendMessage("postMessage", JSON.stringify(notification));
        }

        private ProcessRuntime(method: any, request: any) {
            var processedResult;

            switch (method) {
                case "evaluate":
                case "callFunctionOn":
                    var resultFromEval = undefined;
                    var wasThrown = false;

                    if (method === "evaluate" && request.params.expression) {
                        try {
                            var escapedInput = JSON.stringify(request.params.expression).slice(1, -1);
                            resultFromEval = browser.executeScript(escapedInput);
                        } catch (e) {
                            resultFromEval = e;
                            wasThrown = true;
                        }

                    } else if (method === "callFunctionOn" && request.params.functionDeclaration) {
                        var args = [];
                        if (request.params.arguments) {
                            args.push(" ");
                            for (var i = 0; i < request.params.arguments.length; i++) {
                                var arg = request.params.arguments[i].value;
                                args.push(JSON.stringify(arg));
                            }
                        }

                        try {
                            var command = request.params.functionDeclaration + ".call(window" + args.join(",") + ")";
                            var escapedInput = JSON.stringify(command).slice(1, -1);
                            resultFromEval = browser.executeScript(escapedInput);
                        } catch (e) {
                            resultFromEval = e;
                            wasThrown = true;
                        }
                    }

                    var id = null;
                    var description = (resultFromEval ? resultFromEval.toString() : "");
                    var value = resultFromEval;

                    if (resultFromEval && typeof resultFromEval === "object") {
                        id = "1";
                        description = "Object";
                        value = undefined;
                    }

                    var resultDesc = {
                        objectId: id,
                        type: "" + typeof value,
                        value: value,
                        description: description
                    };

                    processedResult = {
                        result: {
                            wasThrown: wasThrown,
                            result: resultDesc
                        }
                    };

                    break;

                default:
                    processedResult = {};
                    break;
            }

            this.PostResponse(request.id, processedResult);
        }

        private ProcessPage(method: any, request: any) {
            var processedResult;

            switch (method) {
                case "navigate":
                    if (request.params.url) {
                        //browser.document.parentWindow.alert(browser.document.location.href + " -> " + request.params.url);
                        browser.executeScript("window.location.href = '" + request.params.url + "'");

                        processedResult = {
                            result: {
                                frameId: 5000.1
                            }
                        };
                    }
                    break;

                default:
                    processedResult = {};
                    break;
            }<commit_msg>adding some type information to Browser<commit_after>﻿//
// Copyright (C) Microsoft. All rights reserved.
//

/// <reference path="Interfaces.d.ts"/>
module F12.Proxy {
    "use strict";

    declare var host: any; //todo: create some interface for host
    declare var request: any; //todo: create some interface for request

    declare var browser: IBrowser;

    class BrowserHandler {
        windowExternal: any; //todo: Make an appropriate TS interface for external
        constructor() {
            this.windowExternal = (<any>external); 
            this.windowExternal.addEventListener("message", (e: any) => this.messageHandler(e));
        }

        private PostResponse(id: number, value: IWebKitResult) {
            // Send the response back over the websocket
            var response: IWebKitResponse = Common.CreateResponse(id, value);
            this.windowExternal.sendMessage("postMessage", JSON.stringify(response));
        }

        private PostNotification(method: string, params: any) {
            var notification = {
                method: method,
                params: params
            }

            this.windowExternal.sendMessage("postMessage", JSON.stringify(notification)); //todo: should this be postMessage?
        }

        private ProcessRuntime(method: string, request: IWebKitRequest) {
            var processedResult;

            switch (method) {
                case "evaluate":
                case "callFunctionOn":
                    var resultFromEval = undefined;
                    var wasThrown = false;

                    if (method === "evaluate" && request.params.expression) {
                        try {
                            var escapedInput = JSON.stringify(request.params.expression).slice(1, -1);
                            resultFromEval = browser.executeScript(escapedInput);
                        } catch (e) {
                            resultFromEval = e;
                            wasThrown = true;
                        }

                    } else if (method === "callFunctionOn" && request.params.functionDeclaration) {
                        var args = [];
                        if (request.params.arguments) {
                            args.push(" ");
                            for (var i = 0; i < request.params.arguments.length; i++) {
                                var arg = request.params.arguments[i].value;
                                args.push(JSON.stringify(arg));
                            }
                        }

                        try {
                            var command = request.params.functionDeclaration + ".call(window" + args.join(",") + ")";
                            var escapedInput = JSON.stringify(command).slice(1, -1);
                            resultFromEval = browser.executeScript(escapedInput);
                        } catch (e) {
                            resultFromEval = e;
                            wasThrown = true;
                        }
                    }

                    var id = null;
                    var description = (resultFromEval ? resultFromEval.toString() : "");
                    var value = resultFromEval;

                    if (resultFromEval && typeof resultFromEval === "object") {
                        id = "1";
                        description = "Object";
                        value = undefined;
                    }

                    var resultDesc = {
                        objectId: id,
                        type: "" + typeof value,
                        value: value,
                        description: description
                    };

                    processedResult = {
                        result: {
                            wasThrown: wasThrown,
                            result: resultDesc
                        }
                    };

                    break;

                default:
                    processedResult = {};
                    break;
            }

            this.PostResponse(request.id, processedResult);
        }

        private ProcessPage(method: string, request: IWebKitRequest) {
            var processedResult;

            switch (method) {
                case "navigate":
                    if (request.params.url) {
                        //browser.document.parentWindow.alert(browser.document.location.href + " -> " + request.params.url);
                        browser.executeScript("window.location.href = '" + request.params.url + "'");

                        processedResult = {
                            result: {
                                frameId: 5000.1
                            }
                        };
                    }
                    break;

                default:
                    processedResult = {};
                    break;
            }
{'fraction_non_alphanumeric': 0.08988298643471057, 'fraction_numerical': 0.002692347519933727, 'mean_word_length': 1.048356309650053, 'pattern_counts': {'":': 6, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2095383', 'n_tokens_mistral': 2151, 'n_tokens_neox': 1975, 'n_words': 473}