From d55cf9043e057ba5836418276b25a1f2c0a004a4 Mon Sep 17 00:00:00 2001 From: Kenneth Shaw Date: Fri, 14 Jun 2019 23:30:26 +0700 Subject: [PATCH] emulate: further changes Further changes to the emulate actions in chromedp. --- device/device.go | 370 ++++++++++++++++++++++++++++++++++------------- device/gen.go | 48 +++--- device/params.go | 290 ------------------------------------- device/types.go | 91 ++++++++++++ emulate.go | 51 +++++-- emulate_test.go | 18 --- 6 files changed, 422 insertions(+), 446 deletions(-) delete mode 100644 device/params.go create mode 100644 device/types.go diff --git a/device/device.go b/device/device.go index 6cf3f503..7871bf29 100644 --- a/device/device.go +++ b/device/device.go @@ -1,128 +1,292 @@ -// Package device contains device emulation definitions for use with chromedp's -// Emulate action. package device -//go:generate go run gen.go +// See: https://raw.githubusercontent.com/GoogleChrome/puppeteer/master/lib/DeviceDescriptors.js -import "github.com/chromedp/cdproto/emulation" +// Generated by gen.go. DO NOT EDIT. -// Device provides the common type for defined devices. -type Device int +// Devices. +const ( + // Reset is the reset device. + Reset deviceType = iota -// Device is the actual device. -type device struct { - // Name is the device name. - Name string + // BlackberryPlayBook is the "Blackberry PlayBook" device. + BlackberryPlayBook - // UserAgent is the device user agent string. - UserAgent string + // BlackberryPlayBooklandscape is the "Blackberry PlayBook landscape" device. + BlackberryPlayBooklandscape - // Width is the viewport width. - Width int64 + // BlackBerryZ30 is the "BlackBerry Z30" device. + BlackBerryZ30 - // Height is the viewport height. - Height int64 + // BlackBerryZ30landscape is the "BlackBerry Z30 landscape" device. + BlackBerryZ30landscape - // Scale is the device viewport scale factor. - Scale float64 + // GalaxyNote3 is the "Galaxy Note 3" device. + GalaxyNote3 - // Landscape indicates whether or not the device is in landscape mode or - // not. - Landscape bool + // GalaxyNote3landscape is the "Galaxy Note 3 landscape" device. + GalaxyNote3landscape - // Mobile indicates whether it is a mobile device or not. - Mobile bool + // GalaxyNoteII is the "Galaxy Note II" device. + GalaxyNoteII - // Touch indicates whether the device has touch enabled. - Touch bool -} + // GalaxyNoteIIlandscape is the "Galaxy Note II landscape" device. + GalaxyNoteIIlandscape -// String satisfies fmt.Stringer. -func (d Device) String() string { - if d < 1 || int(d) > len(devices) { - return "Invalid device" - } - return devices[d-1].Name -} + // GalaxySIII is the "Galaxy S III" device. + GalaxySIII -// UserAgent satisfies chromedp.Device. -func (d Device) UserAgent() string { - if d < 1 || int(d) > len(devices) { - panic("invalid device") - } - return devices[d-1].UserAgent -} + // GalaxySIIIlandscape is the "Galaxy S III landscape" device. + GalaxySIIIlandscape -// Viewport satisfies chromedp.Device. -func (d Device) Viewport() (int64, int64, []emulateViewportOption) { - if d < 1 || int(d) > len(devices) { - panic("invalid device") - } - dev := devices[d-1] - orientation := emulatePortrait - if dev.Landscape { - orientation = emulateLandscape - } - opts := []emulateViewportOption{ - emulateScale(dev.Scale), - orientation, - } - if dev.Mobile { - opts = append(opts, emulateMobile) - } - if dev.Touch { - opts = append(opts, emulateTouch) - } - return dev.Width, dev.Height, opts -} + // GalaxyS5 is the "Galaxy S5" device. + GalaxyS5 -/* - THE FOLLOWING ARE A COPY OF THE chromedp.EmulateViewport* options. + // GalaxyS5landscape is the "Galaxy S5 landscape" device. + GalaxyS5landscape - Provided here in order to prevent circular imports. -*/ + // IPad is the "iPad" device. + IPad -// emulateViewportOption is the type for emulate viewport options. -type emulateViewportOption = func(*emulation.SetDeviceMetricsOverrideParams, *emulation.SetTouchEmulationEnabledParams) + // IPadlandscape is the "iPad landscape" device. + IPadlandscape -// emulateScale is an emulate viewport option to set the device viewport scaling -// factor. -func emulateScale(scale float64) emulateViewportOption { - return func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - p1.DeviceScaleFactor = scale - } -} + // IPadMini is the "iPad Mini" device. + IPadMini -// emulateOrientation is an emulate viewport option to set the device viewport -// orientation. -func emulateOrientation(orientation emulation.OrientationType, angle int64) emulateViewportOption { - return func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - p1.ScreenOrientation = &emulation.ScreenOrientation{ - Type: orientation, - Angle: angle, - } - } -} + // IPadMinilandscape is the "iPad Mini landscape" device. + IPadMinilandscape -// emulateLandscape is an emulate viewport option to set the device viewport -// orientation in landscape primary mode and an angle of 90. -func emulateLandscape(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - emulateOrientation(emulation.OrientationTypeLandscapePrimary, 90)(p1, p2) -} + // IPadPro is the "iPad Pro" device. + IPadPro -// emulatePortrait is an emulate viewport option to set the device viewport -// orentation in portrait primary mode and an angle of 0. -func emulatePortrait(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - emulateOrientation(emulation.OrientationTypePortraitPrimary, 0)(p1, p2) -} + // IPadProlandscape is the "iPad Pro landscape" device. + IPadProlandscape -// emulateMobile is an emulate viewport option to toggle the device viewport to -// display as a mobile device. -func emulateMobile(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - p1.Mobile = true -} + // IPhone4 is the "iPhone 4" device. + IPhone4 + + // IPhone4landscape is the "iPhone 4 landscape" device. + IPhone4landscape + + // IPhone5 is the "iPhone 5" device. + IPhone5 + + // IPhone5landscape is the "iPhone 5 landscape" device. + IPhone5landscape + + // IPhone6 is the "iPhone 6" device. + IPhone6 + + // IPhone6landscape is the "iPhone 6 landscape" device. + IPhone6landscape + + // IPhone6Plus is the "iPhone 6 Plus" device. + IPhone6Plus + + // IPhone6Pluslandscape is the "iPhone 6 Plus landscape" device. + IPhone6Pluslandscape + + // IPhone7 is the "iPhone 7" device. + IPhone7 + + // IPhone7landscape is the "iPhone 7 landscape" device. + IPhone7landscape + + // IPhone7Plus is the "iPhone 7 Plus" device. + IPhone7Plus + + // IPhone7Pluslandscape is the "iPhone 7 Plus landscape" device. + IPhone7Pluslandscape + + // IPhone8 is the "iPhone 8" device. + IPhone8 + + // IPhone8landscape is the "iPhone 8 landscape" device. + IPhone8landscape + + // IPhone8Plus is the "iPhone 8 Plus" device. + IPhone8Plus + + // IPhone8Pluslandscape is the "iPhone 8 Plus landscape" device. + IPhone8Pluslandscape + + // IPhoneSE is the "iPhone SE" device. + IPhoneSE + + // IPhoneSElandscape is the "iPhone SE landscape" device. + IPhoneSElandscape + + // IPhoneX is the "iPhone X" device. + IPhoneX + + // IPhoneXlandscape is the "iPhone X landscape" device. + IPhoneXlandscape + + // JioPhone2 is the "JioPhone 2" device. + JioPhone2 + + // JioPhone2landscape is the "JioPhone 2 landscape" device. + JioPhone2landscape + + // KindleFireHDX is the "Kindle Fire HDX" device. + KindleFireHDX + + // KindleFireHDXlandscape is the "Kindle Fire HDX landscape" device. + KindleFireHDXlandscape + + // LGOptimusL70 is the "LG Optimus L70" device. + LGOptimusL70 + + // LGOptimusL70landscape is the "LG Optimus L70 landscape" device. + LGOptimusL70landscape + + // MicrosoftLumia550 is the "Microsoft Lumia 550" device. + MicrosoftLumia550 + + // MicrosoftLumia950 is the "Microsoft Lumia 950" device. + MicrosoftLumia950 + + // MicrosoftLumia950landscape is the "Microsoft Lumia 950 landscape" device. + MicrosoftLumia950landscape + + // Nexus10 is the "Nexus 10" device. + Nexus10 + + // Nexus10landscape is the "Nexus 10 landscape" device. + Nexus10landscape + + // Nexus4 is the "Nexus 4" device. + Nexus4 + + // Nexus4landscape is the "Nexus 4 landscape" device. + Nexus4landscape + + // Nexus5 is the "Nexus 5" device. + Nexus5 + + // Nexus5landscape is the "Nexus 5 landscape" device. + Nexus5landscape + + // Nexus5X is the "Nexus 5X" device. + Nexus5X + + // Nexus5Xlandscape is the "Nexus 5X landscape" device. + Nexus5Xlandscape + + // Nexus6 is the "Nexus 6" device. + Nexus6 + + // Nexus6landscape is the "Nexus 6 landscape" device. + Nexus6landscape + + // Nexus6P is the "Nexus 6P" device. + Nexus6P + + // Nexus6Plandscape is the "Nexus 6P landscape" device. + Nexus6Plandscape + + // Nexus7 is the "Nexus 7" device. + Nexus7 + + // Nexus7landscape is the "Nexus 7 landscape" device. + Nexus7landscape + + // NokiaLumia520 is the "Nokia Lumia 520" device. + NokiaLumia520 + + // NokiaLumia520landscape is the "Nokia Lumia 520 landscape" device. + NokiaLumia520landscape + + // NokiaN9 is the "Nokia N9" device. + NokiaN9 + + // NokiaN9landscape is the "Nokia N9 landscape" device. + NokiaN9landscape + + // Pixel2 is the "Pixel 2" device. + Pixel2 + + // Pixel2landscape is the "Pixel 2 landscape" device. + Pixel2landscape + + // Pixel2XL is the "Pixel 2 XL" device. + Pixel2XL + + // Pixel2XLlandscape is the "Pixel 2 XL landscape" device. + Pixel2XLlandscape +) -// emulateTouch is an emulate viewport option to enable touch emulation. -func emulateTouch(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { - p2.Enabled = true +// devices is the list of devices. +var devices = [...]Device{ + {"", "", 0, 0, 0.000000, false, false, false}, + {"Blackberry PlayBook", "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+", 600, 1024, 1.000000, false, true, true}, + {"Blackberry PlayBook landscape", "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+", 1024, 600, 1.000000, true, true, true}, + {"BlackBerry Z30", "Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+", 360, 640, 2.000000, false, true, true}, + {"BlackBerry Z30 landscape", "Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+", 640, 360, 2.000000, true, true, true}, + {"Galaxy Note 3", "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 3.000000, false, true, true}, + {"Galaxy Note 3 landscape", "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 3.000000, true, true, true}, + {"Galaxy Note II", "Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 2.000000, false, true, true}, + {"Galaxy Note II landscape", "Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 2.000000, true, true, true}, + {"Galaxy S III", "Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 2.000000, false, true, true}, + {"Galaxy S III landscape", "Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 2.000000, true, true, true}, + {"Galaxy S5", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 360, 640, 3.000000, false, true, true}, + {"Galaxy S5 landscape", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 360, 3.000000, true, true, true}, + {"iPad", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 768, 1024, 2.000000, false, true, true}, + {"iPad landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 768, 2.000000, true, true, true}, + {"iPad Mini", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 768, 1024, 2.000000, false, true, true}, + {"iPad Mini landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 768, 2.000000, true, true, true}, + {"iPad Pro", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 1366, 2.000000, false, true, true}, + {"iPad Pro landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1366, 1024, 2.000000, true, true, true}, + {"iPhone 4", "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 320, 480, 2.000000, false, true, true}, + {"iPhone 4 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 480, 320, 2.000000, true, true, true}, + {"iPhone 5", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 320, 568, 2.000000, false, true, true}, + {"iPhone 5 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 568, 320, 2.000000, true, true, true}, + {"iPhone 6", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, + {"iPhone 6 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, + {"iPhone 6 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, + {"iPhone 6 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, + {"iPhone 7", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, + {"iPhone 7 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, + {"iPhone 7 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, + {"iPhone 7 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, + {"iPhone 8", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, + {"iPhone 8 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, + {"iPhone 8 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, + {"iPhone 8 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, + {"iPhone SE", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 320, 568, 2.000000, false, true, true}, + {"iPhone SE landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 568, 320, 2.000000, true, true, true}, + {"iPhone X", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 812, 3.000000, false, true, true}, + {"iPhone X landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 812, 375, 3.000000, true, true, true}, + {"JioPhone 2", "Mozilla/5.0 (Mobile; LYF/F300B/LYF-F300B-001-01-15-130718-i;Android; rv:48.0) Gecko/48.0 Firefox/48.0 KAIOS/2.5", 240, 320, 1.000000, false, true, true}, + {"JioPhone 2 landscape", "Mozilla/5.0 (Mobile; LYF/F300B/LYF-F300B-001-01-15-130718-i;Android; rv:48.0) Gecko/48.0 Firefox/48.0 KAIOS/2.5", 320, 240, 1.000000, true, true, true}, + {"Kindle Fire HDX", "Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true", 800, 1280, 2.000000, false, true, true}, + {"Kindle Fire HDX landscape", "Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true", 1280, 800, 2.000000, true, true, true}, + {"LG Optimus L70", "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3765.0 Mobile Safari/537.36", 384, 640, 1.250000, false, true, true}, + {"LG Optimus L70 landscape", "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 384, 1.250000, true, true, true}, + {"Microsoft Lumia 550", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 640, 360, 2.000000, false, true, true}, + {"Microsoft Lumia 950", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 360, 640, 4.000000, false, true, true}, + {"Microsoft Lumia 950 landscape", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 640, 360, 4.000000, true, true, true}, + {"Nexus 10", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 800, 1280, 2.000000, false, true, true}, + {"Nexus 10 landscape", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 1280, 800, 2.000000, true, true, true}, + {"Nexus 4", "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 384, 640, 2.000000, false, true, true}, + {"Nexus 4 landscape", "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 384, 2.000000, true, true, true}, + {"Nexus 5", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 360, 640, 3.000000, false, true, true}, + {"Nexus 5 landscape", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 360, 3.000000, true, true, true}, + {"Nexus 5X", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 2.625000, false, true, true}, + {"Nexus 5X landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 2.625000, true, true, true}, + {"Nexus 6", "Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 3.500000, false, true, true}, + {"Nexus 6 landscape", "Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 3.500000, true, true, true}, + {"Nexus 6P", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 3.500000, false, true, true}, + {"Nexus 6P landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 3.500000, true, true, true}, + {"Nexus 7", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 600, 960, 2.000000, false, true, true}, + {"Nexus 7 landscape", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 960, 600, 2.000000, true, true, true}, + {"Nokia Lumia 520", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)", 320, 533, 1.500000, false, true, true}, + {"Nokia Lumia 520 landscape", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)", 533, 320, 1.500000, true, true, true}, + {"Nokia N9", "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13", 480, 854, 1.000000, false, true, true}, + {"Nokia N9 landscape", "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13", 854, 480, 1.000000, true, true, true}, + {"Pixel 2", "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 411, 731, 2.625000, false, true, true}, + {"Pixel 2 landscape", "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 731, 411, 2.625000, true, true, true}, + {"Pixel 2 XL", "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 411, 823, 3.500000, false, true, true}, + {"Pixel 2 XL landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 823, 411, 3.500000, true, true, true}, } diff --git a/device/gen.go b/device/gen.go index 8d291547..e2786aa0 100644 --- a/device/gen.go +++ b/device/gen.go @@ -18,7 +18,7 @@ import ( const deviceDescriptorsURL = "https://raw.githubusercontent.com/GoogleChrome/puppeteer/master/lib/DeviceDescriptors.js" -var flagOut = flag.String("out", "params.go", "out") +var flagOut = flag.String("out", "device.go", "out") func main() { flag.Parse() @@ -49,24 +49,28 @@ func run() error { return err } - buf := new(bytes.Buffer) - fmt.Fprintf(buf, "package device\n\n") - fmt.Fprintf(buf, "// Generated by gen.go. DO NOT EDIT.\n\n") - fmt.Fprintf(buf, "// See: %s\n\n", deviceDescriptorsURL) + // add reset device + descriptors = append([]deviceDescriptor{deviceDescriptor{}}, descriptors...) - fmt.Fprintf(buf, "// Devices.\n") - fmt.Fprintf(buf, "const (\n") - fmt.Fprintf(buf, "_ Device = iota // make the zero value invalid\n\n") - for _, d := range descriptors { - name := cleanRE.ReplaceAllString(d.Name, "") - name = strings.ToUpper(name[0:1]) + name[1:] - fmt.Fprintf(buf, "// %s is the %q device.\n", name, d.Name) - fmt.Fprintf(buf, "%s\n\n", name) + buf := new(bytes.Buffer) + fmt.Fprintf(buf, hdr, deviceDescriptorsURL) + fmt.Fprintln(buf, "\n// Devices.") + fmt.Fprintln(buf, "const (") + for i, d := range descriptors { + if i == 0 { + fmt.Fprintln(buf, "// Reset is the reset device.") + fmt.Fprintln(buf, "Reset deviceType = iota\n") + } else { + name := cleanRE.ReplaceAllString(d.Name, "") + name = strings.ToUpper(name[0:1]) + name[1:] + fmt.Fprintf(buf, "// %s is the %q device.\n", name, d.Name) + fmt.Fprintf(buf, "%s\n\n", name) + } } - fmt.Fprintf(buf, ")\n\n") + fmt.Fprintln(buf, ")\n") - fmt.Fprintf(buf, "// devices is the list of devices.\n") - fmt.Fprintf(buf, "var devices = [...]device{\n") + fmt.Fprintln(buf, "// devices is the list of devices.") + fmt.Fprintln(buf, "var devices = [...]Device{") for _, d := range descriptors { fmt.Fprintf(buf, "{%q, %q, %d, %d, %f, %t, %t, %t},\n", d.Name, d.UserAgent, @@ -74,12 +78,12 @@ func run() error { d.Viewport.IsLandscape, d.Viewport.IsMobile, d.Viewport.HasTouch, ) } - fmt.Fprintf(buf, "}\n") + fmt.Fprintln(buf, "}") + // write and format if err := ioutil.WriteFile(*flagOut, buf.Bytes(), 0644); err != nil { return err } - return exec.Command("gofmt", "-w", "-s", *flagOut).Run() } @@ -126,6 +130,12 @@ func get(v interface{}) error { return json.Unmarshal(buf, v) } -const tpl = `package device +const ( + hdr = `package device + +// See: %s + +// Generated by gen.go. DO NOT EDIT. ` +) diff --git a/device/params.go b/device/params.go deleted file mode 100644 index 5c7ab257..00000000 --- a/device/params.go +++ /dev/null @@ -1,290 +0,0 @@ -package device - -// Generated by gen.go. DO NOT EDIT. - -// See: https://raw.githubusercontent.com/GoogleChrome/puppeteer/master/lib/DeviceDescriptors.js - -// Devices. -const ( - _ Device = iota // make the zero value invalid - - // BlackberryPlayBook is the "Blackberry PlayBook" device. - BlackberryPlayBook - - // BlackberryPlayBooklandscape is the "Blackberry PlayBook landscape" device. - BlackberryPlayBooklandscape - - // BlackBerryZ30 is the "BlackBerry Z30" device. - BlackBerryZ30 - - // BlackBerryZ30landscape is the "BlackBerry Z30 landscape" device. - BlackBerryZ30landscape - - // GalaxyNote3 is the "Galaxy Note 3" device. - GalaxyNote3 - - // GalaxyNote3landscape is the "Galaxy Note 3 landscape" device. - GalaxyNote3landscape - - // GalaxyNoteII is the "Galaxy Note II" device. - GalaxyNoteII - - // GalaxyNoteIIlandscape is the "Galaxy Note II landscape" device. - GalaxyNoteIIlandscape - - // GalaxySIII is the "Galaxy S III" device. - GalaxySIII - - // GalaxySIIIlandscape is the "Galaxy S III landscape" device. - GalaxySIIIlandscape - - // GalaxyS5 is the "Galaxy S5" device. - GalaxyS5 - - // GalaxyS5landscape is the "Galaxy S5 landscape" device. - GalaxyS5landscape - - // IPad is the "iPad" device. - IPad - - // IPadlandscape is the "iPad landscape" device. - IPadlandscape - - // IPadMini is the "iPad Mini" device. - IPadMini - - // IPadMinilandscape is the "iPad Mini landscape" device. - IPadMinilandscape - - // IPadPro is the "iPad Pro" device. - IPadPro - - // IPadProlandscape is the "iPad Pro landscape" device. - IPadProlandscape - - // IPhone4 is the "iPhone 4" device. - IPhone4 - - // IPhone4landscape is the "iPhone 4 landscape" device. - IPhone4landscape - - // IPhone5 is the "iPhone 5" device. - IPhone5 - - // IPhone5landscape is the "iPhone 5 landscape" device. - IPhone5landscape - - // IPhone6 is the "iPhone 6" device. - IPhone6 - - // IPhone6landscape is the "iPhone 6 landscape" device. - IPhone6landscape - - // IPhone6Plus is the "iPhone 6 Plus" device. - IPhone6Plus - - // IPhone6Pluslandscape is the "iPhone 6 Plus landscape" device. - IPhone6Pluslandscape - - // IPhone7 is the "iPhone 7" device. - IPhone7 - - // IPhone7landscape is the "iPhone 7 landscape" device. - IPhone7landscape - - // IPhone7Plus is the "iPhone 7 Plus" device. - IPhone7Plus - - // IPhone7Pluslandscape is the "iPhone 7 Plus landscape" device. - IPhone7Pluslandscape - - // IPhone8 is the "iPhone 8" device. - IPhone8 - - // IPhone8landscape is the "iPhone 8 landscape" device. - IPhone8landscape - - // IPhone8Plus is the "iPhone 8 Plus" device. - IPhone8Plus - - // IPhone8Pluslandscape is the "iPhone 8 Plus landscape" device. - IPhone8Pluslandscape - - // IPhoneSE is the "iPhone SE" device. - IPhoneSE - - // IPhoneSElandscape is the "iPhone SE landscape" device. - IPhoneSElandscape - - // IPhoneX is the "iPhone X" device. - IPhoneX - - // IPhoneXlandscape is the "iPhone X landscape" device. - IPhoneXlandscape - - // JioPhone2 is the "JioPhone 2" device. - JioPhone2 - - // JioPhone2landscape is the "JioPhone 2 landscape" device. - JioPhone2landscape - - // KindleFireHDX is the "Kindle Fire HDX" device. - KindleFireHDX - - // KindleFireHDXlandscape is the "Kindle Fire HDX landscape" device. - KindleFireHDXlandscape - - // LGOptimusL70 is the "LG Optimus L70" device. - LGOptimusL70 - - // LGOptimusL70landscape is the "LG Optimus L70 landscape" device. - LGOptimusL70landscape - - // MicrosoftLumia550 is the "Microsoft Lumia 550" device. - MicrosoftLumia550 - - // MicrosoftLumia950 is the "Microsoft Lumia 950" device. - MicrosoftLumia950 - - // MicrosoftLumia950landscape is the "Microsoft Lumia 950 landscape" device. - MicrosoftLumia950landscape - - // Nexus10 is the "Nexus 10" device. - Nexus10 - - // Nexus10landscape is the "Nexus 10 landscape" device. - Nexus10landscape - - // Nexus4 is the "Nexus 4" device. - Nexus4 - - // Nexus4landscape is the "Nexus 4 landscape" device. - Nexus4landscape - - // Nexus5 is the "Nexus 5" device. - Nexus5 - - // Nexus5landscape is the "Nexus 5 landscape" device. - Nexus5landscape - - // Nexus5X is the "Nexus 5X" device. - Nexus5X - - // Nexus5Xlandscape is the "Nexus 5X landscape" device. - Nexus5Xlandscape - - // Nexus6 is the "Nexus 6" device. - Nexus6 - - // Nexus6landscape is the "Nexus 6 landscape" device. - Nexus6landscape - - // Nexus6P is the "Nexus 6P" device. - Nexus6P - - // Nexus6Plandscape is the "Nexus 6P landscape" device. - Nexus6Plandscape - - // Nexus7 is the "Nexus 7" device. - Nexus7 - - // Nexus7landscape is the "Nexus 7 landscape" device. - Nexus7landscape - - // NokiaLumia520 is the "Nokia Lumia 520" device. - NokiaLumia520 - - // NokiaLumia520landscape is the "Nokia Lumia 520 landscape" device. - NokiaLumia520landscape - - // NokiaN9 is the "Nokia N9" device. - NokiaN9 - - // NokiaN9landscape is the "Nokia N9 landscape" device. - NokiaN9landscape - - // Pixel2 is the "Pixel 2" device. - Pixel2 - - // Pixel2landscape is the "Pixel 2 landscape" device. - Pixel2landscape - - // Pixel2XL is the "Pixel 2 XL" device. - Pixel2XL - - // Pixel2XLlandscape is the "Pixel 2 XL landscape" device. - Pixel2XLlandscape -) - -// devices is the list of devices. -var devices = [...]device{ - {"Blackberry PlayBook", "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+", 600, 1024, 1.000000, false, true, true}, - {"Blackberry PlayBook landscape", "Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.1.0; en-US) AppleWebKit/536.2+ (KHTML like Gecko) Version/7.2.1.0 Safari/536.2+", 1024, 600, 1.000000, true, true, true}, - {"BlackBerry Z30", "Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+", 360, 640, 2.000000, false, true, true}, - {"BlackBerry Z30 landscape", "Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+", 640, 360, 2.000000, true, true, true}, - {"Galaxy Note 3", "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 3.000000, false, true, true}, - {"Galaxy Note 3 landscape", "Mozilla/5.0 (Linux; U; Android 4.3; en-us; SM-N900T Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 3.000000, true, true, true}, - {"Galaxy Note II", "Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 2.000000, false, true, true}, - {"Galaxy Note II landscape", "Mozilla/5.0 (Linux; U; Android 4.1; en-us; GT-N7100 Build/JRO03C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 2.000000, true, true, true}, - {"Galaxy S III", "Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 360, 640, 2.000000, false, true, true}, - {"Galaxy S III landscape", "Mozilla/5.0 (Linux; U; Android 4.0; en-us; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30", 640, 360, 2.000000, true, true, true}, - {"Galaxy S5", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 360, 640, 3.000000, false, true, true}, - {"Galaxy S5 landscape", "Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 360, 3.000000, true, true, true}, - {"iPad", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 768, 1024, 2.000000, false, true, true}, - {"iPad landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 768, 2.000000, true, true, true}, - {"iPad Mini", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 768, 1024, 2.000000, false, true, true}, - {"iPad Mini landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 768, 2.000000, true, true, true}, - {"iPad Pro", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1024, 1366, 2.000000, false, true, true}, - {"iPad Pro landscape", "Mozilla/5.0 (iPad; CPU OS 11_0 like Mac OS X) AppleWebKit/604.1.34 (KHTML, like Gecko) Version/11.0 Mobile/15A5341f Safari/604.1", 1366, 1024, 2.000000, true, true, true}, - {"iPhone 4", "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 320, 480, 2.000000, false, true, true}, - {"iPhone 4 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53", 480, 320, 2.000000, true, true, true}, - {"iPhone 5", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 320, 568, 2.000000, false, true, true}, - {"iPhone 5 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 568, 320, 2.000000, true, true, true}, - {"iPhone 6", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, - {"iPhone 6 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, - {"iPhone 6 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, - {"iPhone 6 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, - {"iPhone 7", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, - {"iPhone 7 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, - {"iPhone 7 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, - {"iPhone 7 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, - {"iPhone 8", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 667, 2.000000, false, true, true}, - {"iPhone 8 landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 667, 375, 2.000000, true, true, true}, - {"iPhone 8 Plus", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 414, 736, 3.000000, false, true, true}, - {"iPhone 8 Plus landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 736, 414, 3.000000, true, true, true}, - {"iPhone SE", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 320, 568, 2.000000, false, true, true}, - {"iPhone SE landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_1 like Mac OS X) AppleWebKit/603.1.30 (KHTML, like Gecko) Version/10.0 Mobile/14E304 Safari/602.1", 568, 320, 2.000000, true, true, true}, - {"iPhone X", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 375, 812, 3.000000, false, true, true}, - {"iPhone X landscape", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1", 812, 375, 3.000000, true, true, true}, - {"JioPhone 2", "Mozilla/5.0 (Mobile; LYF/F300B/LYF-F300B-001-01-15-130718-i;Android; rv:48.0) Gecko/48.0 Firefox/48.0 KAIOS/2.5", 240, 320, 1.000000, false, true, true}, - {"JioPhone 2 landscape", "Mozilla/5.0 (Mobile; LYF/F300B/LYF-F300B-001-01-15-130718-i;Android; rv:48.0) Gecko/48.0 Firefox/48.0 KAIOS/2.5", 320, 240, 1.000000, true, true, true}, - {"Kindle Fire HDX", "Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true", 800, 1280, 2.000000, false, true, true}, - {"Kindle Fire HDX landscape", "Mozilla/5.0 (Linux; U; en-us; KFAPWI Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Silk/3.13 Safari/535.19 Silk-Accelerated=true", 1280, 800, 2.000000, true, true, true}, - {"LG Optimus L70", "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3765.0 Mobile Safari/537.36", 384, 640, 1.250000, false, true, true}, - {"LG Optimus L70 landscape", "Mozilla/5.0 (Linux; U; Android 4.4.2; en-us; LGMS323 Build/KOT49I.MS32310c) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 384, 1.250000, true, true, true}, - {"Microsoft Lumia 550", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 550) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 640, 360, 2.000000, false, true, true}, - {"Microsoft Lumia 950", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 360, 640, 4.000000, false, true, true}, - {"Microsoft Lumia 950 landscape", "Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; Microsoft; Lumia 950) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Mobile Safari/537.36 Edge/14.14263", 640, 360, 4.000000, true, true, true}, - {"Nexus 10", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 800, 1280, 2.000000, false, true, true}, - {"Nexus 10 landscape", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 10 Build/MOB31T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 1280, 800, 2.000000, true, true, true}, - {"Nexus 4", "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 384, 640, 2.000000, false, true, true}, - {"Nexus 4 landscape", "Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 384, 2.000000, true, true, true}, - {"Nexus 5", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 360, 640, 3.000000, false, true, true}, - {"Nexus 5 landscape", "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 640, 360, 3.000000, true, true, true}, - {"Nexus 5X", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 2.625000, false, true, true}, - {"Nexus 5X landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 5X Build/OPR4.170623.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 2.625000, true, true, true}, - {"Nexus 6", "Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 3.500000, false, true, true}, - {"Nexus 6 landscape", "Mozilla/5.0 (Linux; Android 7.1.1; Nexus 6 Build/N6F26U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 3.500000, true, true, true}, - {"Nexus 6P", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 412, 732, 3.500000, false, true, true}, - {"Nexus 6P landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Nexus 6P Build/OPP3.170518.006) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 732, 412, 3.500000, true, true, true}, - {"Nexus 7", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 600, 960, 2.000000, false, true, true}, - {"Nexus 7 landscape", "Mozilla/5.0 (Linux; Android 6.0.1; Nexus 7 Build/MOB30X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Safari/537.36", 960, 600, 2.000000, true, true, true}, - {"Nokia Lumia 520", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)", 320, 533, 1.500000, false, true, true}, - {"Nokia Lumia 520 landscape", "Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; IEMobile/10.0; ARM; Touch; NOKIA; Lumia 520)", 533, 320, 1.500000, true, true, true}, - {"Nokia N9", "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13", 480, 854, 1.000000, false, true, true}, - {"Nokia N9 landscape", "Mozilla/5.0 (MeeGo; NokiaN9) AppleWebKit/534.13 (KHTML, like Gecko) NokiaBrowser/8.5.0 Mobile Safari/534.13", 854, 480, 1.000000, true, true, true}, - {"Pixel 2", "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 411, 731, 2.625000, false, true, true}, - {"Pixel 2 landscape", "Mozilla/5.0 (Linux; Android 8.0; Pixel 2 Build/OPD3.170816.012) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 731, 411, 2.625000, true, true, true}, - {"Pixel 2 XL", "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 411, 823, 3.500000, false, true, true}, - {"Pixel 2 XL landscape", "Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3765.0 Mobile Safari/537.36", 823, 411, 3.500000, true, true, true}, -} diff --git a/device/types.go b/device/types.go new file mode 100644 index 00000000..a1366f97 --- /dev/null +++ b/device/types.go @@ -0,0 +1,91 @@ +// Package device contains device emulation definitions for use with chromedp's +// Emulate action. +package device + +//go:generate go run gen.go + +import "github.com/chromedp/cdproto/emulation" + +// Device holds device information for use with chromedp.Emulate. +type Device struct { + // Name is the device name. + Name string + + // UserAgent is the device user agent string. + UserAgent string + + // Width is the viewport width. + Width int64 + + // Height is the viewport height. + Height int64 + + // Scale is the device viewport scale factor. + Scale float64 + + // Landscape indicates whether or not the device is in landscape mode or + // not. + Landscape bool + + // Mobile indicates whether it is a mobile device or not. + Mobile bool + + // Touch indicates whether the device has touch enabled. + Touch bool +} + +// String satisfies fmt.Stringer. +func (d Device) String() string { + return d.Name +} + +// UserAgentString satisfies chromedp.Device. +func (d Device) UserAgentString() string { + return d.UserAgent +} + +// EmulateViewportOption satisfies chromedp.Device. +func (d Device) EmulateViewportOption() []func(*emulation.SetDeviceMetricsOverrideParams, *emulation.SetTouchEmulationEnabledParams) { + return []func(*emulation.SetDeviceMetricsOverrideParams, *emulation.SetTouchEmulationEnabledParams){ + func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { + var angle int64 + orientation := emulation.OrientationTypePortraitPrimary + if d.Landscape { + orientation, angle = emulation.OrientationTypeLandscapePrimary, 90 + } + + // force parameters + *p1 = emulation.SetDeviceMetricsOverrideParams{ + Width: d.Width, + Height: d.Height, + DeviceScaleFactor: d.Scale, + ScreenOrientation: &emulation.ScreenOrientation{ + Type: orientation, + Angle: angle, + }, + Mobile: d.Mobile, + } + *p2 = emulation.SetTouchEmulationEnabledParams{ + Enabled: d.Touch, + } + }, + } +} + +// deviceType provides the enumerated device type. +type deviceType int + +// String satisfies fmt.Stringer. +func (d deviceType) String() string { + return devices[d].String() +} + +// UserAgent satisfies chromedp.Device. +func (d deviceType) UserAgentString() string { + return devices[d].UserAgentString() +} + +// EmulateViewportOption satisfies chromedp.Device. +func (d deviceType) EmulateViewportOption() []func(*emulation.SetDeviceMetricsOverrideParams, *emulation.SetTouchEmulationEnabledParams) { + return devices[d].EmulateViewportOption() +} diff --git a/emulate.go b/emulate.go index 2e6875c8..857fa038 100644 --- a/emulate.go +++ b/emulate.go @@ -1,10 +1,19 @@ package chromedp -import "github.com/chromedp/cdproto/emulation" +import ( + "github.com/chromedp/cdproto/emulation" + "github.com/chromedp/chromedp/device" +) // EmulateViewport is an action to change the browser viewport. // // Wraps calls to emulation.SetDeviceMetricsOverride and emulation.SetTouchEmulationEnabled. +// +// Note: this has the effect of setting/forcing the screen orientation to +// landscape, and will disable mobile and touch emulation by default. If this +// is not the desired behavior, use the emulate viewport options +// EmulateOrientation (or EmulateLandscape/EmulatePortrait), EmulateMobile, and +// EmulateTouch, respectively. func EmulateViewport(width, height int64, opts ...EmulateViewportOption) Action { p1 := emulation.SetDeviceMetricsOverride(width, height, 1.0, false) p2 := emulation.SetTouchEmulationEnabled(false) @@ -26,7 +35,7 @@ func EmulateScale(scale float64) EmulateViewportOption { } // EmulateOrientation is an emulate viewport option to set the device viewport -// orientation. +// screen orientation. func EmulateOrientation(orientation emulation.OrientationType, angle int64) EmulateViewportOption { return func(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { p1.ScreenOrientation = &emulation.ScreenOrientation{ @@ -37,13 +46,13 @@ func EmulateOrientation(orientation emulation.OrientationType, angle int64) Emul } // EmulateLandscape is an emulate viewport option to set the device viewport -// orientation in landscape primary mode and an angle of 90. +// screen orientation in landscape primary mode and an angle of 90. func EmulateLandscape(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { EmulateOrientation(emulation.OrientationTypeLandscapePrimary, 90)(p1, p2) } // EmulatePortrait is an emulate viewport option to set the device viewport -// orentation in portrait primary mode and an angle of 0. +// screen orentation in portrait primary mode and an angle of 0. func EmulatePortrait(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.SetTouchEmulationEnabledParams) { EmulateOrientation(emulation.OrientationTypePortraitPrimary, 0)(p1, p2) } @@ -59,23 +68,26 @@ func EmulateTouch(p1 *emulation.SetDeviceMetricsOverrideParams, p2 *emulation.Se p2.Enabled = true } -// ResetViewport is an action to reset the browser viewport. +// ResetViewport is an action to reset the browser viewport to the default +// values the browser was started with. +// +// Note: does not modify / change the browser's emulated User-Agent, if any. func ResetViewport() Action { return EmulateViewport(0, 0, EmulatePortrait) } -// Device is a interface for a known device. +// Device is the shared interface for known device types. // // See: github.com/chromedp/chromedp/device for a set of off-the-shelf devices // and modes. type Device interface { - // UserAgent returns the string to pass to - // emulation.SetUserAgentOverride to emulate this device. - UserAgent() string + // UserAgentString returns the string to pass to + // emulation.SetUserAgentOverride to emulate the device. + UserAgentString() string - // Viewport returns the parameters to pass to EmulateViewport to emulate - // this device. - Viewport() (width, height int64, opts []EmulateViewportOption) + // EmulateViewportOption returns the emulate viewport options used to + // emulate the device. + EmulateViewportOption() []EmulateViewportOption } // Emulate is an action to emulate a specific device. @@ -83,10 +95,17 @@ type Device interface { // See: github.com/chromedp/chromedp/device for a set of off-the-shelf devices // and modes. func Emulate(device Device) Action { - userAgent := device.UserAgent() - width, height, opts := device.Viewport() return Tasks{ - emulation.SetUserAgentOverride(userAgent), - EmulateViewport(width, height, opts...), + emulation.SetUserAgentOverride(device.UserAgentString()), + EmulateViewport(0, 0, device.EmulateViewportOption()...), } } + +// EmulateReset is an action to reset the device emulation. +// +// Resets the browser's viewport, screen orientation, user-agent, and +// mobile/touch emulation settings to the original values the browser was +// started with. +func EmulateReset() Action { + return Emulate(device.Reset) +} diff --git a/emulate_test.go b/emulate_test.go index 0eaf78fa..197beb1b 100644 --- a/emulate_test.go +++ b/emulate_test.go @@ -31,21 +31,3 @@ func TestEmulate(t *testing.T) { t.Errorf("expected size 400x400, got: %dx%d", size.X, size.Y) } } - -func TestEmulateInvalidDevice(t *testing.T) { - t.Parallel() - - var dev device.Device - - want := "Invalid device" - if got := dev.String(); got != want { - t.Fatalf("want %q, got %q", want, got) - } - - defer func() { - if r := recover(); r == nil { - t.Fatalf("expected a panic in Emulate(Device(0))") - } - }() - _ = Emulate(dev) -}