-
Notifications
You must be signed in to change notification settings - Fork 7
/
listenerItyc.js
251 lines (211 loc) · 7.74 KB
/
listenerItyc.js
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
let drawTheme = "dark";
let gameSize = 0;
let gameSizeApply = 0;
let readVal = window.localStorage.getItem('addOnTheme');
if(readVal) drawTheme = readVal;
readVal = window.localStorage.getItem('addOnGameSize');
if(readVal) gameSize = readVal;
let fullScreenState = false;
let dashInstallDrawn = false;
let dashState = "notInstall";
var manifestVersion = "0.0.0";
let originIframeWidth = 0;
let originIframeHeight = 0;
let currentIframeWidth = 0;
let currentIframeHeight = 0;
let originalSize;
window.addEventListener("load", function () {
var idC = document.getElementById('itycDashId');
if(idC) manifestVersion = idC.getAttribute('ver');
document.addEventListener("fullscreenchange", manageGoInFullScreen);
document.addEventListener("webkitfullscreenchange", manageGoInFullScreen);
document.addEventListener("mozfullscreenchange", manageGoInFullScreen);
document.addEventListener("MSFullscreenChange", manageGoInFullScreen);
document.documentElement.setAttribute("data-theme", drawTheme);
// chrome.tabs.onActivated.addListener(manageGoInFullScreen);
dashState = "notInstall";
const targetIframe = foundVRIframe();
if(targetIframe) {
sendMaxSize(targetIframe);
}
manageFullScreen2();
});
function detectIframeSize(targetIframe) {
originIframeWidth = Number(document.defaultView.getComputedStyle(targetIframe).width.replace('px', ''));
originIframeHeight = Number(document.defaultView.getComputedStyle(targetIframe).height.replace('px', ''));
}
function manageGoInFullScreen()
{
const targetIframe = foundVRIframe();
if(targetIframe) {
sendVRFullScreen(targetIframe);
sendMaxSize(targetIframe);
}
manageFullScreen2();
}
function foundVRIframe() {
const iframeO = Array.from(document.getElementsByClassName('iframe-class'));
return targetIframe = iframeO.find(iframe =>
iframe.getAttribute('src') === "https://play.offshore.virtualregatta.com/"
);
}
function reduceLangFlag() {
let flag = document.getElementById('trp-floater-ls');
if(flag) {
flag.style.setProperty('right', '0', 'important');
flag.style.setProperty('min-width', 'auto', 'important');
}
flag = document.querySelectorAll('img[src$="flags/fr_FR.png"]');
for(let i=0; i< flag.length;i++)
{
flag[i].parentElement.lastChild.nodeValue = "FR";
}
flag = document.querySelectorAll('img[src$="flags/en_GB.png"]');
for(let i=0; i< flag.length;i++)
{
flag[i].parentElement.lastChild.nodeValue = "EN";
}
}
window.onmessage = function(e) {
let msg = e.data;
if(msg && msg.port ) {
if(msg.port==("ItycIframe2VR" + manifestVersion)) {
if (msg.order === "resize") {
const targetIframe = foundVRIframe();
if (targetIframe) {
if(originIframeWidth == 0 || originIframeHeight == 0 )
{
detectIframeSize(targetIframe);
}
currentIframeWidth = msg.w;
currentIframeHeight = msg.h;
targetIframe.setAttribute("height", msg.h);
targetIframe.setAttribute("width", msg.w );
}
} else if (msg.order === "param") {
drawTheme = msg.theme;
window.localStorage.setItem('addOnTheme', drawTheme);
document.documentElement.setAttribute("data-theme", drawTheme);
gameSize = msg.gameSize;
window.localStorage.setItem('addOnGameSize', gameSize);
dashState = msg.state;
if(!msg.paramReceived )
{ const targetIframe = foundVRIframe();
if(targetIframe)
sendMaxSize(targetIframe);
}
}
manageFullScreen2();
}
}
return true;
};
function sendMaxSize(iframe)
{
let winHeight = window.innerHeight;
let winWidth = window.innerWidth;
/* remove padding-left and right */
winWidth -= 40;
winHeight -= 20;
iframe.contentWindow.postMessage(
{ port:"VR2Iframe" + manifestVersion,
order: "maxSize",
screenW : winWidth,
screenH : winHeight
}, '*');
}
function sendVRFullScreen(iframe) {
let fullscreen = false;
if (document.fullscreenElement || document.webkitFullscreenElement
|| document.mozFullScreenElement || document.msFullscreenElement)
fullscreen = true;
iframe.contentWindow.postMessage(
{ port:"VR2Iframe" + manifestVersion,
order: "fullScreenVR",
fullScreenVR : fullscreen
}, '*');
}
function manageFullScreen2() {
if (document.fullscreenElement || document.webkitFullscreenElement
|| document.mozFullScreenElement || document.msFullscreenElement)
{
} else
{
try {
if(dashState == "notInstall")
{
const targetIframe = foundVRIframe();
if (targetIframe) {
let ourDiv = document.getElementById('dashIntegRow');
if(ourDiv) ourDiv.remove();
ourDiv = document.createElement( 'div' );
ourDiv.id = 'dashIntegRow';
if(gameSize == 0)
ourDiv.style.setProperty('max_width', 'none');
else
ourDiv.style.setProperty('max_width', '1080px');
ourDiv.style.setProperty('margin_left', 'auto');
ourDiv.style.setProperty('margin_right', 'auto');
ourDiv.style.userSelect='none';
ourDiv.style.zIndex="1";
let ourDiv2 = document.createElement( 'div' );
ourDiv2.id = 'dashInteg';
ourDiv.appendChild(ourDiv2);
ourDiv.innerHTML = '<table id="raceStatusTable">'
+ '<thead>'
+ '<tr><th>ITYC Dashboard</th></tr>'
+ '</thead>'
+ '<tbody>'
+ '<tr><td>❌ Pas de dashboard détectée / No dashboard detected</td></tr>'
+ '</tbody>'
+ '</table>';
targetIframe.parentNode.insertBefore(ourDiv, targetIframe.nextSibling);
dashState = "install";
}
} else if(dashState == "detected")
{
let ourDiv = document.getElementById('dashIntegRow');
if(ourDiv) ourDiv.remove();
}
const targetElement = document.querySelector('[data-colibri-id="1752-c14"]');
if (targetElement) {
targetElement.style.setProperty('text-align', 'center');
}
if(gameSize != 0) {
let div = document.getElementById('hero');
if(div) div.style.setProperty('display', 'none', 'important');
div = document.querySelector('div[data-colibri-id="1342-h1"]');
if(div) div.style.setProperty('display', 'none', 'important');
div = document.querySelector('div[data-colibri-id="1342-h2"]');
if(div) div.style.removeProperty('position');
let elements = document.querySelectorAll('.h-section-boxed-container');
elements.forEach(el => {
originalSize = document.defaultView.getComputedStyle(el).getPropertyValue('max-width');
el.style.setProperty('max-width', '100%', 'important');
});
const targetIframe = foundVRIframe();
if (targetIframe) {
targetIframe.style.setProperty('margin-left', '20px', 'important');
targetIframe.style.setProperty('margin-right', '20px', 'important');
}
} else
{
let div =document.getElementById('hero');
if(div) div.style.removeProperty('display');
div = document.querySelector('div[data-colibri-id="1342-h2"]');
if(div) div.style.removeProperty('position');
div = document.querySelectorAll('.h-section-boxed-container');
div.forEach(el => {
el.style.setProperty('max-width', originalSize);
});
div = document.querySelector('div[data-colibri-id="1342-h1"]');
if(div)
{
div.style.removeProperty('display');
}
fullScreenState = false;
}
reduceLangFlag();
} catch(error) {console.log(error);}
}
}