<commit_before>var SublimeTextArea = {
    openTab: function (url) {
        var optionsUrl = chrome.extension.getURL(url);

        chrome.tabs.query({url: optionsUrl}, function(tabs) {
            if (tabs.length) {
                chrome.tabs.update(tabs[0].id, {active: true});
            } else {
                chrome.tabs.create({url: optionsUrl});
            }
        });
    },

    serverPort: function (port) {
        if (port == null) {
            return localStorage.getItem('server-port') || 1337;
        }

        localStorage.setItem('server-port', port);
    },

    connectTextarea: function (textarea, title) {
        var that = this;
        var textareaDom = $(textarea).get(0);

        try {
            var webSocket = new WebSocket('ws://localhost:' + SublimeTextArea.serverPort());    
        } catch (e) {
            if(e.name && e.name === "SecurityError") {
                if(confirm('SublimeTextArea doesn\'t work on HTTPS pages in some versions of Chrome. Click OK to see how you can fix this.')){
                    window.open('https://github.com/Cacodaimon/SublimeTextArea/issues/5#issuecomment-44571987')
                }
            }
            return;
        }

        webSocket.onopen = function () {
            webSocket.send(that.textChange(title, textarea));
        };

        webSocket.onmessage = function (event) {
            var response = JSON.parse(event.data);
            textarea.val(response.text);

            textareaDom.selectionStart = response.cursor.min;
            textareaDom.selectionEnd = response.cursor.max;
            textareaDom.focus();
        };

        textarea.bind('input propertychange onmouseup', function() {
            webSocket.send(that.textChange(title, textarea));
        });
    },

    textChange: function (title, textarea) {
        var textareaDom = $(this).get(0);

        return JSON.stringify({
                title:  title,
                text:   textarea.val(),
                cursor: {
                    start: textareaDom.selectionStart,
                    end: textareaDom.selectionEnd
                },
            });
    },
};<commit_msg>Added some connection error handling<commit_after>var SublimeTextArea = {
    openTab: function (url) {
        var optionsUrl = chrome.extension.getURL(url);

        chrome.tabs.query({url: optionsUrl}, function(tabs) {
            if (tabs.length) {
                chrome.tabs.update(tabs[0].id, {active: true});
            } else {
                chrome.tabs.create({url: optionsUrl});
            }
        });
    },

    serverPort: function (port) {
        if (port == null) {
            return localStorage.getItem('server-port') || 1337;
        }

        localStorage.setItem('server-port', port);
    },

    connectTextarea: function (textarea, title) {
        var that = this;
        var textareaDom = $(textarea).get(0);

        try {
            var webSocket = new WebSocket('ws://localhost:' + SublimeTextArea.serverPort());    
        } catch (e) {
            that.errorHandler(e);
            return;
        }

        webSocket.onopen = function () {
            webSocket.send(that.textChange(title, textarea));
        };
        
        webSocket.onerror = function (event) {
            if (that.errorHandler(event)) {
	            textarea.off('.sta');
	            console.error('SublimeTexArea: detached from TextArea');
            }
        };

        webSocket.onmessage = function (event) {
            var response = JSON.parse(event.data);
            textarea.val(response.text);

            textareaDom.selectionStart = response.cursor.min;
            textareaDom.selectionEnd = response.cursor.max;
            textareaDom.focus();
        };

        textarea.on('input.sta propertychange.sta onmouseup.sta', function() {
            webSocket.send(that.textChange(title, textarea));
        });
    },
    textChange: function (title, textarea) {
        var textareaDom = $(this).get(0);

        return JSON.stringify({
                title:  title,
                text:   textarea.val(),
                cursor: {
                    start: textareaDom.selectionStart,
                    end: textareaDom.selectionEnd
                },
            });
    },
    errorHandler: function (e) {
        if(e && e.target && e.target.readyState === 3) {
            alert('Connection error. Make sure that Sublime Text is open and has SublimeTextArea installed. Try closing and opening it and try again. See if there are any errors in Sublime Text\'s console');   
            return true;//the error was handled
        } else if(e.name && e.name === "SecurityError") {
            if(confirm('SublimeTextArea doesn\'t work on HTTPS pages in some versions of Chrome. Click OK to see how you can fix this.')){
                window.open('https://github.com/Cacodaimon/SublimeTextArea/issues/5#issuecomment-44571987');
            }
            return true;//the error was handled
        }
    },
};
{'fraction_non_alphanumeric': 0.12136888181456426, 'fraction_numerical': 0.006565857540787903, 'mean_word_length': 1.7651265126512652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2988165', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1328, 'n_words': 290}