Can't capture screenshot on click, if a new tab opens immediatly - chrome extension

66 views
Skip to first unread message

Abhishek Sachdeva

unread,
Dec 3, 2021, 12:49:04 PM12/3/21
to Chromium Extensions

I am building a chrome extension that captures the screenshot (tab) on clicking, say, a hyperlink. The current tab is not being captured if a new tab opens on click.

Error: Unchecked runtime.lastError: Cannot access contents of url "". Extension manifest must request permission to access this host.

How can I capture the current tab in this scenario (not the new tab)?

For more details, please check this.

Thanks in advance!

hrg...@gmail.com

unread,
Dec 3, 2021, 1:19:58 PM12/3/21
to Chromium Extensions, sachd...@gmail.com
You have to ensure that the tab you want to capture is the active one in its parent window by calling chrome.tabs.update(tabId, {active:true}) first

Abhishek Sachdeva

unread,
Dec 4, 2021, 2:34:56 AM12/4/21
to Chromium Extensions, hrg...@gmail.com, Abhishek Sachdeva
Thanks for replying. I had tried that but the UX is not smooth.
This was the workflow:
1. Click a hyperlink in the current tab (TabA)
2. Focus shifts on new tab (TabB)
3. Use chrome.tabs.update(TabA.id, {active:true}) to make TabA active again. TabA is in focus right now.
4. Once the screenshot is taken, shift focus to TabB using chrome.tabs.update(TabB.id, {active:true})
Steps 3 and 4 look like a flicker. Is there any way to do this without shifting focuses?

hrg...@gmail.com

unread,
Dec 4, 2021, 2:45:51 PM12/4/21
to Chromium Extensions, sachd...@gmail.com, hrg...@gmail.com
The  captureVisibleTab function can only capture the active tab. You have no choice but to deal with that limitation.
For example, cancel the click event so that a new tab is not open, then call captureVisibleTab and finally call .click() on the link element.

Abhishek Sachdeva

unread,
Dec 15, 2021, 12:52:32 AM12/15/21
to hrg...@gmail.com, Chromium Extensions
That is the only way for now it seems. Thanks for the help :)

----
Thanks and Regards
Abhishek Sachdeva

Reply all
Reply to author
Forward
0 new messages