Why detect the simulator
You may want to run some code only when your website is displayed in the simulator: disable a feature that makes no sense on a computer, hide a banner, add debug logs, etc. When the simulator is active, the extension defines a global variable window.isMobileFirstSimulatorActive set to true in your website's page.
Detect the simulator
Step 1
Add this function to your website. It returns a Promise that resolves as soon as the variable exists, whether it is already defined or defined later.
function whenVariableExists(name, target = window) {
return new Promise((resolve) => {
// If the variable is already defined
if (name in target) {
return resolve(target[name]);
}
// Otherwise, intercept its future assignment
let value;
Object.defineProperty(target, name, {
configurable: true,
enumerable: true,
get() {
return value;
},
set(newValue) {
value = newValue;
resolve(newValue);
}
});
});
} Step 2
Use it to wait for isMobileFirstSimulatorActive and run your code.
// Usage:
whenVariableExists('isMobileFirstSimulatorActive').then((value) => {
// Code to execute
}); Need help?
If you still have a question, contact us.