Terry Li
Terry Li

Reputation: 17268

Chrome extension doesn't work

<!--
You are free to copy and use this sample in accordance with the terms of the
Apache license (http://www.apache.org/licenses/LICENSE-2.0.html)
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>
      Google Visualization API Sample
    </title>
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load('visualization', '1', {packages: ['corechart']});
    </script>
    <script type="text/javascript">
      function drawVisualization() {
        // Create and populate the data table.
        var data = google.visualization.arrayToDataTable([
          ['Task', 'Hours per Day'],
          ['Work', 11],
          ['Eat', 2],
          ['Commute', 2],
          ['Watch TV', 2],
          ['Sleep', 7]
        ]);

        // Create and draw the visualization.
        new google.visualization.PieChart(document.getElementById('visualization')).
            draw(data, {title:"So, how was your day?"});
      }


      google.setOnLoadCallback(drawVisualization);
    </script>
  </head>
  <body style="font-family: Arial;border: 0 none;">
    <div id="visualization" style="width: 600px; height: 400px;"></div>
  </body>
</html>

The above code generates a pie chart for us in an html file. It works when I open it in a browser. Let's call the file popup.html.

Then I want to make it a Chrome extension with the following manifest file:

{
  "name": "History Visualizer",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Helps us analyze history stats in a visual way",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  }
}

But when I hit the icon button, the pie chart just won't load. Anyone help me?

EDIT: error message after inspecting the popup is as follows.

Refused to load script from 'http://www.google.com/jsapi' because of Content-Security-Policy. Refused to execute inline script because of Content-Security-Policy.

Upvotes: 1

Views: 4538

Answers (1)

Konrad Dzwinel
Konrad Dzwinel

Reputation: 37923

You need to specify permissions for your extension in manifest.json in order to use resources from google.com:

"permissions": [
  "http://*.google.com/"
],

Or if this won't work, try allowing your extension to access all URLs:

"permissions": [
  "<all_urls>"
],

Read more about permissions here.

Upvotes: 1

Related Questions