如何将 javascript 模块导入 Firefox WebExtension 后台脚本

问题描述

这是我的清单:

  "background": {
    "page": "./src/background_scripts/background.html"
  },

这是我的 background.html:

    <!DOCTYPE html>
    <html>
        <head>
            <Meta charset="UTF-8">
        </head>
        <body>
<script type="module" src="./ScrapingContent.js"></script>
    
        </body>
    </html>

这是 ScrapingContent.js:

import * as cheerio from "../../node_modules/cheerio/index.js"

但我收到错误

ReferenceError: require is not defined

现在显然我还需要导入 require,所以我将 background.html 更改为:

        <!DOCTYPE html>
        <html>
            <head>
                <Meta charset="UTF-8">
            </head>
            <body>
<script type="text/javascript"src="../../node_modules/requirejs/require.js"></script>
    <script type="module" src="./ScrapingContent.js"></script>
        
            </body>
        </html>

然后我收到错误

Error: Module name "lib/static" has not been loaded yet for context: _. Use require([])
https://requirejs.org/docs/errors.html#notloaded

如何导入这些模块/外部库?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)