Chrome扩展脚本不工作
Chrome Extension Script not Working
我对创建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();
});
});
相关文章:
- HTML标记在脚本标记中工作
- 使用谷歌应用程序脚本从工作表中获取值并将其显示在文本框中
- 密码输入键脚本在首次使用后无法工作
- prettyPhoto脚本是't工作
- 在pjax完成其工作时加载脚本
- $(document).ready和jQuery(function($)不工作;jQuery已定义,但脚本尚未定义;t进
- 使用几个<脚本>标签不会't工作-只调用一个脚本
- 重新启动jquery脚本后,角度停止工作
- 我的时间倒计时脚本在谷歌浏览器中工作正常,但在其他浏览器中打开时它显示为南楠
- 当源工作表添加了行时,如何编写在一个工作表中添加行的脚本
- Cufon脚本没有'不能在IE9下工作
- java脚本点击事件处理程序不工作
- 更改谷歌工作表中ImportHTML值的脚本
- 为什么这个书签脚本是't在IE工作
- Javascript函数不工作.脚本定位问题
- 谷歌SWIFFY无法在IE9中工作-脚本错误'运行时.js'
- 从控制台中的工作函数转换为工作脚本.(请参阅编辑)
- jQuery最新的库不工作脚本
- window.close()没有't工作-脚本可能只关闭它打开的窗口
- 链接到外部js文件不工作<脚本src="client.js"