diff --git a/background.js b/background.js index 0534202..b272811 100644 --- a/background.js +++ b/background.js @@ -10,11 +10,65 @@ chrome.contextMenus.create({ - title: "打开 v_jstools 配置页面", + title: "打开 v_jstools 动态调试", contexts: ['all'], onclick: function(){ - chrome.tabs.create({ - url: chrome.extension.getURL('options.html') - }); + AttachDebugger(); } -}); \ No newline at end of file +}); +function sendCommand(method, params, source, chainfun){ + chrome.debugger.sendCommand(source, method, params, result => { + if (chrome.runtime.lastError) { + console.error('chrome.runtime.lastError', chrome.runtime.lastError) + } else { + if (chainfun){ + chainfun(result) + } + } + }); +} +chrome.debugger.onEvent.addListener(function (source, method, params){ + switch(method){ + case "Fetch.requestPaused": + console.log("xmFetch","Fetch.requestPaused", params.request.url) + var itheaders = params.responseHeaders; + if (itheaders.find(v => v.name == "Location")) { + sendCommand("Fetch.continueRequest", { requestId: params.requestId, url: itheaders.value }, source); + break; + } + if ((params.responseStatusCode || params.responseErrorReason)) { + if (params.responseErrorReason) { + sendCommand("Fetch.failRequest", { requestId: params.requestId, errorReason: params.responseErrorReason }, source); + break; + } + sendCommand("Fetch.getResponseBody", { requestId: params.requestId }, source, function(result){ + if (result.body !== undefined){ + var rescode = atob(result.body) // 收到的也是 base64 的代码,base64 解一下就是原始代码,对这个代码处理一下后续再传入 body + console.log(rescode) + } + sendCommand("Fetch.fulfillRequest", { + requestId: params.requestId, responseCode: params.responseStatusCode, responseHeaders: params.responseHeaders, + body: result.body, // 这里只能传 base64(指定代码) + }, source); + }); + break; + } + } +}) +var attached = false; +function AttachDebugger() { + if (attached){ return } + attached = true + chrome.tabs.query( + { active: true, currentWindow: true }, + function (tabs) { + var tab = { tabId: tabs[0].id }; + chrome.debugger.attach(tab, "1.2", function () { + console.log('attached debugger.') + sendCommand("Fetch.enable", { patterns: [ + {urlPattern:"*",resourceType:"Script",requestStage:"Response"} // 监听类型,目前主要监听 Script + ] }, tab); + }); + } + ); +} \ No newline at end of file diff --git a/manifest.json b/manifest.json index 620245d..2aff3a5 100644 --- a/manifest.json +++ b/manifest.json @@ -19,23 +19,11 @@ "all_frames": true } ], - "options_page": "options.html", "options_ui": { "chrome_style": false, "open_in_tab": true, "page": "options.html" }, - - // 暂时用不上 - // "commands": { - // "attach-debugger": { - // "suggested_key": { - // "default": "Alt+Shift+D" - // }, - // "description": "Attach" - // } - // }, - "manifest_version": 2 }