获取弹出窗口而非背景窗口的鼠标坐标
我这里有点问题。根据我之前提出的问题,我开发了一个扩展程序,可以记录鼠标单击并标记该位置。
但是,当我单击链接打开弹出窗口时;标记的鼠标单击是在背景窗口上而不是在弹出窗口上。
这是安全问题还是其他问题?
背景.html
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
chrome.tabs.captureVisibleTab(null, {format:"png"}, function(dataUrl){
var img = new Image();
img.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
ctx.arc(request.x, request.y, 5, 0, Math.PI*2, true);
ctx.fillStyle = "rgb(255,0,0)";
ctx.fill();
chrome.tabs.create({url: canvas.toDataURL("image/png")});
};
img.src = dataUrl;
});
sendResponse({});
});
<body>
<canvas id="canvas"></canvas>
</body>
content_script.js :
window.addEventListener("click", function(event) {
chrome.extension.sendRequest({x: event.x, y: event.y});
});
I have a bit of a problem here. Based on my previous question asked I have developed an extension that takes note of the mouse-clicks and marks the place.
However, when I click on a link to open a pop-up; the mouse-clicks that are marked are on the background window and not on the pop-up.
Is this a security issue or something ?
background.html
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
chrome.tabs.captureVisibleTab(null, {format:"png"}, function(dataUrl){
var img = new Image();
img.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
ctx.arc(request.x, request.y, 5, 0, Math.PI*2, true);
ctx.fillStyle = "rgb(255,0,0)";
ctx.fill();
chrome.tabs.create({url: canvas.toDataURL("image/png")});
};
img.src = dataUrl;
});
sendResponse({});
});
<body>
<canvas id="canvas"></canvas>
</body>
content_script.js :
window.addEventListener("click", function(event) {
chrome.extension.sendRequest({x: event.x, y: event.y});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
发生这种情况是因为当前窗口和聚焦窗口是Chrome API中的两个不同的东西。将
null
传递给chrome.tabs.captureVisibleTab()
会获取当前窗口,这对于弹出窗口意味着背景窗口。正如文档中所述:要制作发送请求的窗口的屏幕截图,我们需要明确指定它:
This happens because current window and focused window are two different things in Chrome API. Passing
null
tochrome.tabs.captureVisibleTab()
takes current window, which for popups means a background window. As it says in the docs:To make screenshot of a window that sent the request we need to specify it explicitly:
如果弹出窗口未激活(如图所示),您将无法在后台页面中执行任何操作。假设您遵循这一事实。
确保您的鼠标单击位于该弹出窗口 DOM 内。因此,在弹出窗口中,请执行侦听器操作。
If the popup window is not active (shown), you cannot do anything from it within the background page. Assuming you are following that fact.
Make sure your mouse clicks are within that popup window DOM. So within your popup window, do the listeners.