Force close chrome extension popup from background script - google-chrome-extension

So I have a background script and I am trying to force close the extension popup programmatically from the background script.
I tried doing
window.close();
Which won't work since that would have to be ran from an extension page not a background script.
Then I tried doing
chrome.tabs.update({ active: true });
which is supposed to switch the focus to the current tab which should then close the extension window, but that does not seem to work either.
Anyone have any ideas?

You can use chrome.extension.getViews to close all opened popups:
chrome.extension.getViews({type: 'popup'}).forEach(v => v.close());
...or just the one in the focused window:
chrome.windows.getLastFocused(w => {
chrome.extension.getViews({type: 'popup', windowId: w.id}).forEach(v => v.close());
});
Alternatively you can use messaging via chrome.runtime.sendMessage to send a message like 'closePopup' so the popup receives it and closes itself using window.close() in its onMessage handler.

Related

How to pass reference to opened popup from content script to chrome extension

In my web app I need to open a popup window via let popup = window.open(url, 'MyPopupName' ..) and then somehow get the reference to that window/tab in a chrome extension.
I cannot use any scripts in the popup because url is from another origin. And it seems like there is no easy way to query all tabs in chrome extension via assigned 'MyPopupName' name (except by injecting a script into all tabs and getting window.name via message passing).
Is there an easy way to pass popup to the extension and get tabId of the popup window?
Since extensions are subject to the same cross-origin JS restrictions, use chrome.windows.create instead of window.open.
async function openPopup(url) {
const wnd = await chrome.windows.create({
url, type: 'popup', width: 500, height: 500,
});
const tabId = wnd.tabs[0].id;
console.log(tabId, wnd);
}
It is available only in extension pages/frames e.g. action popup, options, background script. It means it can't be used in a content script, but you can send a message with the URL and let the background script open it in its onMessage.
Note that wnd is not related to a JavaScript window object, it's an entirely different thing, so you can't access the new window's globals.

How find to whether my Chrome extension was called by shortcut?

I have a Chrome extension with a popup that can also be opened by a shortcut. When the popup gets opened, can I find out whether the user has used the shortcut or whether they have clicked on the extension icon?
The reason is that I'd like to hint users to use the shortcut, but I don't want to show that hint to users who already know and use the shortcut.
Popup and shortcut are defined like this in manifest.json:
"browser_action": {
"default_icon": "images/icon48.png",
"default_popup": "popup.html",
"default_title": "__MSG_tooltip__"
},
"commands": {
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+Space"
}
}
},
chrome.browserAction.onClicked.addListener
Not available, because:
Fired when a browser action icon is clicked. This event will not fire
if the browser action has a popup.
We has popup.
chrome.commands.onCommand.addListener
Not available, because:
The '_execute_browser_action' and '_execute_page_action' commands are
reserved for the action of opening your extension's popups. They won't
normally generate events that you can handle.
May try inject press listener to some page and track pressed of keys (on each page).
var isPressed;
document.body.addEventListener("keydown", function (e) {
if (!(e.keyCode != 17) || !(e.keyCode != 16) || !(e.keyCode != 32)) return;
isPressed = true;
});
From popup in moment expand send message to content_scripts:
chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, {action: "isPressed"}, function (responce) {
if (responce) {
}
});
});
After message receiving, listener in content scripts send variable isPressed as response:
chrome.runtime.onMessage.addListener(function (message, sender, response) {
if (message.action == "isPressed") {
response(isPressed);
}
});
If variable is true, means called via keystrokes, else on click on icon.
can I find out whether the user has used the shortcut or whether they
have clicked on the extension icon?
There seems to be no clean direct way of detecting that.
Since I put some effort into trying to find a workaround, I would like to share what I have considered/tried and reasons why it doesn't work:
Attach the keyboard shortcut to some custom command, modify popup page and then open it programmatically.
Doesn't work because there is no way to open the popup programmatically, at least not in current stable version of Chrome (v50).
Create a custom command with the same shortcut as "_execute_browser_action" and use it to send a message to the popup.
Doesn't work because two commands cannot share the same shortcut.
Try to capture keyup in the popup page immediately after loading.
I tested this and it seemed to work at first, but it is definitely not reliable. If the user only presses the keyboard shortcut very briefly, the keyup event is fired before the popup page gets a chance to register a listener for it.
Capture keydown on pages using a content script and then send a message to the popup page to let it know that the keyboard shortcut was pressed (as suggested by UserName above).
This should work on most pages, but the content script won't be able to capture the keypress in address bar and on certain pages (chrome://, chrome-extension://).
Difficult to implement because you need to take into account the fact that users can customize the shortcut on chrome://extensions page. Finding the currently assigned shortcut programmatically to test against it in the content script is surprisingly difficult, because chrome.commands.getAll() provides localized key names (eg. "Ctrl+Shift+Space" in English, but "Ctrl+Shift+Mezera" in Czech) There are languages where even Ctrl and Shift don't stay in English.

Listen when extension popup page closes in Crossrider

I need to know when the crossrider's extension closes so I can throw some message in the background. It was easy on the other way around
appAPI.ready(function() {
}
This is what I'm using on popup.html to determine if the extension is opened. So what I need is something like appAPI.close(function() {}) but I can't find it on the Crossrider's doc http://docs.crossrider.com/
Thanks,
Kevin
So if you want to monitor "popup page close" event, just listen to unload event for popup page.
window.addEventListener("unload", function() {
// Your logic here
}, false);
Please be aware you can't use console.log in the function, since by the time the unload event fires it is already too late. See this post for more details.

Chrome extension popup resets even after changing its contents

In my chrome extension, I had to change popup html from background. Changes affects then and after clicking again in extension icon, the unchanged popup is showing. Why?
Every time you click away from your popup window, the window is reset. A way to fix this would be to use your background page to store session data, in your popup.js, do something like this:
chrome.runtime.getBackgroundPage(function(bg){
if(bg.myDataHTML){
document.body.innerHTML = bg.myDataHTML;
}
setInterval(function(){
bg.myDataHTML = document.body.innerHTML
},1000);
//do the rest of your work here.
})
I typically do everything in my popup inside that anonymous function to give me access to the libraries defined within my background page.

window.close() in XPages

I am having a Xpage with some links. One of my link call EXIT having window.close() to close the current tab in browser.
In browser when I am opening the document(Xpage holds the document) from the view and clicking on the EXIT link, it closes the current tab/window.
Whereas, I am redirecting the same xpage from SSJS using context.redirectToPage() . When I clicking on the EXIT link, it is not closing the tab/window.
In Javascript console: Scripts may not close windows that were not opened by script
Anyone help me.
Thanks in Advance,
Karthick
As the Javascript console says: Window.close() needs a window.open() to work.
See http://www-10.lotus.com/ldd/ddwiki.nsf/dx/Whats_New_in_852_for_XPages#window.close%28%29+support
Update:
You can create your response document using client-side JS - including opening the window. The following will do that:
// get parent document id
docid='#{javascript:document.getNoteID()}';
// create the URL
url="response.xsp?action=newDocument&parentId="+docid;
// open a new window with the response document
window.open(url);
"In Javascript console: Scripts may not close windows that were not opened by script"
Thats you're answer to the question. Javascript can't close tabs / windows which are not created by javascript.
You could try the following:
How to close browser window
you can try this trick im using.
window.open('', '_self', '');
window.close();

Resources