Javascript 如何保持 Google Chrome 扩展弹出窗口打开?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4146930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:06:36  来源:igfitidea点击:

How to keep Google Chrome Extension popup open?

javascriptgoogle-chromegoogle-chrome-extension

提问by jprim

If I open my extension popup then I open another window or tab following the popup does not stay open if I return to it.

如果我打开我的扩展弹出窗口,然后我打开另一个窗口或选项卡,如果我返回它,弹出窗口不会保持打开状态。

Is there a way to force it so the popup stays open?

有没有办法强制它使弹出窗口保持打开状态?

采纳答案by Mohamed Mansour

As a user, you currently cannot force the the popup to stay open. That is a UI decision the UI team made. If you want to want to force a setup, you can have other way to show this by changing the popup icon, open a new tab when it requests, or new popup view for registration.

作为用户,您目前无法强制弹出窗口保持打开状态。这是 UI 团队做出的 UI 决定。如果您想强制进行设置,您可以通过更改弹出图标、在请求时打开新选项卡或用于注册的新弹出视图来通过其他方式来显示此设置。

As a developer, inspect the popup, and it will stay open.

作为开发人员,检查弹出窗口,它将保持打开状态。

回答by Komal Waseem

In an answer to a FAQ here: http://developer.chrome.com/extensions/faq.html#faq-persist-popups

在此处回答常见问题解答:http: //developer.chrome.com/extensions/faq.html#faq-persist-popups

Popups automatically close when the user focuses on some portion of the browser outside of the popup. There is no way to keep the popup open after the user has clicked away.

当用户关注弹出窗口之外的浏览器的某些部分时,弹出窗口会自动关闭。在用户单击离开后,无法保持弹出窗口打开。

回答by Flimm

You cannot stop the Chrome pop-up from closing, unless you're in developer mode. You could consider this alternative, though:

不能关闭停止在Chrome弹出,除非你是在开发模式下。不过,您可以考虑这种替代方案:

Launching a normal pop-up instead:

改为启动普通弹出窗口:

In your popup.htmlfile, load a Javascript file that runs this:

在您的popup.html文件中,加载一个运行以下代码的 Javascript 文件:

var popupWindow = window.open(
    chrome.extension.getURL("normal_popup.html"),
    "exampleName",
    "width=400,height=400"
);
window.close(); // close the Chrome extension pop-up

This will open the file normal_popup.htmlin your extension in a normal pop-up window, which won't close when it loses focus. Because the name parameter is the same, the pop-up window will get reused if the user launches popup.htmlagain.

这将normal_popup.html在普通弹出窗口中打开扩展程序中的文件,该窗口在失去焦点时不会关闭。由于 name 参数相同,如果用户popup.html再次启动,弹出窗口将被重用。

回答by user4162184

If you enable panels at "chrome://flags/#enable-panels" you can use something like:

如果您在“chrome://flags/#enable-panels”启用面板,您可以使用以下内容:

chrome.windows.create({
    url:"popup.html",
    type:"panel",
    width:300,
    height:200
});

to open a panel window instead which will stay on top all the time as long as you don't move it from the bottom of the screen.

打开一个面板窗口,只要您不将它从屏幕底部移动,它就会一直保持在顶部。

回答by GratefulGuest

As others have said, this is a deliberate limitation of popup UI.

正如其他人所说,这是对弹出式 UI 的故意限制。

Instead, you could inject some HTML into the page which loads the content you want in your popup into an element which hovers over the existing page. You will have to implement the close functionality yourself, but it will persist.

相反,您可以将一些 HTML 注入页面,将您想要的弹出窗口中的内容加载到悬停在现有页面上的元素中。您必须自己实现关闭功能,但它会持续存在。

Have a look at e.g. how keyframes.apphas done it: https://github.com/mitchas/Keyframes.app/blob/master/Keyframes.app%20(Extension)/src/inject/ui.js

看看例如如何keyframes.app做到的:https: //github.com/mitchas/Keyframes.app/blob/master/Keyframes.app%20(Extension)/src/inject/ui.js

回答by Derlin

This answerto How do I prevent Chrome developer tools from closing when the current browser window closes?what very helpful in my case:

这个答案如何防止Chrome开发者工具从关闭当前浏览器窗口关闭时?在我的情况下非常有帮助:



Not a perfect solution, but you can add breakpoints on the events Window.close and unload by turning on the checkboxes at:

不是一个完美的解决方案,但您可以通过打开以下位置的复选框在事件 Window.close 和 unload 上添加断点:

Developer tools -> "Sources" tab -> Event Listener Breakpoints -> Window -> close

And

Event Listener Breakpoints -> Load -> unload

Try to mark both and see which one works best for you

尝试标记两者,看看哪一个最适合你

回答by ChromeFreak

Best way to workaround this is to:
- Right click inside the addon popup
- inspect (or CTRK+Shift+I)

解决此问题的最佳方法是:
- 在插件弹出窗口内右键单击
- 检查(或 CTRK+Shift+I)

a new window will open with the inspect... just keep that window and addon popup will never close

检查将打开一个新窗口...只需保留该窗口,插件弹出窗口将永远不会关闭