简体   繁体   English

在Chrome扩展程序中使用Google Charts

[英]Using Google Charts in Chrome extension

I want to use a chart library in my extension. 我想在我的扩展程序中使用图表库。 Google Charts library seems to be the most convenient one. Google Charts库似乎是最方便的一个。 But I can't use it in the script I inject into the page. 但我不能在我注入页面的脚本中使用它。 My manifest.json looks like this: 我的manifest.json看起来像这样:

...
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"content_scripts": [
  {
    "js": ["loader.js", ...],
    ...
  }
],
...

But I always get these errors: 但我总是得到这些错误:

jsapi_compiled_format_module.js:1 Uncaught ReferenceError: google is not defined
jsapi_compiled_default_module.js:1 Uncaught ReferenceError: google is not defined
jsapi_compiled_ui_module.js:1 Uncaught ReferenceError: google is not defined
jsapi_compiled_corechart_module.js:1 Uncaught ReferenceError: google is not defined

And it is only one line in the script: 并且脚本中只有一行:

google.charts.load('current', {'packages':['corechart']});

So is it any way to make it work? 那么它是否可以使它工作?

loader.js tries to add additional code by inserting <script> tags into the page. loader.js尝试通过在页面中插入<script>标记来添加其他代码。

Due to content script context isolation , those scripts execute in the page's context (and not the content script context). 由于内容脚本上下文隔离 ,这些脚本在页面的上下文中执行(而不是内容脚本上下文)。 Therefore, google becomes defined in the page, but not in your script. 因此, google会在页面中定义,但不会在您的脚本中定义。

It's not clear if this can be worked around at all. 目前尚不清楚这是否可以解决。

It's by far easier to find an alternative library that you can simply include with the extension, for example Chart.js . 更容易找到一个可以简单地包含扩展名的替代库,例如Chart.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM