使用调试器命令在仿真模式下启动Chrome,但页面显示为PC版本
问题描述:
在PC平台中,我想将Chrome作为iPhone 5设备模式启动以测试我的项目。使用调试器命令在仿真模式下启动Chrome,但页面显示为PC版本
现在我使用调试器命令Page.setDeviceMetricsOverride当标签更新。 因此,我得到了正确的设备模式,但我的页面显示与PC版本相同,而不是移动版本。我希望我的网页显示与iPhone 5相同。
任何人都可以帮助我吗?
示例代码:
var phonesArray = [
{title: "Apple iPhone 4", width: 320, height: 480, deviceScaleFactor: 2, userAgent: "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5", touch: true, mobile: true},
{title: "Apple iPhone 5", width: 320, height: 568, deviceScaleFactor: 2, userAgent: "Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53", touch: true, mobile: true},
\t ];
var phones = {};
phonesArray.forEach(function(phone){
phones[phone.title.replace(/\s+/gi,'')] = phone;
});
chrome.tabs.onCreated.addListener(function(tab) {
\t console.log("chrome.tabs.onCreated...");
\t if(tab.url.indexOf("chrome://") != 0
\t && tab.url.indexOf("chrome-devtools://") != 0)
\t {
\t \t chrome.debugger.attach({"tabId": tab.id}, "1.0");
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Runtime.enable",{}, function(msg){});
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Page.enable",{}, function(msg){});
\t \t console.log("setDeviceMetricsOverride...");
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Page.setDeviceMetricsOverride", {
\t \t \t width: phones.AppleiPhone5.width,
\t \t \t height: phones.AppleiPhone5.height,
\t \t \t deviceScaleFactor: phones.AppleiPhone5.deviceScaleFactor,
\t \t \t mobile: phones.AppleiPhone5.mobile,
\t \t \t emulateViewport: true,
\t \t \t fitWindow: false
\t \t \t },function(msg){});
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Network.setUserAgentOverride",
\t \t {userAgent:phones.AppleiPhone5.userAgent},function(msg){
\t \t });
\t }
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
\t console.log("chrome.tabs.onUpdated...");
\t console.log(changeInfo);
\t if(changeInfo.status == "loading" && tab.url.indexOf("chrome://") != 0
\t && tab.url.indexOf("chrome-devtools://") != 0) {
\t \t console.log("attach debugger on about:blank. tab.id = " + tab.id);
\t \t chrome.debugger.attach({"tabId": tab.id}, "1.0");
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Runtime.enable",{}, function(msg){});
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Page.enable",{}, function(msg){});
\t \t console.log("setDeviceMetricsOverride...");
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Page.setDeviceMetricsOverride", {
\t \t \t width: phones.AppleiPhone5.width,
\t \t height: phones.AppleiPhone5.height,
\t \t deviceScaleFactor: phones.AppleiPhone5.deviceScaleFactor,
\t \t mobile: phones.AppleiPhone5.mobile,
\t \t emulateViewport: true,
\t \t fitWindow: false
\t \t },function(msg){});
\t \t chrome.debugger.sendCommand({"tabId": tab.id}, "Network.setUserAgentOverride",
\t \t {userAgent:phones.AppleiPhone5.userAgent},function(msg){
\t \t }); \t
\t }
});
答
您需要Network.setUserAgentOverride
之前发送命令Network.enable
,像这样:
chrome.debugger.attach({tabId: tab.id}, "1.0", function(){
if (!chrome.runtime.lastError) {
chrome.debugger.sendCommand({tabId: tab.id}, "Network.enable", {}, function(result) {
chrome.debugger.sendCommand({tabId: tab.id}, "Network.setUserAgentOverride", { userAgent : phones.AppleiPhone5.userAgent }, function(result){
});
});
}
});
虽然它似乎在上班的路上你”已经写了它,你应该等待附加调试器和发送命令后的回调。另外,一旦连接了调试器,无论何时更新选项卡,都不需要重新附加它。