内嵌JavaScript堆栈跟踪:比起<匿名>更好的来源 [英] inline javascript stacktrace: better origin than @<anonymous>

查看:87
本文介绍了内嵌JavaScript堆栈跟踪:比起<匿名>更好的来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Chrome扩展程序。为了内容安全的目的,我需要追加Javsacript作为内联脚本。现在的问题是,因为脚本被内联,stacktrace显示@作为源,而不是文件名:

  ReferenceError: asdf未定义
fetchContactByEmail @<匿名>:5439:1

我需要区分堆栈跟踪中调用者的来源,以判断它是否来自我的脚本。有没有办法告诉内联脚本的来源?



(PS我试图解决的问题是:可能有多个扩展在同一页面上注入脚本,并且它们重写了Array.prototype方法,我想设置一个开关来检查调用者是否是我的脚本,然后执行我的函数;如果调用者是其他扩展的脚本,执行它们的函数。)


<在您的代码之前或之后插入以下片段:

  //#sourceURL = name-of-script.js 

现在,只要你的代码抛出一个错误,你会看到name-of-script.js而不是< anonymous> 。此功能是源地图的一部分,您可以在 Chrome devtools文档


I'm working on a Chrome extension. I need to append Javsacript as inline-script for content security purposes. The issue now is that, because script is inlined, the stacktrace shows @ as source, instead of the file name:

ReferenceError: asdf is not defined
fetchContactByEmail@<anonymous>:5439:1

I need to distinguish between the source of the caller in the stack trace, to tell if it's from my script. Is there a way to tell the source of inline script?

(P.S. the problem I'm trying to solve is: there could be multiple extensions injecting script onto the same page, and they override Array.prototype methods. I want to set up a switch that checks if caller is my script, then execute my function; if caller is other extension's script, execute their function.)

解决方案

Insert the following snippet before or after your code:

//# sourceURL=name-of-script.js

Now, whenever your code throws an error, you will see "name-of-script.js" instead of <anonymous>. This feature is a part of Source maps, you can read more about this feature in the Chrome devtools documentation.

这篇关于内嵌JavaScript堆栈跟踪:比起&lt;匿名&gt;更好的来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆