How to programmatically open a chrome extension popup window from background.html

痴心易碎 提交于 2019-12-29 11:37:12

问题


Thanks in advance!

What I want to achieve is to open the popup window when a special tag is detected on a webpage by my extension. After searching for a while it seems like the popup can only be opened when user clicks the extension. Is it true?

Or could I get the location of my extension's icon on browser? Maybe I could draw a window and display under the extension icon to make it looks just like the popup window.


回答1:


It is impossible to get the extension window to open without a user clicking on it... However, you can get the extension popup page to open in a new tab as follows:

1) Set your background page in your manifest file...

"background_page": "background.html",

This is where you will run the code to see whether the special tag is detected... background pages can constantly run code in the background, and it is up to you to set a repeat loop to check whether you variable condition is true every so often...

2) Enable tabs permission in your manifest file...

"permissions": [ "tabs" ],

This permission is needed for step 3 (Allows the background page to open a new tab)

3) In background page, call create and point to the extension popup url.

if(condition == true){
  chrome.tabs.create({url:"popup.html"});
}



回答2:


This guy wants opposite effect and can't get his popup window not to open. Take a look at his code to open your popup page ;)




回答3:


You cannot fake click on browser action, but you can still open the popup page from the background script as usual window popup and it will work as expected. So for example the html page of your browser action is "popup.html"

window.open("popup.html", "extension_popup", "width=300,height=400,status=no,scrollbars=yes,resizable=no");

Note: you can call it only from the background script (event page). Therefore you need to register the background script in your manifest file and add an event handler, which would open the popup.You can read more about event pages here https://developer.chrome.com/extensions/event_pages




回答4:


If by popup you mean browser action popup then you are right, there is no way of opening it programmatically.

You can embed whatever you need from your popup on demand directly into a page through a content script. I think this would be the best solution.

If your popup doesn't contain anything fancy, maybe desktop notifications would be enough for you.

Creating a new window and positioning it under the url bar would be pretty awkward solution and not very user friendly.



来源:https://stackoverflow.com/questions/5544256/how-to-programmatically-open-a-chrome-extension-popup-window-from-background-htm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!