Last active
September 26, 2023 10:38
-
-
Save entrptaher/870246d824c10317edf72a0d4b86760e to your computer and use it in GitHub Desktop.
Fake Screen Resolution with Nightmarejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Credits and more resources: | |
https://gist.github.com/emenoh/65708b03f1a99d92f14db9b0d60d8fd0 | |
https://chromium.googlesource.com/chromium/blink-public/+/master/web/WebDeviceEmulationParams.h | |
*/ | |
const Nightmare = require('nightmare'); | |
Nightmare.action('emulateDevice', | |
(name, options, parent, win, renderer, done) => { | |
parent.respondTo('emulateDevice', (settings, done) => { | |
win.webContents.on('did-get-response-details', () => { | |
win.webContents.enableDeviceEmulation(settings); | |
}); | |
done(); | |
}); | |
done(); | |
}, | |
function(settings, done) { | |
this.child.call('emulateDevice', settings, done); | |
}); | |
const mobilesettings = (CustomWidth, CustomHeight) => ({ | |
// https://chromium.googlesource.com/chromium/blink-public/+/master/web/WebDeviceEmulationParams.h | |
// screenPosition can either be Desktop or Mobile, if it's Mobile, then custom dimensions are used | |
screenPosition: 'mobile', | |
screenSize: { | |
width: CustomWidth, | |
height: CustomHeight | |
}, | |
viewSize: { | |
width: CustomWidth, | |
height: CustomHeight | |
}, | |
deviceScaleFactor: 0, | |
viewPosition: { | |
x: 0, | |
y: 0 | |
}, | |
fitToView: true, | |
offset: { | |
x: 0, | |
y: 0 | |
} | |
}) | |
let nightmare = Nightmare({ | |
show: true | |
}); | |
nightmare | |
.then(() => { | |
return nightmare.emulateDevice(mobilesettings(800, 600)) | |
.goto("http://www.whatismyscreenresolution.com/") | |
.wait() | |
.screenshot("TEST_1_.png") | |
// more actions here | |
}) | |
.then(() => { | |
return nightmare.emulateDevice(mobilesettings(1000, 600)) | |
.goto("https://browserleaks.com/javascript") | |
.wait() | |
.screenshot("TEST_2_.png") | |
// more actions here | |
}) | |
.then(() => { | |
return nightmare.emulateDevice(mobilesettings(1200, 600)) | |
.goto("http://mydevice.io/") | |
.wait() | |
.screenshot("TEST_3_.png") | |
// more actions here | |
}) | |
.then(() => { | |
return nightmare.emulateDevice(mobilesettings(800, 600)) | |
.goto("http://www.screenresolution.org/") | |
.wait() | |
.screenshot("TEST_4_.png") | |
// more actions here | |
}).then(() => { | |
return nightmare.end().then(()=>{ | |
console.log('Finished faking instance life, It was a tough road!') | |
}) | |
// more actions here | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment