// ==UserScript==
// @name         ProgrammersForumGeoIp
// @namespace    http://programmersforum.ru/
// @version      0.1
// @description  adds country/city info on the page with user IP
// @author       Alex P
// @include      http://programmersforum.ru/postings.php?do=getip*
// @include      http://www.programmersforum.ru/postings.php?do=getip*
// @grant        none
// @downloadURL  https://github.com/AlexP11223/ProgForumRuUserscripts/raw/master/pf_geoip.user.js
// ==/UserScript==

(function() {
    'use strict';

    function getJson(url, success, error) {
        const request = new XMLHttpRequest();
        request.open('GET', url, true);

        request.onload = function() {
            if (request.status >= 200 && request.status < 400) {
                const data = JSON.parse(request.responseText);
                success(data);
            } else {
                error(`HTTP error ${request.status} ${request.statusText}`);
            }
        };

        request.onerror = function() {
            error('Connection error');
        };

        request.send();
    }

    function requestIpApi(ip, success, error) {
        getJson(`http://ip-api.com/json/${ip}`, function (data) {
                if (data.status === 'success') {
                    success({ country: data.country, region: data.regionName, city: data.city, isp: data.isp });
                } else {
                    error(`API error ${data.message}`);
                }
            },
            error);
    }

    function requestFreegeoip(ip, success, error) {
        getJson(`http://freegeoip.net/json/${ip}`, function (data) {
                success({ country: data.country_name, region: data.region_name, city: data.city });
            },
            error);
    }

    function formatGeoipData(data) {
        let result = `${data.country}, ${data.region}, ${data.city}`;
        if (data.isp) {
            result += ` (провайдер ${data.isp})`;
        }
        return result;
    }

    function formatError(error) {
        return `<span style="color: red;">${error}</span>`;
    }

    const ipElement = window.document.querySelector('.panelsurround div.panel div div strong');
    if (!ipElement)
        return;

    const ip = ipElement.innerText;

    const container = ipElement.parentNode;

    function appendLine(name, content) {
        function elementFromString(html) {
            const range = document.createRange();
            return range.createContextualFragment(html);
        }

        container.appendChild(elementFromString(`<br>${name}: `));
        container.appendChild(elementFromString(`<strong>${content}</strong>`));
    }

    requestIpApi(ip, function (data) {
            appendLine('Месторасположение (ip-api.com)', formatGeoipData(data));
    }, function (error) {
        appendLine('Месторасположение (ip-api.com)', formatError(error));
    });

    requestFreegeoip(ip, function (data) {
            appendLine('Месторасположение (freegeoip.net)', formatGeoipData(data));
    }, function (error) {
        appendLine('Месторасположение (freegeoip.net)', formatError(error));
    });
})();

@@ -114,17 +114,17 @@
       0.
-1
+2
 %0A// @des
@@ -516,16 +516,81 @@
 rict';%0A%0A
+    const IPSTACK_API_KEY = 'b890dc8f7bc14c40deb7af6a2f9be451';%0A%0A
     func
@@ -1587,33 +1587,31 @@
 tion request
-Freegeoip
+Ipstack
 (ip, success
@@ -1649,31 +1649,58 @@
 p://
-freegeoip.net/json/$%7Bip
+api.ipstack.com/$%7Bip%7D?access_key=$%7BIPSTACK_API_KEY
 %7D%60, 
@@ -2997,17 +2997,15 @@
 uest
-Freegeoip
+Ipstack
 (ip,
@@ -3062,37 +3062,35 @@
 %D1%81%D0%BF%D0%BE%D0%BB%D0%BE%D0%B6%D0%B5%D0%BD%D0%B8%D0%B5 (
-freegeoip.net
+ipstack.com
 )', formatGe
@@ -3174,21 +3174,19 @@
 %D0%B8%D0%B5 (
-freegeoip.net
+ipstack.com
 )', 

update geoip ipstack.com