Chrome Extension API: capturing specific tab - google-chrome-extension

Right now I developing a product based on Chromium extension which monitors user actions by taking screenshots on several DOM events, such as mouse "click". I using chrome.tabs.captureVisibleTab API for doing that.
The problem: if user makes a click on tab "A" content and then very fast choosing another tab "B" - captureVisibleTab takes screenshot of currently visible tab "B" (instead of tab "A", where action happened).
This is because it take some time while "click" message passing from content script to background (asynchronously) and screenshot taken (also asynchronously) and during that time another tab already activated.
One possible solution I had been thinking is instead of using "captureVisibleTab" - use some another capture API which takes tab id as an argument - but I cannot find such API.
Any ideas?

Related

SharePoint Canvas app - load screen based on content type

Situation: SharePoint list with two content types (CTs) - most fields are common, few are unique/exclusive to specific CTs. Plan is to utilize Power Apps canvas app that overwrites default new-edit-view forms for SharePoint items. Already created new Power App "app" with integration, app and screen components - also new screen for other content type.
Goal: when item form is prompted, open screen "assigned" to specific content type. In other words, if I want to open edit form for item with CT "A" load one screen, but if for item with CT "B" - load other. I am looking for solution that would detect CT and pick screen (form?).
Problem: how to achieve goal? Is it possible to have on-the-fly evaluation that decides which screen to load? Have tried different methods, no luck so far. Most articles describe how form contains control that user can use to make a switch - not the case here, since evaluation should be done before any form is loaded. I have tried introducing "temporary loader" form, but could not achive auto-redirect to desired screen/form.

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/

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.

Selecting Browser Action for Current Tab in Chrome Extension

I'm working on a chrome extension that manipulates certain cookies. Most of the manipulation takes place in the background service, but I need to update the icon and pass data to the browser action for the current tab.
I'm looking for an action similar to the AdBlock extension. AdBlock loads a small number in the bottom right of the icon for the number of ads blocked, so it varies from tab to tab.
When I perform this action from the background service, it seems to change across all browsing tabs. Can someone with experience in extensions point me in the right direction for this one?
This should get you started.
setInterval(function(){//every second
chrome.tabs.getSelected(null,function(tab) {//on the current tab,
chrome.browserAction.getBadgeText({tabId:tab.id}, function(badgeText){//get the tab's badge text
if(badgeText.length<1){
badgeText="0";//set the text if its empty
}
chrome.browserAction.setBadgeText({tabId:tab.id,text:badgeText/1+1+""});//and add one.
});
});
},1000);
make sure you don't run this in the console, because chrome will get the developer tool window id, and since no valid tab has that id, it will change every single tab's badgeText.
You just need to include the tab id when you set it, such as:
chrome.browserAction.setBadgeText({ text: "5", tabId: tab.id })

Hidden tabs in google chrome extensions

I've a chrome extension that sends a message from the content script to the background page and logs the tab_id of the content script.
I noticed that on google.com|de|at two messages are logged thus two content scripts are created: one for the actual web page shown in the tab (e.g. https://www.google.com/search?q=python+standard+library ) and another content script for the first item in the google result list ( in the above example http://docs.python.org/library/ )
Even stranger - the tab_id of the second content script (the hidden one) is not valid. I.e. chrome.pageAction.hide(tab_id) causes the following error to appear:
Error during pageAction.hide: No tab with id: 71
Is there a way to figure out if a content script belongs to a "hidden" tab?
thanks,
Peter
First of all, you can use onCreated and/or onUpdated to keep track of tabs and url mappings without the need for a content script.
However, if there's more to your content script than just informing the background page of the tab id, it may mean more checking.
If your content script is run on all_frames, then you will be getting messages from the content script in the top window and all internal frames. Still, when I test a sample implementation, I get the same IDs for all of them. Also, none of them appear to be from entries in the search result list.
If you are running the script in all tabs, you can ensure that only the top window sends the message by wrapping your sendRequest call with an if (window.top === window).
Could it be possible that you have another extension running that previews google results somehow? That may have this effect....

Resources