使用注入调用chrome.devtools.inspectedWindow.reload会导致Aw Snap [英] Calling chrome.devtools.inspectedWindow.reload with injection causes Aw Snap
问题描述
我正在尝试创建一个工具来监控网页中一些JavaScript API的使用情况。我的计划是重写JavaScript API,将调用信息存储到全局变量中,然后通过postMessage将其发送到内容脚本。但是,将如下代码注入网页会导致出现Aw Snap错误。
chrome.devtools.panels.create (测试开发面板,
icon.png,
panel.html,
函数(面板){
var code = [
窗口.counter = 0;,
Array.prototype.push = function(){,
window.counter ++;,
},
窗口。 addEventListener('message',function(event){,
window.postMessage('Array.push()has called'+ window.counter +'times。','*');,
},false);
;
chrome.devtools.inspectedWindow.reload({
injectScript:code.join('')
});
...
});
这可能是安全限制。但有什么办法实现我的目标?
---更新---
如果我删除postMessage()调用不会发生该错误。
另外,它不会删除Array.prototype覆盖的代码。
在我看来,调用postMessage()并同时覆盖Array的原型会导致错误。
这是Array.prototype.push之后的语法错误。
您需要用分号结尾。
...
var code = [
window.counter = 0;,
Array.prototype.push = function(){,
window.counter ++;,
};,//< - Here
window.addEventListener ('message',function(event){,
window.postMessage('Array.push()'has called'+ window.counter +'times。','*');,
},false);
];
...
I'm trying to create a tool that monitors the usage of some JavaScript APIs in web pages. My plan is to override the JavaScript API's, store the call information into a global variable, and then send it to content script via postMessage. However, injecting code something like below into the web page causes an "Aw Snap" error.
chrome.devtools.panels.create("Test Dev Panel",
"icon.png",
"panel.html",
function (panel) {
var code = [
"window.counter = 0;",
"Array.prototype.push = function () {",
"window.counter++;",
"}",
"window.addEventListener('message', function(event) {",
"window.postMessage('Array.push() has been called ' + window.counter + ' times.', '*');",
"}, false);"
];
chrome.devtools.inspectedWindow.reload({
injectedScript: code.join('')
});
...
});
It might be a security restriction. But is there any way to achieve my goal?
--- Update --- The error doesn't occur if I remove the postMessage() call. Also, it doesn't if I remove the Array.prototype overriding code. It seems to me that calling the postMessage() and overwriting Array's prototype at the same time are causing the error.
It's a syntax error after the Array.prototype.push.
You need to end the line with a semicolon.
...
var code = [
"window.counter = 0;",
"Array.prototype.push = function () {",
"window.counter++;",
"};", //<-- Here
"window.addEventListener('message', function(event) {",
"window.postMessage('Array.push() has been called ' + window.counter + ' times.', '*');",
"}, false);"
];
...
这篇关于使用注入调用chrome.devtools.inspectedWindow.reload会导致Aw Snap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!