每当用户点击扩展图标时,我都会编写一个扩展来模仿移动设备。这只是扩展的第一个版本,所以所有的东西都是硬编码的。
我基本上成功地附加到current选项卡,但我不知道为什么我的命令不能像预期的那样工作。我使用Chrome 版本79和协议版本命令是1.3。我目前执行的步骤是:
下面是上述步骤的代码:
function EmulateMobileDevice(tabId) {
var protocolVersion = '1.3';
chrome.debugger.attach({
tabId: tabId
}, protocolVersion, function() {
if (chrome.runtime.lastError) {
alert(chrome.runtime.lastError.message);
return;
}
// Browser.getVersion
chrome.debugger.sendCommand({
tabId: tabId
}, "Browser.getVersion", {}, function(response) {
console.log(response);
});
// Emulation.canEmulate
chrome.debugger.sendCommand({
tabId: tabId
}, "Emulation.canEmulate", {}, function(response) {
console.log(response);
});
// Emulation.clearDeviceMetricsOverride
chrome.debugger.sendCommand({
tabId: tabId
}, "Emulation.clearDeviceMetricsOverride", {}, function(response) {
console.log(response);
// Emulation.setUserAgentOverride
chrome.debugger.sendCommand({
tabId: tabId
}, "Emulation.setUserAgentOverride", {
userAgent: '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',
acceptLanguage: 'en',
platform: 'mobile'
}, function(response) {
console.log(response);
//Emulation.setDeviceMetricsOverride
chrome.debugger.sendCommand({
tabId: tabId
}, "Emulation.setDeviceMetricsOverride", {
width: 0,
height: 0,
deviceScaleFactor: 0,
mobile: true,
screenOrientation: { type: 'portraitPrimary', angle: 1}
}, function(response) {
console.log(response);
// Emulation.setTouchEmulationEnabled
chrome.debugger.sendCommand({
tabId: tabId
}, "Emulation.setTouchEmulationEnabled", {
enabled: true
}, function(response) {
console.log(response);
});
});
});
}); });}我引用了@paulirish实现的相同的想法,但他在协议版本1.1中演示了这里,该版本目前已被否决。我还阅读了来自这里的协议版本1.3的文档。不幸的是,我无法让它工作。
下面是我登录的上述方法的后台脚本的屏幕截图。

非常感谢。
发布于 2022-08-30 06:20:40
我认为您需要在修改DeviceMetricsOverride之后重新加载页面
chrome.tabs.reload(tabId);
发布于 2020-01-10 13:03:18
如果你能简要说明你想用你的代码做些什么,
到目前为止,代码将只显示选项卡,因为当宽度、高度和deviceScaleFactor设置为‘0’(零)时,它会将网站设置为默认大小,但由于您指定了移动的真正默认滚动条,因此会更改(它可能会根据您的css隐藏)。
https://stackoverflow.com/questions/59549696
复制相似问题