inline javascript stacktrace: better origin than @<anonymous>

余生颓废 提交于 2019-12-25 01:38:27

问题


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.)


回答1:


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.



来源:https://stackoverflow.com/questions/27553866/inline-javascript-stacktrace-better-origin-than-anonymous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!