Where to put Bugsnag scripts for Chrome Extension integration

て烟熏妆下的殇ゞ 提交于 2019-12-12 03:36:42

问题


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!


回答1:


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.



来源:https://stackoverflow.com/questions/36067224/where-to-put-bugsnag-scripts-for-chrome-extension-integration

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