Chrome 说我的内容脚本不是 UTF-8 [英] Chrome says my content script isn't UTF-8

查看:50
本文介绍了Chrome 说我的内容脚本不是 UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

收到错误 无法为内容脚本加载文件worker.js".它不是 UTF-8 编码的.

> file -I chrome/worker.js
chrome/worker.js: text/plain; charset=utf-8

使用 to-utf8-unix

> to-utf8-unix chrome/worker.js                                      
chrome/worker.js
----------------
Detected charset:
UTF-8
Confidence of charset detection:
100
Result:
Conversion not needed.
----------------

我也尝试使用 Sublime Text 来回转换文件,但没有任何运气.

I also tried converting the file with Sublime Text back and forth without any luck.

清单:

  "content_scripts": [{
      "matches": ["http://foo.com/*"],
      "js": ["worker.js"]
  }],

相关文件:https://www.dropbox.com/s/kcv23ooh06wlxg3/worker.js?dl=1

这是一个用 cljsbuild 从 clojurescript 中吐出的编译后的 javascript 文件:

It is a compiled javascript file spit out from clojurescript with cljsbuild:

               {:id "chrome-worker"
                :source-paths ["src/chrome/worker"],
                :compiler {:output-to "chrome/worker.js",
                           :optimizations :simple,
                           :pretty-print false}}
               ]}

其他文件(选项页、背景)以同样的方式编译,不会产生这个错误.我试图摆脱像表情符号这样的奇怪字符,但这并没有解决问题.

Other files (options page, background) are compiled the same way and don't generate this error. I tried getting rid of weird characters like Emojis but that didn't fix the problem.

推荐答案

如果你正在使用 Webpack,你可以通过用 Terser 替换默认的 minifier Uglify 来解决它,这不会产生那些编码问题.

In case you are using Webpack you can solve it by replacing the default minifier Uglify with Terser, which won´t produce those encoding issues.

在你的 webpack.conf.js 添加

const TerserPlugin = require('terser-webpack-plugin');

// add this into your config object
optimization: {
    minimize: true,
    minimizer: [
      new TerserPlugin({
        parallel: true,
        terserOptions: {
          ecma: 6,
          output: { 
             ascii_only: true 
          },
        },
      }),
    ],
  },

这篇关于Chrome 说我的内容脚本不是 UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆