问题描述
有一个带有 的 Google Chrome 扩展程序处理所有标签页上发生的 JS 错误的内容脚本
.但问题是没有任何一种获取错误堆栈跟踪的常用方法不起作用.
There is a Google Chrome extension with content script
that handles JS errors occured on all tabs pages. But the problem is that no one of usual methods of getting errors stack trace does not work.
比如Chrome扩展的content script
中有一段代码:
For example, there is a code in content script
of Chrome extension:
window.addEventListener('error', function(event) {
console.log(event.error.stack); // event.error will be null
}, false);
如果我在网页中调用此代码,那么 event.error
将包含具有 stack
属性的 Error
对象.
If I call this code inside web page, so event.error
will contains Error
object with stack
property.
尝试使用以下方法获取堆栈跟踪的问题:
Same problem with trying to get stack trace using:
console.log((new Error()).stack));
是否有人知道在 Chrome 扩展程序的 content script
中获取错误堆栈跟踪的一些工作问题?
Does anybody knows some working issue to get error stack trace inside content script
of Chrome extension?
错误堆栈跟踪必须以string
或Array
的形式接收,这意味着不仅仅是通过调用console.trace().
Error stack trace must be received as
string
or Array
, means not just like some output in JS console by calling console.trace()
.
如何复制:
下载https://mega.co.nz/#!ENw00ZAC!92LYKyp-PU5YAj0h88-3Q
将
jzen.zip
解压到某个/jsen
文件夹在 Google Chrome 中打开
chrome://extensions
,启用Developer mode
http://i.imgur.com/5x5D6NP.png点击
Load unpacked extension
按钮并选择/jsen
文件夹的路径打开
/jsen/content.js
文件并在window.addEventListener('错误', 函数(e) {
转到 http://xpart.ru/_share/js.htm 并查看结果在 JS 控制台中(Ctrl+Shift+J)
尝试编辑
/jsen/content.js
以获得正确的错误跟踪要重新初始化 Chrome 扩展程序源代码,请点击 http://i.imgur.com/SjFgkHA.png
Download https://mega.co.nz/#!ENw00YAC!92gBZEoLCO9jPsWyKht4dbjYyo0Zk-PU5YAj0h88-3Q
Unpack
jzen.zip
to some/jsen
folderOpen
chrome://extensions
in your Google Chrome, enableDeveloper mode
http://i.imgur.com/5x5D6NP.pngClick
Load unpacked extension
button and select path to/jsen
folderOpen
/jsen/content.js
file and addconsole.log('JSEN', e.error.stack);
insidewindow.addEventListener('error', function(e) {
Go to http://xpart.ru/_share/js.htm and see result in JS console(Ctrl+Shift+J)
Try to edit
/jsen/content.js
to get correct error traceTo reinitialize Chrome extension source code click http://i.imgur.com/SjFgkHA.png
推荐答案
// This code will be injected to run in webpage context
function codeToInject() {
window.addEventListener('error', function(e) {
var error = {
stack: e.error.stack
// Add here any other properties you need, like e.filename, etc...
};
document.dispatchEvent(new CustomEvent('ReportError', {detail:error}));
});
}
document.addEventListener('ReportError', function(e) {
console.log('CONTENT SCRIPT', e.detail.stack);
});
//Inject code
var script = document.createElement('script');
script.textContent = '(' + codeToInject + '())';
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
这篇关于如何在 Chrome 扩展内容脚本中获取错误堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!