0

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

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)?

In the example below, dataUrl (screenshot data) will be null if clicked on the first link. The new tab opens quickly, and by the time function is being executed in the background script, the focus is on the new tab (see current tab id).

To reproduce:

index.html

<a id="first" href="https://www.example.org" target="_blank">Open link in new tab</a> <br>
<a id="second" href="https://www.example.org">Open link in same tab</a>

manifest.json

...
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content.js"],
      "css": ["content.css"]
    }
  ],
  "host_permissions": ["<all_urls>","*://localhost:8080/*"],
  "permissions": [
    "activeTab",
    "tabs"
  ],
...

content.js

/////////////// Capture tab on mouse click ///////////////
document.addEventListener("click", (e) => {

  chrome.runtime.sendMessage({ msg: "capture"}, function (response) {

  });
  console.log("inside click eventListener");
}, true);

background.js

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {

  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
        console.log("Current tab id", tabs[0].id);
        console.log("Sender tab id", sender.tab.id);
      });

  chrome.tabs.captureVisibleTab(
    null,
    {
      format: "png",
      quality: 100,
    },
    function (dataUrl) {
      console.log(dataUrl);
    }
  );

  return true;
});

Please let me know if you need additional details. Thanks in advance!

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • This is a bug in Chrome so report it on https://crbug.com. Meanwhile, try using onTabUrlUpdated as [shown here](https://stackoverflow.com/a/67244340). – wOxxOm Jan 25 '22 at 10:51
  • Thanks for the reply. I tried onTabUrlUpdated but it didn't work. I don't want to capture the newly opened tab, I want to capture the tab where the button/link was clicked. A possible (hackish) solution is to wait till the tab has been captured and move to a new tab only after that but this solution will add delay. – Abhishek Sachdeva Jan 27 '22 at 08:54
  • Try making the old tab active (chrome.tabs.update) before calling captureVisibleTab. – wOxxOm Jan 27 '22 at 10:29
  • I've tried that. Made older tab active and focused but UX suffers from this solution. The flow looks like this - Old tab -> Click something -> new tab opens -> focus back to prev tab -> capture screen -> move focus to new tab. It looks like a screen flicker. – Abhishek Sachdeva Jan 27 '22 at 10:56
  • Try using chrome.tabs.onCreated. It may be fast enough to avoid flicker. – wOxxOm Jan 27 '22 at 11:00

0 Answers0