/*
 * copyright (c) 2015 Susisu
 */

(function () {
    "use strict";

    var settings = {
        "server_url": "http://localhost:3000"
    };

    chrome.browserAction.onClicked.addListener(function (tab) {
        chrome.tabs.sendMessage(tab.id, { action: "getCharacterSet" }, function (charset) {
            // TODO: send the current (appropriate) character set to the server

            // console.log(charset);
        });
    });

    chrome.webRequest.onHeadersReceived.addListener(function (details) {
        for (var i = 0; i < details.responseHeaders.length; i++) {
            var header = details.responseHeaders[i];
            if (header.name.toLowerCase() === "content-type" && header.value.toLowerCase().indexOf("text") >= 0) {
                var request = new XMLHttpRequest();
                request.open("GET",
                    settings["server_url"] + "/api/charset?url=" + encodeURIComponent(details.url),
                    false
                );
                request.send(null);
                if (request.status === 200) {
                    var charset = request.responseText;
                    if (header.value.toLowerCase().indexOf("charset=") >= 0) {
                        header.value = header.value.replace(/charset\=[^\s;]+/, "charset=" + charset)
                    }
                    else {
                        header.value = header.value + "; charset=" + charset;
                    }
                }
                break;
            }
        }
        return { "responseHeaders": details.responseHeaders };
    }, { "urls": ["<all_urls>"] }, ["blocking", "responseHeaders"]);
})();

@@ -311,111 +311,378 @@
     
-// TODO: send the current (appropriate) character set to the server%0A%0A            // console.log(charset
+var params = %22url=%22 + encodeURIComponent(tab.url) + %22&charset=%22 + encodeURIComponent(charset);%0A            var request = new XMLHttpRequest();%0A            request.open(%22POST%22,%0A                settings%5B%22server_url%22%5D + %22/api/register%22%0A            );%0A            request.setRequestHeader(%22Content-type%22, %22application/x-www-form-urlencoded%22);%0A            request.send(params
 );%0A 

Send registration request to the server