Adam Howard
Adam Howard

Reputation: 400

Chrome Extension Script not Working

I am extremely new to creating extensions for chrome, and right now I'm just trying to mess around and create a simple canvas that you can open up and draw on.

When I load popup.html as a normal webpage everything seems to work just fine, but when I open the extension in chrome there is no functionality.


Manifest:

{
"manifest_version": 2,

"name": "Sketchpad",
"description": " ",

"version": "0.1",

"browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
 }
}

HTML:

<!doctype html>

<html>
  <head>
    <title>Drawing Pad</title>
    <style>
      body {
        font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
        font-size: 100%;
      }

      .color {display: inline-block;}

      #can {
        cursor: url(pencil.png), auto;
      }
     </style>

     <script src="canvas.js"></script>

     </head>

  <body onload="init()">
    <canvas id="can" width="400" height="400" style="border:2px solid;"></canvas><br>
    <div style="display: inline-block;">Choose Color
        <div class="color" style="width: 10px; height: 10px; background: green;" id="green" onclick="color(this)"></div>
        <div class="color" style="width: 10px; height: 10px; background: blue;" id="blue" onclick="color(this)"></div>
        <div class="color" style="width: 10px; height: 10px; background: red;" id="red" onclick="color(this)"></div>
        <div class="color" style="width: 10px; height: 10px; background: yellow;" id="yellow" onclick="color(this)"></div>
        <div class="color" style="width: 10px; height: 10px; background: orange;" id="orange" onclick="color(this)"></div>
        <div class="color" style="width: 10px; height: 10px; background: black;" id="black" onclick="color(this)"></div>
    </div>
    <div style="display: inline-block;">Eraser
        <div style="width:15px;height:15px;background:white;border:2px solid;display: inline-block" id="white" onclick="color(this)"></div>
    </div><br>

    <input type="button" value="Clear" id="clr" size="23" onclick="erase()">

  </body>
</html>

JS:

var canvas, ctx, flag = false,
    prevX = 0,
    currX = 0,
    prevY = 0,
    currY = 0,
    dot_flag = false;

var x = "black",
    y = 2;

function init() {
    canvas = document.getElementById('can');
    ctx = canvas.getContext("2d");
    w = canvas.width;
    h = canvas.height;

    canvas.addEventListener("mousemove", function (e) {
        findxy('move', e)
    }, false);
    canvas.addEventListener("mousedown", function (e) {
        findxy('down', e)
    }, false);
    canvas.addEventListener("mouseup", function (e) {
        findxy('up', e)
    }, false);
    canvas.addEventListener("mouseout", function (e) {
        findxy('out', e)
    }, false);
}

function color(obj) {
    switch (obj.id) {
        case "green":
            x = "green";
            break;
        case "blue":
            x = "blue";
            break;
        case "red":
            x = "red";
            break;
        case "yellow":
            x = "yellow";
            break;
        case "orange":
            x = "orange";
            break;
        case "black":
            x = "black";
            break;
        case "white":
            x = "white";
            break;
    }
    if (x == "white") y = 14;
    else y = 2;

}

function draw() {
    ctx.beginPath();
    ctx.moveTo(prevX, prevY);
    ctx.lineTo(currX, currY);
    ctx.strokeStyle = x;
    ctx.lineWidth = y;
    ctx.stroke();
    ctx.closePath();
}

function erase() {
    ctx.clearRect(0, 0, w, h);
}

function findxy(res, e) {
    if (res == 'down') {
        prevX = currX;
        prevY = currY;
        currX = e.clientX - canvas.offsetLeft;
        currY = e.clientY - canvas.offsetTop;

        flag = true;
        dot_flag = true;
        if (dot_flag) {
            ctx.beginPath();
            ctx.fillStyle = x;
            ctx.fillRect(currX, currY, 2, 2);
            ctx.closePath();
            dot_flag = false;
        }
    }
    if (res == 'up' || res == "out") {
        flag = false;
    }
    if (res == 'move') {
        if (flag) {
            prevX = currX;
            prevY = currY;
            currX = e.clientX - canvas.offsetLeft;
            currY = e.clientY - canvas.offsetTop;
            draw();
        }
    }
}

After doing some research I have a feeling that something is wrong with the way I have my js code formatted, but I cannot pinpoint what that is. Sorry for the long post and thanks, any help is appreciated.

Upvotes: 0

Views: 105

Answers (2)

Dayton Wang
Dayton Wang

Reputation: 2352

Also since inline JS is not allowed, onclick like <input type="button" value="Clear" id="clr" size="23" onclick="erase()"> should be

<input type="button" value="Clear" id="clr" size="23"> and use addEventListener to bind the event in your js file.

document.addEventListener('DOMContentLoaded', function() {
    var clear = document.getElementById('clr');

    clear.addEventListener('click', function() {
        erase();
    });
});

Upvotes: 1

Elad
Elad

Reputation: 1144

You can't put javascript code in html file in goolge-chrome-extension.

replace

html

<body onload="init()">

with:

js

document.body.addEventListener('load', function () {...})

For more information, see contentSecurityPolicy.

Upvotes: 2

Related Questions