Alcune delle funzionalità avanzate offerte dagli strumenti di sviluppatori si può accedere attraverso il chrome.debugger
API (aggiungere l'autorizzazione debugger
al file manifesto) .
L'agente utente può essere modificata utilizzando il comando Network.setUserAgentOverride
:
// Assume: tabId is the ID of the tab whose UA you want to change
// It can be obtained via several APIs, including but not limited to
// chrome.tabs, chrome.pageAction, chrome.browserAction, ...
// 1. Attach the debugger
var protocolVersion = '1.0';
chrome.debugger.attach({
tabId: tabId
}, protocolVersion, function() {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
return;
}
// 2. Debugger attached, now prepare for modifying the UA
chrome.debugger.sendCommand({
tabId: tabId
}, "Network.enable", {}, function(response) {
// Possible response: response.id/response.error
// 3. Change the User Agent string!
chrome.debugger.sendCommand({
tabId: tabId
}, "Network.setUserAgentOverride", {
userAgent: 'Whatever you want'
}, function(response) {
// Possible response: response.id/response.error
// 4. Now detach the debugger (this restores the UA string).
chrome.debugger.detach({tabId: tabId});
});
});
});
La documentazione ufficiale per i protocolli e comandi supportati può essere trovato here. Al momento della scrittura, non c'è documentazione per la modifica delle metriche del dispositivo. Tuttavia, dopo aver scavato nel codice sorgente di Chromium, ho scoperto un file che definisce tutti i comandi attualmente conosciuti:
Quando guardo attraverso l'elenco delle definizioni, trovo lo Page.setDeviceMetricsOverride
. Questa frase sembra corrispondere le nostre aspettative, quindi cerchiamo di cercare ulteriormente, per scoprire come usarlo:
Questo produce "chromium/src/out/Release/obj/gen/devtools/DevTools.js" (migliaia di linee). Da qualche parte, c'è una linea che definisce (abbellito):
InspectorBackend.registerCommand("Page.setDeviceMetricsOverride", [{
"name": "width",
"type": "number",
"optional": false
}, {
"name": "height",
"type": "number",
"optional": false
}, {
"name": "fontScaleFactor",
"type": "number",
"optional": false
}, {
"name": "fitWindow",
"type": "boolean",
"optional": false
}], []);
Come leggere questo? Beh, usare la vostra immaginazione:
chrome.debugger.sendCommand({
tabId: tabId
}, "Page.setDeviceMetricsOverride",{
width: 1000,
height: 1000,
fontScaleFactor: 1,
fitWindow: false
}, function(response) {
// ...
});
Ho provato questo in Chrome 25 con il protocollo versione 1.0, e funziona: la scheda in fase di debug viene ridimensionato. Sìì!
Wow! Questa è una delle risposte più complete e premurose che abbia mai visto o avuto. Grazie! – JJS
Ottima risposta. Tuttavia, quando sto provando il soution ho ricevuto "Errore durante debugger.attach: un altro debugger è già collegato alla scheda con id" o "Errore durante debugger.sendCommand: Debugger non è collegato alla scheda con id: 43". Problemi simili con il tuo codice. – Krasimir
@KrasimirStefanovTsonev Attualmente è possibile collegare solo un'istanza di debugger (sebbene la documentazione indichi che il desiderio finale è quello di consentire più debugger). Probabilmente hai aperto strumenti di sviluppo (F12), un debugger esistente o un debugger remoto nella scheda che stai eseguendo il debug. Chiudi gli strumenti di sviluppo per la scheda specifica e inizierà a funzionare. –