Chrome扩展程序脚本无效

时间:2015-06-03 21:17:03

标签: javascript html google-chrome-extension

我非常擅长为chrome创建扩展,现在我只是想乱搞并创建一个可以打开并绘制的简单画布。

当我将 popup.html 加载为普通网页时,一切似乎都运行正常,但是当我在Chrome中打开扩展程序时,没有任何功能。

清单:

{
"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();
        }
    }
}

在做了一些研究后,我觉得我的js代码格式化方式有问题,但我无法确定这是什么。对不起,很长的帖子和感谢,任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:2)

你不能将javascript代码放在goolge-chrome-extension中的html文件中。

替换

的HTML

<body onload="init()">

使用:

JS

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

有关详细信息,请参阅contentSecurityPolicy

答案 1 :(得分:1)

此外,由于inline JS不被允许,onclick <input type="button" value="Clear" id="clr" size="23" onclick="erase()">应为<{p}}

<input type="button" value="Clear" id="clr" size="23">并使用addEventListener绑定js文件中的事件。

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

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