Chrome 扩展脚本不工作

Chrome Extension Script not Working

我对为 chrome 创建扩展非常陌生,现在我只是想弄乱并创建一个简单的 canvas,您可以打开并利用它。

当我将 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 代码格式化方式有问题,但我无法确定那是什么。很抱歉 post,谢谢,感谢您的帮助。

您不能将 javascript 代码放入 goolge-chrome-扩展名的 html 文件中。

替换

html

<body onload="init()">

与:

js

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

有关详细信息,请参阅 contentSecurityPolicy

另外,由于 inline JS 是不允许的,onclick 就像 <input type="button" value="Clear" id="clr" size="23" onclick="erase()"> 应该是

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