switch to chrome extension page without the tabs permission - google-chrome-extension

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.

Related

event listener when tab is changed

I need to store some data every time I move to some other tab in the same window or some other window using chrome storage API or even when a new tab is created. So, basically when my active tab is no longer active anymore.
So, for example, If I am on tab A and then I move to tab B(or create a new tab). When this switching happens from A to B, I need to save data from the website running in tab A. (I am getting the data from the site in tab A using content script.)
A more concrete example would be, suppose I am on YOutube site and I have a timeout timer running(using a content script). So when I move to some other site I want to stop the time get the current remaining time and save that. and if the other site is youtube too(in tab B) I would start the time from the previously saved value.
Is there any event listener for this? I looked at the documentation for chrome.tabs but could not figure it out.
I saw onActivated event listener but I am not sure if that would be useful.
Or is there some other way to achieve this?
Yes, according to the documentation onActivated:
Fires when the active tab in a window changes.
As an alternative, also can use content scripts that notify your extension when a page gain or loses focus with window.onfocus and window.onblur.
This way you'll be able to track the visibility of the tabs, though you should carefully select the required permission.

Keep alive processing after close the google extension "popup"

When I started create extension, I didn't know that "popup" always reinit after open.
Is it have some options for off this behavior?
"background" page is not good for me.
That is the default behavior, just like closing-opening a tab/window.
Using background page (persistent: true) is a good way, it lives through the entire browser lifetime.
However, if for some reason you are not planning to use background page, I guess chrome.storage or Window.localStorage is also a good way, taking the former for example, you can store data through chrome.storage.local.set while retrieving the data via chrome.storage.local.get every time the popup page opens.

Use a content script to create an element on the page that is invisible to the page

I have a content script in a Chrome extension. I'd like to inject an element into the page, but I need to do so in such a way that the page is unaware of the injected element at all, i.e. it needs to be completely invisible to the page and only visible within the content script context. The page should not be able to locate the element (even with getElementByID), receive events related to it, or anything.
Is such a thing possible? How would I go about doing that? If it's not, what is the closest thing I can do?
If Shadow DOM as mentioned by #Xan doesn't work for you:
The next best thing would depend a lot on what you are trying to do.
A browser action might suit your needs as you could put the data/buttons/etc. you want to display there and the page would not be able to get to it. You could still run a content script to get the data needed from the page.

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

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.

How to control over browser back button

I want a confirmation window on click of a browser back button. If I press yes then the previous page will get load ortherwise I will remain in the same page?
Any suggestion is highly appreciated.. But please be on track.. my question is straight forward
thx in advance..
Why do you want to do that?
You can prevent them from leaving the page by using Javascript, but if you can't use that, it's not possible to do anything about it.
Generally you should use the unload event in the body (in jQuery for instance, just add
jQuery(window).unload(function(evt){
if(!confirm('Do you really want to leave')){
evt.preventDefault();
}});
Prototype have something similar, and for pure Javascript, I guess that it still depends on the browser you're using, but window.unload = function(evt){return false;} might work.
Don't remember the correct syntax for it though.
but I do not know if you can limit that for only the back or if it will trigger for all the unloads (like clicking on a link, closing the browser etc.)
If you want to stop them because they might have unsaved data in a form, then that is ok. If you want to stop them from going back for another reason than that, I think you should rethink why.
Generally if using the back button can cause issues you already have bigger problems.
What you probably want to do is check that you do things like this:
Use POST for all requests that alter data
Use nonce's (unique ID's) to enure forms don't get submitted twice
I use noscript for this very reason. I insist on having control of my browser not the site that I am visiting. I only allow scripts for certain sites. For any site that disables my back button,I don't allow it to run scripts.

Resources