简体   繁体   English

Chrome扩展程序弹出窗口的非阻止警报

[英]Non blocking alert for a chrome extension popup window

I'm making a chrome extension and for it I need to add in some non blocking alerts to the pop up window. 我正在制作Chrome扩展程序,为此我需要在弹出窗口中添加一些非阻塞警报。 Regular alerts pause the javascript code execution and the client does not want that. 定期警报暂停javascript代码执行,客户端不希望这样。 I tried using jQuery's UI Dialog box but when I click the "OK" button to close it, the popup window loses focus and closes as well. 我尝试使用jQuery的UI对话框但是当我单击“确定”按钮关闭它时,弹出窗口失去焦点并关闭。 Any advice on how to either add persistence to the popup window or on how to create a non blocking alert from the popup? 有关如何在弹出窗口中添加持久性或如何从弹出窗口创建非阻塞警报的任何建议?

UPDATE: The problem is that content.js is the one creating the alert. 更新:问题是content.js是创建警报的人。 So when i click it the popup loses focus and closes. 因此,当我点击它时,弹出窗口失去焦点并关闭。 Is there any way that I can create an alert attached to popup.html instead of to the page loaded in the current tab? 有什么方法可以创建附加到popup.html而不是当前标签中加载的页面的警报吗?

Simply display the dialog in the popup window because it can only display content inside its rigidly limited bounds (maximum 750px or 800px) and you can't "attach" something from outside. 只需在弹出窗口中显示对话框,因为它只能在其严格限制的范围内显示内容(最大750px或800px),并且您无法从外部“附加”某些内容。

  1. Send a message from your content script and wait for the reponse in a listener: 从内容脚本发送消息并等待侦听器中的响应:

     if (someCondition) { chrome.runtime.sendMessage({action: "alert", text: "STOP!"}); } chrome.runtime.onMessage.addListener(function(msg) { if (msg.action == "alert-response") { doSomething(msg.response); } }); 
  2. All open popups receive the message, the active one shows an alert UI and when any of its buttons are clicked a response message is sent to the content script with an id of the button: 所有打开的弹出窗口都会收到消息,活动的弹出窗口会显示一个警报UI,当单击任何按钮时,会向内容脚本发送一个响应消息,其ID为按钮:

     chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) { if (msg.action == "alert") { // if several popups are visible in different windows only one should respond chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { if (tabs[0].id == sender.tab.id) { showAlert(msg.text); } }); } }); function showAlert(text) { // show the nonblocking dialog ................................ btnOK.addEventListener("click", buttonClick); btnCancel.addEventListener("click", buttonClick); function buttonClick(event) { chrome.tabs.query({active: true, currentWindow: true}, function(tabs) { chrome.tabs.sendMessage(tabs[0].id, { action: "alert-response", response: event.target.id // id of the clicked button: "ok", "cancel" } }); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM