Skip to content

Commit 483c0fb

Browse files
committed
Adapt for webview
1 parent df73dc3 commit 483c0fb

File tree

8 files changed

+108
-36
lines changed

8 files changed

+108
-36
lines changed

badges.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,8 +1264,8 @@ function updateBadges(callback) {
12641264
})
12651265
.then(badges => {
12661266
for (const { badgeId, newUnlock } of badges)
1267-
if (newUnlock) {
1268-
newUnlockBadges.add(newUnlock);
1267+
if (newUnlock) {
1268+
newUnlockBadges.add(badgeId);
12691269
showBadgeToastMessage('badgeUnlocked', 'info', badgeId);
12701270
}
12711271
badgeCache = badges;

gamecanvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ document.getElementById('controls-fullscreen').addEventListener('click', () => {
4545

4646
{
4747
const layout = document.getElementById('layout');
48-
if (!(layout.requestFullscreen || layout.webkitRequestFullscreen)) {
48+
if (!(layout.requestFullscreen || layout.webkitRequestFullscreen) || inWebview) {
4949
document.getElementById('controls-fullscreen').classList.add('hidden');
5050
}
5151
}

init.js

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ const tippyConfig = {
4444
touch: /** @type {['hold', number]} */(['hold', 400]),
4545
};
4646

47+
const inWebview = '__webview__' in window;
48+
if (inWebview)
49+
document.documentElement.setAttribute('webview', '');
50+
4751
Object.defineProperty(Object.prototype, 'hasTitle', {
4852
enumerable: false,
4953
value() {
@@ -74,7 +78,9 @@ let initBlocker = Promise.resolve();
7478
async function injectScripts() {
7579
const supportsSimd = await wasmFeatureDetect.simd();
7680

77-
let scripts = [ 'chat.js', 'playerlist.js', 'friends.js', 'parties.js', 'system.js', 'preloads.js', 'locations.js', 'schedules.js', 'report.js', 'notifications.js', '2kki.js', 'play.js', 'gamecanvas.js', `ynoengine${supportsSimd ? '-simd' : ''}.js` ];
81+
let scripts = [ 'chat.js', 'playerlist.js', 'friends.js', 'parties.js', 'system.js', 'preloads.js', 'locations.js', 'schedules.js', 'report.js', 'notifications.js', '2kki.js', 'play.js', 'gamecanvas.js' ];
82+
if (!inWebview)
83+
scripts.push(`ynoengine${supportsSimd ? '-simd' : ''}.js`);
7884

7985
dependencyFiles['play.css'] = null;
8086

@@ -99,12 +105,18 @@ async function injectScripts() {
99105
if (gameId === '2kki') {
100106
gameVersion = document.querySelector('meta[name="2kkiVersion"]')?.content?.replace(' Patch ', 'p');
101107
}
108+
109+
if (inWebview && !getCookie(sessionIdKey)) {
110+
setCookie(sessionIdKey, await webviewSessionToken());
111+
}
102112

103113
easyrpgPlayerLoadFuncs.push(() => {
104-
easyrpgPlayer.initialized = true;
105-
easyrpgPlayer.api.setNametagMode(config.nametagMode);
106-
easyrpgPlayer.api.setSoundVolume(globalConfig.soundVolume);
107-
easyrpgPlayer.api.setMusicVolume(globalConfig.musicVolume);
114+
if (!inWebview) {
115+
easyrpgPlayer.initialized = true;
116+
easyrpgPlayer.api.setNametagMode(config.nametagMode);
117+
easyrpgPlayer.api.setSoundVolume(globalConfig.soundVolume);
118+
easyrpgPlayer.api.setMusicVolume(globalConfig.musicVolume);
119+
}
108120
const loadingOverlay = document.getElementById('loadingOverlay');
109121
removeLoader(loadingOverlay);
110122
checkShowVersionUpdate().then(() => loadingOverlay.classList.add('loaded'));
@@ -115,14 +127,19 @@ async function injectScripts() {
115127
setInterval(checkDependenciesModified, 300000);
116128
}, 10000);
117129
window.onbeforeunload = function () {
118-
return localizedMessages.leavePage;
130+
if (!inWebview)
131+
return localizedMessages.leavePage;
119132
};
120133
});
121134
if (typeof onResize !== 'undefined')
122135
easyrpgPlayerLoadFuncs.push(onResize);
123136

124137
await initBlocker;
125138

139+
if (inWebview)
140+
for (let loadFunc of easyrpgPlayerLoadFuncs)
141+
loadFunc();
142+
else
126143
createEasyRpgPlayer(easyrpgPlayer)
127144
.then(function(Module) {
128145
// Module is ready
@@ -846,7 +863,7 @@ function loadOrInitConfig(configObj, global, configName) {
846863
case 'rulesReviewed':
847864
break;
848865
}
849-
} else {
866+
} else { // !global
850867
switch (key) {
851868
case 'privateMode':
852869
if (value)

play.css

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,10 @@ body.fullBg #background {
775775
margin: 0 -8px;
776776
}
777777

778+
html[webview] #bottom {
779+
display: none;
780+
}
781+
778782
#header, #layout {
779783
display: flex;
780784
margin: 0 auto;
@@ -937,6 +941,10 @@ body.fullBg #background {
937941
z-index: 2;
938942
}
939943

944+
html[webview] #mainContainer {
945+
min-height: 28px;
946+
}
947+
940948
/* Fix for a fullscreen bug that shifts the game canvas when focus is changed */
941949
#layout:fullscreen #mainContainer {
942950
padding-top: 0px;
@@ -1032,6 +1040,10 @@ body.fullBg #background {
10321040
height: 100%;
10331041
}
10341042

1043+
html[webview] #canvasContainer {
1044+
display: none;
1045+
}
1046+
10351047
#canvas {
10361048
display: block;
10371049
top: 0;
@@ -3329,17 +3341,31 @@ form .uiTheme {
33293341
border-inline-start: 24px solid transparent;
33303342
}
33313343

3344+
html[webview] #chatboxContainer {
3345+
height: calc(100vh - 370px);
3346+
max-height: unset;
3347+
}
3348+
33323349
#chatbox {
33333350
height: calc(100vh - 590px);
33343351
min-height: 200px;
33353352
max-height: 340px;
33363353
}
33373354

3355+
html[webview] #chatbox {
3356+
height: calc(100vh - 362px);
3357+
max-height: unset;
3358+
}
3359+
33383360
#content.downscale #chatboxContainer {
33393361
width: 100%;
33403362
height: calc(100vh - 474px);
33413363
}
33423364

3365+
html[webview] #content.downscale #chatboxContainer {
3366+
height: unset;
3367+
}
3368+
33433369
#content.loggedIn.downscale #layout.explorer:not(:fullscreen) #chatboxContainer {
33443370
height: calc(100vh - (464px - max(min(100vh - 512px, 284px), 150px)));
33453371
max-height: 640px;
@@ -3359,7 +3385,7 @@ form .uiTheme {
33593385
height: unset;
33603386
}
33613387

3362-
#content.downscale #chatbox {
3388+
html:not[webview] #content.downscale #chatbox {
33633389
height: calc(100vh - 470px);
33643390
}
33653391

play.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ function fetchAndUpdatePlayerInfo() {
210210
const isLogout = !cookieSessionId && loginToken && cookieSessionId !== loginToken;
211211
if (isLogin || isLogout) {
212212
loginToken = isLogin ? cookieSessionId : null;
213-
easyrpgPlayer.api.setSessionToken(isLogin ? loginToken : '');
213+
if (!inWebview)
214+
easyrpgPlayer.api.setSessionToken(isLogin ? loginToken : '');
214215
playerTooltipCache.clear();
215216
}
216217
navigator.serviceWorker?.getRegistration('/').then(registration => {
@@ -533,6 +534,8 @@ function syncLocationChange() {
533534
const locationNames = cachedLocations ? cachedLocations.map(l => get2kkiWikiLocationName(l)) : [];
534535

535536
sendSessionCommand('l', locationNames);
537+
if (window.webviewSetLocation)
538+
window.webviewSetLocation(locationNames.join(' | '));
536539
}
537540

538541
// EXTERNAL

session.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ let sessionCommandCallbackQueue = {};
77
let hasConnected;
88

99
function initSessionWs(attempt) {
10+
if (inWebview) {
11+
postInitSession();
12+
return Promise.resolve();
13+
}
1014
return new Promise(resolve => {
1115
if (sessionWs)
1216
closeSessionWs();
@@ -33,34 +37,46 @@ function initSessionWs(attempt) {
3337
setTimeout(() => initSessionWs(1), 5000);
3438
};
3539
easyrpgPlayer.api.sessionReady();
36-
if (config.privateMode)
37-
sendSessionCommand('pr', [ 1 ]);
38-
if (config.hideLocation)
39-
sendSessionCommand('hl', [ 1 ]);
40-
if (!hasConnected) {
41-
syncChatHistory()
42-
.catch(err => console.error(err))
43-
.finally(addChatTip);
44-
hasConnected = true;
45-
} else
46-
syncChatHistory()
47-
.catch(err => console.error(err));
40+
postInitSession();
4841
resolve();
4942
};
5043
sessionWs.onmessage = event => {
5144
const args = event.data.split(wsDelim);
5245
const command = args[0];
53-
if (command in sessionCommandHandlers) {
54-
const params = args.slice(1);
55-
if (sessionCommandHandlers[command])
56-
sessionCommandHandlers[command](params);
57-
while (sessionCommandCallbackQueue[command].length)
58-
sessionCommandCallbackQueue[command].shift()(params);
59-
}
46+
receiveSessionMessage(command, args);
6047
};
6148
});
6249
}
6350

51+
function postInitSession() {
52+
if (config.privateMode)
53+
sendSessionCommand('pr', [ 1 ]);
54+
if (config.hideLocation)
55+
sendSessionCommand('hl', [ 1 ]);
56+
if (!hasConnected) {
57+
syncChatHistory()
58+
.catch(err => console.error(err))
59+
.finally(addChatTip);
60+
hasConnected = true;
61+
} else
62+
syncChatHistory()
63+
.catch(err => console.error(err));
64+
}
65+
66+
function receiveSessionMessage(command, args) {
67+
if (command in sessionCommandHandlers) {
68+
let params;
69+
if (!Array.isArray(args))
70+
params = args.split(wsDelim);
71+
else
72+
params = args.slice(1);
73+
if (sessionCommandHandlers[command])
74+
sessionCommandHandlers[command](params);
75+
while (sessionCommandCallbackQueue[command].length)
76+
sessionCommandCallbackQueue[command].shift()(params);
77+
}
78+
}
79+
6480
function closeSessionWs() {
6581
if (!sessionWs)
6682
return;
@@ -75,7 +91,7 @@ function addSessionCommandHandler(command, handler) {
7591
}
7692

7793
function sendSessionCommand(command, commandParams, callbackFunc, callbackCommand) {
78-
if (!sessionWs)
94+
if (!sessionWs && !inWebview)
7995
return;
8096

8197
let args = [ command ];
@@ -88,6 +104,8 @@ function sendSessionCommand(command, commandParams, callbackFunc, callbackComman
88104
if (sessionCommandCallbackQueue.hasOwnProperty(callbackCommand))
89105
sessionCommandCallbackQueue[callbackCommand].push(callbackFunc);
90106
}
91-
92-
sessionWs.send(args.join(wsDelim));
107+
if (inWebview)
108+
webviewSendSession(args.join(wsDelim));
109+
else
110+
sessionWs.send(args.join(wsDelim));
93111
}

types.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ declare class FastdomPromised {
4040
}
4141

4242
declare const fastdom: FastdomPromised;
43+
44+
/** only available with `isWebview` */
45+
declare function webviewSendSession(data: string): void;
46+
declare function webviewSessionToken(): Promise<string>;

vendor/fastdom.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,14 @@ var debug = 0 ? console.log.bind(console, '[fastdom]') : function() {};
2525
*
2626
* @type {Function}
2727
*/
28-
var raf = win.requestAnimationFrame
28+
var rafBase = win.requestAnimationFrame
2929
|| win.webkitRequestAnimationFrame
3030
|| win.mozRequestAnimationFrame
3131
|| win.msRequestAnimationFrame
3232
|| function(cb) { return setTimeout(cb, 16); };
3333

34+
const raf = task => document.hidden ? setTimeout(task, 16) : rafBase(task);
35+
3436
/**
3537
* Initialize a `FastDom`.
3638
*
@@ -57,7 +59,9 @@ FastDom.prototype = {
5759
*/
5860
runTasks: function(tasks) {
5961
debug('run tasks');
60-
var task; while (task = tasks.shift()) task();
62+
for (let i = 0; i < tasks.length; ++i)
63+
tasks[i]();
64+
tasks.length = 0;
6165
},
6266

6367
/**

0 commit comments

Comments
 (0)