在Firefox扩展中,未在chart.js之前加载moment.js [英] moment.js is not loading before chart.js in Firefox extension
问题描述
我正在将Chrome扩展程序移植到Firefox,它会将Chartjs图表添加到页面中.它需要moment.js,并且可以在Chrome上很好地工作.但是,当我将其移植到Firefox时,图表无法加载,并且出现错误:
I am porting a Chrome extension to Firefox and it adds Chartjs charts to a page. It requires moment.js and works great on Chrome. However, when I port it to Firefox, the charts don't load and I get an error:
错误:Chart.js-找不到Moment.js!您必须在Chart.js之前包括它才能使用时间标度.在 https://momentjs.com
Error: Chart.js - Moment.js could not be found! You must include it before Chart.js to use the time scale. Download at https://momentjs.com
我的manifest.json按需要加载的顺序包含了内容脚本,但是在填充图表时,Chartjs似乎无法'发现'瞬间js.
My manifest.json has the content scripts in the order they need to load, but Chartjs can't seem to 'find' moment js when the charts are populated.
"content_scripts": [
{
"matches": [
"<site this should be active on>"
],
"css": [
"main.css"
],
"js": [
"jquery.js",
"moment.js",
"chartjs.js",
"main.js"
]
}
]
我在Google上狩猎和狩猎都没有运气.请帮忙!
I've hunted and hunted on Google to no luck. Please help!
推荐答案
更新:借助Chart.js贡献者,我们已经确认这是Chart.js加载方式的错误内容,请参见此注释及以上以了解详细信息.因此,下面的解决方法可能目前为止还不错.
Update: with help of a Chart.js contributor we've confirmed that it's a bug with Chart.js' way of loading things, see this comment and above for details. So below workaround is probably as good as it gets for now.
我遇到了同样的问题,并且在其上做了一个错误报告Chart.js存储库.我还找到了一种解决方法,该方法是在构建/打包扩展程序时将其添加到moment.min.js
:
I ran into the same issue, and I made a bug report for it on the Chart.js repository. I also found a workaround, which is to add this to moment.min.js
when building/packing your extension:
// Minified momentjs code on this line;
window.moment = moment;
在我的情况下,我使用Powershell将文件转储到build
文件夹中:
In my case I use Powershell to dump files into a build
folder:
Remove-Item build -Recurse -Force -Confirm:$false
mkdir build
Copy-Item node_modules/moment/min/moment.min.js build/
Copy-Item node_modules/chart.js/dist/Chart.js build/
Copy-Item src/js/app.js build/app.js
Copy-Item manifest.json build/manifest.json
# Workaround for: https://stackoverflow.com/questions/51948350/
# See also: https://github.com/chartjs/Chart.js/issues/5901
Add-Content -Path build/moment.min.js -Value "`n`nwindow.moment = moment;"
并不是真正的解决方案或答案,但足以发布我想...
Not really a solution or answer, but useful enough to post I suppose...
这篇关于在Firefox扩展中,未在chart.js之前加载moment.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!