1 |
/** |
2 |
* @copyright Copyright (c) 2011 Romain d'Alverny |
3 |
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU GPL v3 and later |
4 |
* |
5 |
* Redirect browser online IF it is in online mode. |
6 |
* Or stay on offline page and listen to online event. |
7 |
* |
8 |
* Browser targets: Mozilla Firefox 3.6+, Konqueror. |
9 |
*/ |
10 |
|
11 |
function StartPage(url, image) { |
12 |
this.location = url; |
13 |
this.onlinePx = image; |
14 |
} |
15 |
|
16 |
StartPage.prototype.run = function () { |
17 |
if (false && navigator.onLine !== null) { |
18 |
!navigator.onLine ? this.actOnline() : this.actOffline(); |
19 |
|
20 |
} else { // for khtml-based browsers |
21 |
try { |
22 |
var sp = this, |
23 |
i = document.createElement('img'); |
24 |
|
25 |
window.alert = function() {}; |
26 |
|
27 |
i.id = 'i'; |
28 |
i.onload = function () { console.log(sp); sp.actOnline(); }; |
29 |
i.onerror = function () { sp.actOffline(); }; |
30 |
i.src = this.onlinePx + '?' + new Date().getTime(); |
31 |
document.getElementsByTagName('body').item(0).appendChild(i); |
32 |
} |
33 |
catch (e) { return false; } |
34 |
} |
35 |
|
36 |
return true; |
37 |
} |
38 |
|
39 |
StartPage.prototype.actOnline = function () { |
40 |
var productId = '', |
41 |
lang = '', |
42 |
args = new Array; |
43 |
|
44 |
if (null !== (product_id = this.getProductId())) { |
45 |
args.push('p=' + product_id); |
46 |
} |
47 |
|
48 |
if (null !== (lang = this.getLocale())) { |
49 |
args.push('l=' + lang); |
50 |
} |
51 |
|
52 |
if (args.length > 0) { |
53 |
this.location = [this.location, args.join('&')].join('?'); |
54 |
} |
55 |
parent.location = this.location; |
56 |
|
57 |
return true; |
58 |
} |
59 |
|
60 |
StartPage.prototype.actOffline = function () { |
61 |
var p = this, |
62 |
i = document.getElementById("i"); |
63 |
|
64 |
if (null !== i) { |
65 |
i.parentNode.removeChild(i); |
66 |
} |
67 |
document.getElementsByTagName("body").item(0).setAttribute("class", "offline"); |
68 |
document.body.addEventListener("online", function () { p.actOnline(); }, false); |
69 |
|
70 |
return true; |
71 |
} |
72 |
|
73 |
StartPage.prototype.getLocale = function () { |
74 |
var ret = null; |
75 |
|
76 |
try { |
77 |
ret = parent.window.document.documentElement.attributes.getNamedItem('lang').value.trim(); |
78 |
} catch (e) {} |
79 |
|
80 |
return ret; |
81 |
} |
82 |
|
83 |
StartPage.prototype.getProductId = function () { |
84 |
var ret = null, |
85 |
t = document.getElementsByTagName('meta'); |
86 |
|
87 |
for (var i = 0; i < t.length; i += 1) { |
88 |
if (t.item(i).getAttribute('name') == 'product:id') { |
89 |
ret = t.item(i).getAttribute('content').trim(); |
90 |
break; |
91 |
} |
92 |
} |
93 |
|
94 |
return ret; |
95 |
} |