Chrome扩展集成的Bugsnag脚本放在哪里 [英] Where to put Bugsnag scripts for Chrome Extension integration

查看:316
本文介绍了Chrome扩展集成的Bugsnag脚本放在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很多有用的信息,但我不明白如何实际加载bugsnag进入扩展。 (例如在后台脚本,内容脚本,background.html页面或其他地方)。你写代码插入脚本标签到页面中?



Bugsnag 设置文档建议配置

 < script src =// d2wy8f7a9ursnm.cloudfront.net /bugsnag-2.min.js
data-apikey =YOUR-API-KEY-HERE>< / script>

我试着将它添加到 background.html 页面,但没有运气。我也尝试将cloudfront脚本添加到扩展中的内容和后台脚本中,但无法加载它。

 未捕获的ReferenceError:Bugsnag没有定义

类似地,放置bugsnag脚本并修改Bugsnag对象的最佳位置(例如

  Bugsnag.beforeNotify = function(error,metaData){
error.stacktrace = error.stacktrace.replace (/ chrome-extension:/ g,chromeextension:);
}

允许扩展错误到达bugsnag,如此处



认识到这是一个非常基本的设置问题,但是在为这个特定用例寻找方向时遇到了一些麻烦。 >解决方案

你试图包含一个外部脚本,这将是棘手的对于扩展页面(背景/弹出窗口),是的,你可以包含它,但是:$ / $> $ / $> $ / $> b
$ b


  1. 您需要声明一个CSP ,它允许这段代码作为例外。

  2. https:// ,协议相对URL // ... 失败,原因是 chrome-extension: //... URL。


对于内容脚本,它会更多复杂。您不能使用标记方法,因为< script> 标记中的任何内容都会转到页面上下文,而不是 content script context 所以你 >需要使用 executeScript 注入它,之前已经使用XHR下载它(不能为 executeScript 但你可以指定一个包含代码的字符串)。显然,您需要使用替代方法设置API密钥






请注意Bugsnag 提及将代码捆绑在本地作为一种可能性:


如果您想避免额外阻止请求,您可以在资产编译过程中包含JavaScript,以便将其内联到您现有的脚本文件中。唯一可以确定的是Bugsnag包含在你的onload处理程序运行之前。


因此,所有上述内容都可以安全地忽略。但是最好让脚本保持最新状态。


I'm working on a Chrome extension for the first time and trying to integrate Bugsnag js error reporting.

I found a lot of useful information, but I don't understand how to actually load bugsnag into the extension. (e.g. in the background script, content script, a background.html page or somewhere else). Do you write code to insert the script tag into the page?

Bugsnag setup docs suggest configuring

<script src="//d2wy8f7a9ursnm.cloudfront.net/bugsnag-2.min.js"
     data-apikey="YOUR-API-KEY-HERE"></script>

I tried adding that to a background.html page, but no luck. I also tried adding the cloudfront script to content and background scripts in the extension, but had trouble loading it.

Uncaught ReferenceError: Bugsnag is not defined

Similarly, where's the best place to put reference to the bugsnag script and modify the Bugsnag object (e.g.

Bugsnag.beforeNotify = function (error, metaData) {
    error.stacktrace = error.stacktrace.replace(/chrome-extension:/g, "chromeextension:");
}

to permit extension errors to reach bugsnag, as suggested here.

Realize it's a pretty basic setup question but have had some trouble finding a direction for this specific use case. Thanks for any suggestion!

解决方案

You're trying to include an external script. That's going to be tough - extensions primarily expect all their resources bundled with them.

For extension pages (background/popup), yes, you can include it, but:

  1. You need to declare a CSP that allows this code as an exception.

  2. You have to explicitly load it over https://, the protocol-relative URL //... fails since the origin has chrome-extension://... URL.

For content scripts, it's going to be more complicated. You can't use the tag method, since anything in a <script> tag goes to the page context, not the content script context.

So you will need to inject it with executeScript, having previously downloaded it with XHR (you can't specify a remote URL for executeScript but you can specify a string containing the code). You'll need to set the API key with the alternative method, obviously.


Do note that Bugsnag mentions bundling the code locally as a possibility:

If you'd like to avoid an extra blocking request, you can include the javascript in your asset compilation process so that it is inlined into your existing script files. The only thing to be sure of is that Bugsnag is included before your onload handlers run.

As such, all of the above can be safely ignored. But it will be up to you to keep the script up to date.

这篇关于Chrome扩展集成的Bugsnag脚本放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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