Get Tab ID of Calling Extension Page / chrome.tabs.close on Self - google-chrome-extension

Is there a way to get the tab ID of the caller? Or is there a way to use chrome.tabs.remove on the calling tab without an id?
Edit: Found how to get tab id: chrome.tabs.getCurrent
However, that also does not bypass the close dialog.

To close itself, an extension page does not need to call Chrome API.
Good old window.close() works.

Related

Chrome API chrome.desktopCapture.chooseDesktopMedia - How to get the id of selected tab?

I am using desktopCapture.chooseDesktopMedia with only ["tab"] option to choose the tab to be streamed. After making the selection, chrome doesn't automatically switch to the selected tab. How can I get the tab ID that was selected so that I can make it active? I only receive a stream ID in the callback.
Good question, but reading the docs seems to suggest you don't get that information in any way, nor can you change the flow to having the user select the tab first and then offering only that tab for capture.
Might be a candidate for a feature request over at https://crbug.com/

Chrome Extension, trigger click on the icon

I searched on Google and StackOverflow, and I was not able to find a solution to my problem (to my greatest surprise).
I'm looking to display the popup, exactly like when the user click on the icon of my extension, but via javascript.
The idea behind it is simple : On a specific page, I inject a button and add an event listener on it ("click"). When the user click on that button, I'd like to display the tooltip, simple as that :)
... but I can not find anything related to it. Any idea ?
Thank you in advance.
Opening the popup is impossible without user interaction. For good reason too, remember that no one likes popups that open themselves. What you can do is inject your popup onto the site the user is at, through a content script.
https://developer.chrome.com/extensions/content_scripts
As per your description,
On a specific page, I inject a button and add an event listener on it ("click"). When the user click on that button, I'd like to display the tooltip, simple as that :)
I think what you need is just chrome.pageAction, it's similar to browserAction, while represents actions that can be taken on the current page, but aren't applicable to all pages.

Chrome Extension "run_at" page close?

What options do you have when using run_at in your extension manifesto?
I'm ideally looking for a content script to be run when a page from a specified website is closing down. Just so I can save how far the user had scrolled down.
Any idea how I might manage this?
You can use the content script to listen to the window onbeforeunload event and add your logic in the callback. This fires when user navigates away from the page. I'm assuming this is what you mean by "closing down".
In addition to the peterdotjs answer, you can use the chrome.tab.onRemoved event

Open and pass data to a popup (new tab) from content script

I'm writing a chrome extension and have a question.
My extension has some .html page in it, let it be 'popup.html'. I inject a content script into some page and this script opens a 'popup.html' in a new tab with something like 'var p = window.open(chrome.extension.getURL('/popup.html'), "popup")', which works perfectly. Next, I need to pass some data to this window and I can't figure how to do it in a simple way.
For some reason I can't call child window's function from a content script with
var p = window.open(chrome.extension.getURL('/popup.html'), "popup");
p.foo(data);
In the console I see Uncaught TypeError: Cannot call method 'foo' of undefined message.
I can't pass data in a query string, because the data is simply too big.
Is there an elegant and simple way to pass data to such kind of window? I thought about messaging, but how do I effectively get tab ID of a newly opened window w/out using a background page?
Thanks a lot in advance.
UPD:
I tried to inverse the logic and get a data from parent window with 'window.opener.foo()' but in a newly opened tab window.opener returns null.
Ok, I found two solutions to my problem.
1) Add a background page, which opens a popup with chrome.tabs.create(). Then send a message from a content script to a background page, which re-sends it to a corresponding tab via chrome.tabs.sendMessage(). It looks a little ugly, but works.
2) A better one, w/out background page. Extension (popup) page creates a listener for long-lived connection. Then content script sends a message to this connection. A problem here is that a listener is not created right after the page is opened, so there should be a mechanism for a content script to wait until popup is loaded. It can be a simple setTimeout or a notification from popup via same long-lived connection.
If anyone has a better solution I'd gladly check it out as well.

switch to chrome extension page without the tabs permission

I'm writing an extension for chrome that shows a results page when a user enters a query from the popup.
I would like subsequent queries to use the existing results page rather than opening new ones.
I used chrome.extension.getViews({'type':'tab'}) to get the currently opened extension pages and to pass the query to a javascript function in the open page.
The issue I'm having is switching to that page without requiring the 'tabs' permission.
This snippet of code works well from the background page.
chrome.tabs.getCurrent(function(tab){
chrome.tabs.update(tab.id, {selected: true});
});
However, tabs.getCurrent requires the 'tabs' permission. Is there a way to do this without the tabs permission?
I could use chrome.tabs.update but I don't know how to get the tab id from the view object returned from chrome.extension.getViews.
I'm fairly certain this can't be done without the tabs permission. You have correctly identified that chrome.tabs.update is the function you need, but without the tabs permission, it can only operate on the currently selected tab by provifing a null tab ID argument. Since you can't supply tab IDs in chrome.tabs.update without permission, switching between tabs is not possible.
/edit: As appsilers mentioned, not really doable.
If you can instead use chrome.tabs.create() to open the required tab, it will also return its tab id upon creation.
Otherwise the only way is to plast a global inside the target context and use chrome.extension.getViews() to identify the target and call a focus-stealing function, like alert(). But I'd advise against, since it's more like a hack and there isn't really a guarantee this functionality will keep stable between builds.

Resources