Chrome扩展脚本不工作

Chrome Extension Script not Working

本文关键字:工作 脚本 扩展 Chrome      更新时间:2023-09-26

我对创建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代码格式的方式是错误的,但我不能确定那是什么。很抱歉发了这么长时间的帖子,谢谢,任何帮助都很感激。

不能在google -chrome-extension的html文件中放置javascript代码。

代替

<标题> html
<body onload="init()">

:

<标题> js h1> 多信息请参见contentSecurityPolicy

也因为内联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();
    });
});