Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome screenshot works only when extension is clicked first

So I tried to write some code which would allow me to take a screenshot of the page when a button is pressed on my website. The code works, but the only problem is that I have to click on the chrome extension first, and then I can click on the button for it to take the screenshot. I guess that's because the active tab is not invoked. Any ideas?

This is the error message:

Unchecked runtime.lastError while running tabs.captureVisibleTab: The 'activeTab' permission is not in effect because this extension has not been invoked.

manifest.json

"permissions": [
    "tabs",
    "*://google.com/*"
  ],

background.js

var id = 100;

// Listen for a click on the camera icon. On that click, take a screenshot.
function takeScreenshot() { 

  chrome.tabs.captureVisibleTab(null, function(screenshotUrl) {
    .....
}

chrome.extension.onRequest.addListener(function(request, sender) {
  takeScreenshot();
});

contentscript1.js

contentScriptMessage = "Take a screenshot";

document.addEventListener("hello", function(data) { //When overlay is clicked
    chrome.extension.sendRequest({message: contentScriptMessage}); //call background script
})

And I pass a message from the webpage when the image is clicked (calls the function go()) like so:

  var go = function() {
          var event = document.createEvent('Event');
          event.initEvent('hello');
          document.dispatchEvent(event);
          }
like image 439
user3374309 Avatar asked Sep 21 '14 23:09

user3374309


1 Answers

Unchecked runtime.lastError while running tabs.captureVisibleTab: The 'activeTab' permission is not in effect because this extension has not been invoked.

This error means that your extension does not have sufficient permissions to access the current tab if there was no explicit user gesture (like a click on your extension). I must say the error is quite misleading here, as you don't have activeTab permission in your manifest.

Docs mention that you need specifically the "<all_urls>" permission in order to use this function without explicit invocation. See this bug for an explanation of the requirement.

like image 193
Xan Avatar answered Oct 13 '22 13:10

Xan