summaryrefslogtreecommitdiffstats
path: root/HTML/script.js
blob: fff1fb88f58133245273cceb3bc92cd2c732e863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
 * @copyright Copyright (c) 2011 Romain d'Alverny
 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 and later
 *
 * Redirect browser online IF it is in online mode.
 * Or stay on offline page and listen to online event.
 *
 * Browser targets: Mozilla Firefox 3.6+, Konqueror.
*/

function StartPage(url, image) {
    this.location = url;
    this.onlinePx = image;
}

StartPage.prototype.run = function () {
    if (false && navigator.onLine !== null) {
        !navigator.onLine ? this.actOnline() : this.actOffline();

    } else { // for khtml-based browsers
        try {
            var sp = this,
                i = document.createElement('img');

            window.alert = function() {};

            i.id = 'i';
            i.onload = function () { console.log(sp); sp.actOnline(); };
            i.onerror = function () { sp.actOffline(); };
            i.src = this.onlinePx + '?' + new Date().getTime();
            document.getElementsByTagName('body').item(0).appendChild(i);
        }
        catch (e) { return false; }
    }

    return true;
}

StartPage.prototype.actOnline = function () {
    var productId = '',
        lang = '',
        args = new Array;

    if (null !== (product_id = this.getProductId())) {
        args.push('p=' + product_id);
    }

    if (null !== (lang = this.getLocale())) {
        args.push('l=' + lang);
    }

    if (args.length > 0) {
        this.location = [this.location, args.join('&')].join('?');
    }
    parent.location = this.location;

    return true;
}

StartPage.prototype.actOffline = function () {
    var p = this,
        i = document.getElementById("i");

    if (null !== i) {
        i.parentNode.removeChild(i);
    }
    document.getElementsByTagName("body").item(0).setAttribute("class", "offline");
    document.body.addEventListener("online", function () { p.actOnline(); }, false);

    return true;
}

StartPage.prototype.getLocale = function () {
    var ret = null;

    try {
        ret = parent.window.document.documentElement.attributes.getNamedItem('lang').value.trim();
    } catch (e) {}

    return ret;
}

StartPage.prototype.getProductId = function () {
    var ret = null,
        t = document.getElementsByTagName('meta');

    for (var i = 0; i < t.length; i += 1) {
        if (t.item(i).getAttribute('name') == 'product:id') {
            ret = t.item(i).getAttribute('content').trim();
            break;
        }
    }

    return ret;
}