【发布时间】:2021-01-16 01:48:00
【问题描述】:
所以我正在构建一个简单的 chrome 扩展程序并处理弹出窗口。 当用户安装或激活扩展程序时,应该会出现一个带有说明的弹出窗口,然后用户可以单击三个按钮之一:“完成”、“跳过”、“完成”。
“跳过”按钮有问题:当用户单击它时,它应该“告诉”后台脚本单击了跳过,然后关闭自身(按钮所在的窗口),然后是后台脚本将打开一个新窗口,该窗口与之前的窗口相同,但上面有不同的数据/信息。现在用户可以再次在三个按钮之间进行选择,循环开始。 问题:如果我点击跳过按钮几次(2-3-4x),新窗口将不会打开,有时即使我只点击一次,窗口也会关闭并且不会打开新窗口。
相关代码: background.js(这会初始化弹出窗口并从中接收数据):
window.open("notification.html", currentEx,
"width=300,height=400,status=no,scrollbars=yes,resizable=no");
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
console.log("received " + msg.button);
if(msg.button == "done"){
console.log("done");
}else if(msg.button == "skip"){
randEx = Math.floor((Math.random() * 5));
currentEx = something[randEx];
console.log(currentEx);
// currentEx is the data I want to access from the popup window that's why it is the name of the window
var newWindow = window.open("notification.html", currentEx,
"width=300,height=400,status=no,scrollbars=yes,resizable=no");
}else if(msg.button == "finish"){
console.log("finish");
}
});
notification.js(激活扩展或用户点击跳过时打开的弹出窗口):
var btnSkip = document.createElement("button");
btnSkip.innerHTML = "Skip";
btnSkip.onclick = function(){
chrome.runtime.sendMessage({
button: 'skip'
}, function(){
window.close();
});
};
document.body.appendChild(btnSkip);
我只是不明白为什么弹出窗口不会在 2 次“跳过”或有时更多点击后打开。无论我做了多少次,它都应该关闭和打开,对吧?不知道是哪里出了问题,可能是听错了?
【问题讨论】:
-
我解决了:如果window.name与关闭窗口的名称相同,则javascript不会打开窗口(可能是由于名称重复)。我通过使用 window.custom_field 来传递信息来解决它,并且我使用 Date.now() 作为窗口的名称,因此每次都不同。我不知道我是否应该删除这个问题,因为我认为其他人可能有同样的问题,但如果版主觉得这是一个无用的问题,请随意删除它。我只是不容易找到这种信息,所以我想我会保留这个问题。
-
如果您有解决方案,请将其作为答案提供(即使是针对您自己的问题)并将其标记为已接受。
-
感谢您指出,我会这样做的。
标签: javascript html google-chrome web