问题
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:
You need to declare a CSP that allows this code as an exception.
You have to explicitly load it over
https://
, the protocol-relative URL//...
fails since the origin haschrome-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