我正在尝试将rollbar.js实现到火狐扩展。但是,不可能在"rollbar片段“(从cdn加载.js文件)中加载它,这在Firefox插件中是不允许的。
因此,我对其进行了重新处理,并从本地文件加载了rollbar.js。此解决方案适用于Chrome扩展,但是火狐插件中的总是得到"Rollbar是未定义的“。
下面是我在poppup.html...中使用的简化代码
<script>
chrome.storage.local.get(['optins'], function(value) {
var rollbarOptIn = true;
window._rollbarConfig = {
accessToken: "xxx",
captureUncaught: true,
captureUnhandledRejections: true,
enabled: rollbarOptIn,
payload: {
environment: "{{ ENVIRONMENT }}",
client: {
source_map_enabled: true,
code_version: "{{ VERSION }}",
guess_uncaught_frames: true
}
}
};
});
</script>
<script type="text/javascript" src="scripts/rollbar.js" async="false"></script>
<script>
/* ugly workaround with set timeout... */
setTimeout(function(){
...
Rollbar.error("test");
...
}, 600);
</script>我终于..。请帮帮忙。
发布于 2019-11-07 19:00:10
由于chrome.storage是异步的,所以它的回调运行在rollbar.js之后,所以在rollbar中的异步初始化和那个回调之间可能有一个特定于浏览器的竞争条件。
从HTML中删除<script>并尝试动态加载它,然后使用其onload事件:
chrome.storage.local.get('optins', ({optins}) => {
window._rollbarConfig = {
// ...............
};
const el = document.createElement('script');
el.src = 'scripts/rollbar.js';
el.onload = useRollbar;
document.documentElement.appendChild(el);
});
function useRollbar() {
Rollbar.error("test");
// ...............
}https://stackoverflow.com/questions/58754965
复制相似问题