【问题标题】:moment.js is not loading before chart.js in Firefox extension在 Firefox 扩展中,moment.js 未在 chart.js 之前加载
【发布时间】:2018-08-21 12:09:12
【问题描述】:

我正在将 Chrome 扩展程序移植到 Firefox,并将 Chartjs 图表添加到页面。它需要 moment.js 并且在 Chrome 上运行良好。但是,当我将其移植到 Firefox 时,图表无法加载并且出现错误:

错误:Chart.js - 找不到 Moment.js!您必须在 Chart.js 之前包含它才能使用时间刻度。在https://momentjs.com下载

我的 manifest.json 按需要加载的顺序包含内容脚本,但 Chartjs 在填充图表时似乎无法“找到”时刻 js。

"content_scripts": [
    {
        "matches": [
            "<site this should be active on>"
        ],
        "css": [
            "main.css"
        ],
        "js": [
            "jquery.js",
            "moment.js",
            "chartjs.js",
            "main.js"
        ]
    }
]

我在 Google 上打了又打,但都没有运气。请帮忙!

【问题讨论】:

  • 这可能是 Firefox 中的一个错误;已知在discourse.mozilla.org/t/…bugzilla.mozilla.org/show_bug.cgi?id=1347700 之前发生过
  • 我在看到 OP 的 this question 后登陆了这里。我不认为这是一个 Firefox 错误,因为你会收到一个控制台错误 "Chart.js - Moment.js 无法找到!您必须在之前包含它..." i> - 我没有收到该错误(除非我明确破坏清单内容脚本的顺序)。

标签: javascript firefox-addon chart.js momentjs


【解决方案1】:

更新:在 Chart.js 贡献者的帮助下,我们已确认这是 Chart.js 加载内容方式的错误,请参阅 this comment and above 了解详细信息。所以下面的解决方法可能和现在一样好。


我遇到了同样的问题,我发了bug report for it on the Chart.js repository。我还找到了一种解决方法,即在构建/打包扩展时将其添加到 moment.min.js

// Minified momentjs code on this line;
window.moment = moment;

就我而言,我使用 Powershell 将文件转储到 build 文件夹中:

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;"

不是真正的解决方案或答案,但我想足以发布......

【讨论】:

    猜你喜欢
    • 2012-04-30
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2015-10-25
    • 1970-01-01
    • 2023-03-10
    • 2023-03-30
    相关资源
    最近更新 更多