我正在构建一个Chrome扩展程序,因为我试图将弹出窗口从Chrome Extension的背景脚本生成。页面上有链接,在它们悬停的情况下,我需要打开一个带有这些链接的弹出窗口,如果用户徘徊在弹出窗口中已经打开的链接上,那么我不再需要再次打开弹出窗口,我只需要要专注于同一弹出窗口,但是Focus Thing不起作用,而是在命中。这是铬错误吗?
到目前为止我拥有的代码是:
content.js
$(document).on("mouseover", "a", function (event) {
var viewportwidth = document.documentElement.clientWidth;
var viewportheight = document.documentElement.clientHeight;
var data = { url: $(this).attr("href"), clientY: viewportwidth, clientX: viewportheight }
if (data.url.indexOf("://") !== -1) sendMessage(data);
})
function sendMessage(data) {
chrome.runtime.sendMessage(data);
}
背景.js
var win;
var oldUrl ;
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
if(oldUrl !== request.url || win.closed){
oldUrl = request.url ;
win && win.close();
win = window.open(request.url, "mywindow", "menubar=0,tittlebar=0,resizable=1,width=1000,height=650" + ",top=" + request.clientY + ",left=" + request.clientX + 100);
win.onclose = function(){
oldUrl = "";
}
}
else{
alert('hi');
win.focus();
}
});
我有此链接的选项,您可以将弹出窗口从Chrome扩展
这没有太多的弹出窗口选择,这是我的扩展名。
任何帮助将不胜感激
您可以使用chrome.windows.update()
聚焦窗口。但是,为此,您需要要影响的窗口的窗口ID。我没有快速看到从window.open()
返回的窗口获取窗口ID的好方法。因此,我只是选择捕获窗口的ID,该ID是在致电window.open()
之后关注的下一个集中精力。
以下代码使用chrome.windows.update()
将窗口聚焦:
var win;
var oldUrl;
var focusedWinId = null;
chrome.runtime.onMessage.addListener( function (request, sender, sendResponse) {
if(oldUrl !== request.url || win.closed){
oldUrl = request.url ;
win && win.close();
focusedWinId = null; //Flag to remember the next focused window.
win = window.open(request.url, "mywindow", "menubar=0,tittlebar=0,resizable=1,width=1000,height=650" + ",top=" + request.clientY + ",left=" + request.clientX + 100);
win.onclose = function(){
oldUrl = "";
}
} else{
if(focusedWinId !== null){
chrome.windows.update(focusedWinId,{focused:true});
}
}
});
chrome.windows.onFocusChanged.addListener(function(winId){
if(focusedWinId === null & winId > -1){
focusedWinId = winId;
}
});
只需更改窗口的URL而不是关闭和重新打开:
我发现,每当悬停在其他URL的链接时,窗口一直被破坏并创建。因此,我将代码更改为仅在窗口尚未打开时才打开窗口。如果窗口已经打开,则只需更改URL即可。
您的win.onclose
都从未运行,并且没有做任何影响代码的事情。它被检查的唯一位置是oldUrl !== request.url || win.closed
,在这些条件下win.closed
是正确的。因此,我删除了它。如果您需要清除oldUrl
,则需要使用chrome.windows.onRemoved
侦听器。
以下代码将更新弹出窗口的URL,而不是在request.url
与当前显示的窗口不同时重新打开新窗口:
var win;
var oldUrl;
var focusedWinId = null;
var popupTabId = null;
chrome.runtime.onMessage.addListener( function (request, sender, sendResponse) {
//win && console.log('closed:',win.closed);
if(oldUrl !== request.url || win.closed){
oldUrl = request.url ;
if(win && !win.closed){
chrome.tabs.update(popupTabId,{url:request.url});
} else {
focusedWinId = null; //Flag to remember the next focused window.
win = window.open(request.url, "mywindow", "menubar=0,tittlebar=0,resizable=1,width=1000,height=650" + ",top=" + request.clientY + ",left=" + request.clientX + 100);
}
} else{
if(focusedWinId !== null){
chrome.windows.update(focusedWinId,{focused:true});
}
}
});
chrome.windows.onFocusChanged.addListener(function(winId){
if(focusedWinId === null & winId > -1){
focusedWinId = winId;
chrome.windows.get(winId,{populate:true},function(win){
popupTabId = win.tabs[0].id;
});
}
});
使用chrome.windows而不是使用普通窗口?您可以使用chrome.windows.create
功能并指定focused: true
选项。通过这样做,您可以直接从扩展名中打开一个新窗口,您可以轻松地焦点/模糊。