Hi, let me give you some context:
I tried several times to address this problem, but due to limitations of the "iframe" for security reasons, most websites have a filter that prevents a website from being played within another (or in this case within the game):
Here I propose 3 options:
Save any of the codes from option 1 or 2 with the following name:
WebViewerMZ.js
and Incorporate that plugin into your project like any other.
(Open a notepad, copy and paste the desired code, and save it in your plugins folder with the name "WebViewerMZ.js" without quotes)
Remember to activate it from the rpg maker mz plugin manager.
You can call plugins with the Call Plugin option
1) Integrate the Iframe to the Game:
JavaScript:
/*:
* @target MZ
* @plugindesc Web Viewer for RPG MAKER MZ
* @author Maxii1996 | Undermax Games
*
* @command showWebViewer
* @text Show Web Viewer
* @desc Displays the web viewer with the specified URL.
*
* @arg url
* @type string
* @text URL
* @desc URL to be loaded in the web viewer.
* @default https://www.example.com
*
* @param closeButtonSize
* @type number
* @text Close Button Size
* @desc Size of the close button in pixels.
* @default 30
*
* @param closeButtonPosition
* @type select
* @text Close Button Position
* @desc Position of the close button.
* @default Top Right
* @option Top Left
* @option Top Center
* @option Top Right
* @option Bottom Left
* @option Bottom Center
* @option Bottom Right
*
* @help
* To open the web viewer, use the plugin command "Show Web Viewer" in events.
*/
(() => {
const pluginName = "WebViewerMZ";
const parameters = PluginManager.parameters(pluginName);
PluginManager.registerCommand(pluginName, "showWebViewer", args => {
SceneManager.push(Scene_WebViewer);
Scene_WebViewer.prototype.url = args.url;
});
class Scene_WebViewer extends Scene_Base {
create() {
super.create();
this.createLoadingText();
this.createIframe();
this.createCloseButton();
}
createCloseButton() {
this._closeButton = document.createElement('button');
this._closeButton.innerText = 'X';
this._closeButton.style.position = 'absolute';
this._closeButton.style.width = parameters.closeButtonSize + 'px';
this._closeButton.style.height = parameters.closeButtonSize + 'px';
this._closeButton.style.zIndex = 10000; // Ensure it's above the iframe
this.setButtonPosition();
this._closeButton.onclick = this.closeScene.bind(this);
document.body.appendChild(this._closeButton);
}
createLoadingText() {
this._loadingText = new PIXI.Text('Loading...', { fontSize: 28, fill: 0xffffff });
this._loadingText.x = (Graphics.width - this._loadingText.width) / 2;
this._loadingText.y = (Graphics.height - this._loadingText.height) / 2;
this.addChild(this._loadingText);
}
createIframe() {
this._iframe = document.createElement('iframe');
this.updateIframeSize();
this._iframe.src = this.url;
this._iframe.style.border = 'none';
this._iframe.style.position = 'absolute';
this._iframe.style.zIndex = 9999;
this._iframe.style.backgroundColor = 'white';
this._iframe.onload = this.onIframeLoad.bind(this);
document.body.appendChild(this._iframe);
window.addEventListener('resize', this.onWindowResize.bind(this));
document.addEventListener('keydown', this.onGlobalKeydown.bind(this));
}
setButtonPosition() {
const position = parameters.closeButtonPosition;
const width = Graphics.width;
const height = Graphics.height;
const size = parseInt(parameters.closeButtonSize);
switch (position) {
case "Top Left":
this._closeButton.style.left = '10px';
this._closeButton.style.top = '10px';
break;
case "Top Center":
this._closeButton.style.left = (width - size) / 2 + 'px';
this._closeButton.style.top = '10px';
break;
case "Top Right":
this._closeButton.style.right = '10px';
this._closeButton.style.top = '10px';
break;
case "Bottom Left":
this._closeButton.style.left = '10px';
this._closeButton.style.bottom = '10px';
break;
case "Bottom Center":
this._closeButton.style.left = (width - size) / 2 + 'px';
this._closeButton.style.bottom = '10px';
break;
case "Bottom Right":
this._closeButton.style.right = '10px';
this._closeButton.style.bottom = '10px';
break;
}
}
onGlobalKeydown(event) {
if ((event.key === 'Escape' || event.key === 'Esc') && SceneManager._scene instanceof Scene_WebViewer) {
this.closeScene();
}
}
updateIframeSize() {
this._iframe.style.width = window.innerWidth + 'px';
this._iframe.style.height = window.innerHeight + 'px';
this._iframe.style.left = '0px';
this._iframe.style.top = '0px';
}
onIframeLoad() {
this.removeChild(this._loadingText);
}
onWindowResize() {
this.updateIframeSize();
this.setButtonPosition();
}
update() {
super.update();
if (Input.isTriggered('cancel')) {
this.closeScene();
}
}
closeScene() {
if (this._iframe && this._iframe.parentNode) {
document.body.removeChild(this._iframe);
}
window.removeEventListener('resize', this.onWindowResize.bind(this));
SceneManager.pop();
document.removeEventListener('keydown', this.onGlobalKeydown.bind(this));
if (this._closeButton && this._closeButton.parentNode) {
document.body.removeChild(this._closeButton);
}
}
}
})();
This may be the most desired option, but the most difficult to do, since what it does is integrate an iframe into the game and therefore uses the same game window to display a web page. I included a button to close the window and continue the game.
Problem of this:
That many sites have a system to prevent this type of system, but a basic web page, perhaps with a domain that you have, can be done without problem as long as you do not block this functionality.
2) Use nw.js as a floating window:
This is perhaps a more interesting option, since it allows you to view more websites than in case 1) but with the drawback that the websites open in a separate window to the game. (Not the default browser, but in a secondary window of NW.exe)
JavaScript:
/*:
* @target MZ
* @plugindesc Web Viewer for RPG MAKER MZ using NW.js
* @author Maxii1996 | Undermax Games
*
* @command showWebViewer
* @text Show Web Viewer
* @desc Displays the web viewer with the specified URL.
*
* @arg url
* @type string
* @text URL
* @desc URL to be loaded in the web viewer.
* @default https://www.google.com
*
* @arg windowWidth
* @type number
* @text Window Width
* @desc Width of the web viewer window.
* @default 800
*
* @arg windowHeight
* @type number
* @text Window Height
* @desc Height of the web viewer window.
* @default 600
*
* @help
* To open the web viewer, use the plugin command "Show Web Viewer" in events.
*/
(() => {
const pluginName = "WebViewerMZ";
window.webViewerIsOpen = false;
const originalUpdate = Scene_Map.prototype.update;
Scene_Map.prototype.update = function() {
if (!window.webViewerIsOpen) {
originalUpdate.call(this);
}
};
PluginManager.registerCommand(pluginName, "showWebViewer", args => {
const nw = require('nw.gui');
nw.Window.open(args.url, {
width: parseInt(args.windowWidth) || 800,
height: parseInt(args.windowHeight) || 600,
position: 'center',
focus: true
}, (webViewerWindow) => {
webViewerWindow.setAlwaysOnTop(true);
window.webViewerIsOpen = true;
webViewerWindow.on('blur', function() {
webViewerWindow.focus();
});
webViewerWindow.on('closed', function() {
window.webViewerIsOpen = false;
const mainWindow = nw.Window.get();
mainWindow.focus();
});
});
});
})();
3) open a URL with the default browser.
This may be the most compatible option, but the least user friendly.
You can do it with a simple script call:
I remain at your disposal!
Sincerely, Maxii1996