我正在寻找如何将文本设置为页面操作图标并找到了这个示例:
window.setInterval(function() {
chrome.pageAction.setIcon({
imageData: draw(10, 0),
tabId: tabId
});
}, 1000);
function draw(starty, startx) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var img = new Image();
img.src = "icon_16.png"
img.onload = function() {
context.drawImage(img, 0, 2);
}
//context.clearRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "rgba(255,0,0,1)";
context.fillRect(startx % 19, starty % 19, 10, 10);
context.fillStyle = "white";
context.font = "11px Arial";
context.fillText("3", 0, 19);
return context.getImageData(0, 0, 19, 19);
}
But after I include it to my eventPage.js
it says Uncaught TypeError: Cannot call method 'getContext' of null
. Then I googled for this error and found that I have to use getContext
only after DOM is loaded. So I wrapped above code into jQuery .ready
function but result was the same.
所以我现在不知道错误在哪里以及我必须以何种方式进行搜索。