i made a game that doesn’t work on the Replit page, it requires you to go to its website to play, is there a way do detect whether or not the user is running my repl on the replit page or using the website and show different content if they are on replit page, I am getting toooo many comments about it not working and people aren’t checking the description witch tells them to open in a new tab
If you’re serving HTML content, you could use a JavaScript script to check either the size of the window
(if that’s what really matters) or whether the window
is within an iframe
.
If size matters:
window.addEventListener("load", function() {
if (window.innerWidth <= some size || window.innerHeight <= some size) {
// you probably don't want to use alert, this is just to demonstrate
alert("Please open in a larger window.");
}
});
If iframe
matters:
if (window !== window.top) {
// you probably don't want to use alert, this is just to demonstrate
alert("Please open this Repl in its own window.");
}
ok thank you, just wondering is there a way to do it by detecting what the URL is for the page, like if page URL is https://replit.com/@username/project_name?v=1 then show the alert?
window.location.href
gives you the URL of the current page, however in an iframe
, I believe that that would just be whatever the URL is of the webpage displayed in the iframe
.
I made a library you can use for this: Replit WebView Detector
To use it in your game, just put this in the <head>
section of your HTML file:
<!-- load the WebView detector library -->
<script src="https://webview-detector.luisafk.repl.co/webview-detector.js"></script>
And then the following in your JavaScript file:
const webViewDetector = new WebViewDetector();
if (webViewDetector.isWebView()) {
alert('Please open the game in a new tab');
}