Javascript 如何在使用插件sdk创建的插件中使用canvas drawWindow函数?
我已经使用创建了一个Firefox插件。我正在尝试使用这个函数 我有以下代码来使用该函数,其中ctx指的是画布上下文,我使用Javascript 如何在使用插件sdk创建的插件中使用canvas drawWindow函数?,javascript,firefox,canvas,firefox-addon,firefox-addon-sdk,Javascript,Firefox,Canvas,Firefox Addon,Firefox Addon Sdk,我已经使用创建了一个Firefox插件。我正在尝试使用这个函数 我有以下代码来使用该函数,其中ctx指的是画布上下文,我使用canvas.getContext(“2d”)获得 当我运行此代码时,在使用 tabs.activeTab.attach({ contentScriptFile: data.url("app.js") // app.js contains the above line of code }); 我得到以下错误: TypeError: ctx.drawWindow i
canvas.getContext(“2d”)
获得
当我运行此代码时,在使用
tabs.activeTab.attach({
contentScriptFile: data.url("app.js") // app.js contains the above line of code
});
我得到以下错误:
TypeError: ctx.drawWindow is not a function
在同一个ctx对象上调用strokeRect和fillRect等函数时,不会发生此错误
上面的文档说你需要chrome权限才能使用代码,所以这可能是个问题。根据函数的类型,我预计会出现不同的错误
我发现我可以在我的插件中使用chrome权限,但是接下来我该怎么使用ctx.drawWindow呢
此外,当我从页面上的草稿行而不是加载项运行代码时,而不是“错误:操作不安全”,我得到了相同的“异常:ctx.drawWindow不是函数”
所以,基本上我要问的是,我将如何在使用插件sdk创建的插件中使用canvas drawWindow
编辑:我之所以尝试这样做,是因为我需要一种方法来访问渲染页面的各个像素。我希望将页面绘制到画布上,然后使用访问像素。如果有其他方法访问单个像素(在Firefox插件中),那也会很有帮助。这里有一个代码片段,是从SDK的旧
选项卡浏览器
模块中借用的。这将获得当前选项卡的缩略图,而不附加到选项卡本身
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
var tab = require('sdk/tabs/utils').getActiveTab(window);
var thumbnail = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
thumbnail.mozOpaque = true;
window = tab.linkedBrowser.contentWindow;
thumbnail.width = Math.ceil(window.screen.availWidth / 5.75);
var aspectRatio = 0.5625; // 16:9
thumbnail.height = Math.round(thumbnail.width * aspectRatio);
var ctx = thumbnail.getContext("2d");
var snippetWidth = window.innerWidth * .6;
var scale = thumbnail.width / snippetWidth;
ctx.scale(scale, scale);
ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth, snippetWidth * aspectRatio, "rgb(255,255,255)");
// thumbnail now represents a thumbnail of the tab
console.log(thumbnail.toDataURL());
从这里,您应该能够通过
getImageData
获取数据,如果不想,只需忽略缩放部分即可。只需一个简单的问题,这一行是什么,thumbnail.mozonaque=true代码>,执行?如何在sdk 1.17中执行此操作?使用较新的模块位置对其进行了更新,并在末尾添加了console.log。
var window = require('sdk/window/utils').getMostRecentBrowserWindow();
var tab = require('sdk/tabs/utils').getActiveTab(window);
var thumbnail = window.document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
thumbnail.mozOpaque = true;
window = tab.linkedBrowser.contentWindow;
thumbnail.width = Math.ceil(window.screen.availWidth / 5.75);
var aspectRatio = 0.5625; // 16:9
thumbnail.height = Math.round(thumbnail.width * aspectRatio);
var ctx = thumbnail.getContext("2d");
var snippetWidth = window.innerWidth * .6;
var scale = thumbnail.width / snippetWidth;
ctx.scale(scale, scale);
ctx.drawWindow(window, window.scrollX, window.scrollY, snippetWidth, snippetWidth * aspectRatio, "rgb(255,255,255)");
// thumbnail now represents a thumbnail of the tab
console.log(thumbnail.toDataURL());