Chrome Extension : generate screenshot - Permission error - google-chrome-extension

I'm trying to develop a chrome extension that will capture screenshots without user intervention and push them to a server. I do not intend to publish this extension, I just want to develop it for me. I would feed the extension with a list of urls to visit on a daily base and then capture screenshots and send them to a server.
I'm already stuck by a permission problem at the very beginning. Here is the code in background.js:
chrome.tabs.create({ url: 'https://www.example.com', active: true }, tab =>
{
if (chrome.runtime.lastError)
{
console.error(chrome.runtime.lastError);
}
else
{
console.log(tab);
chrome.tabs.captureVisibleTab(null, {format: 'png'}, function(dataURI)
{
// do something
});
}
});
When running this code, I get the following error: Unchecked runtime.lastError: Cannot access contents of url "". Extension manifest must request permission to access this host.
In my manifest, I have:
"host_permissions":
[
"tabs",
"*://*/",
"http://*/",
"https://*/",
"<all_urls>",
"notifications",
"webRequest",
"webNavigation",
"management",
"storage",
"webRequestBlocking",
"http://*/*",
"background",
"alarms",
"desktopCapture",
"activeTab",
"https://*/*"
],
"permissions": [
"tabs",
"http://*/",
"https://*/",
"<all_urls>",
"notifications",
"webRequest",
"webNavigation",
"management",
"storage",
"webRequestBlocking",
"http://*/*",
"background",
"alarms",
"desktopCapture",
"activeTab",
"https://*/*"
],
"background": {
"scripts": ["background.js"],
"persistent": true
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["content.js"]
}],
"browser_action": {
"default_icon": "logo.png",
"default_popup": "popup.html"
}
There are multiple discussions about this topic on stackexchange, I have applied recommendations about permissions but it still doesn't work.
Where am I missing something?
Thanks
Laurent

Related

captureVisibleTab is returning undefined [duplicate]

I am new to the extension development
I have tried with permissions url as
"<all_urls>
"*"
"http://*/*", "https://*/*"
None of the pattern is working
Full manifest:
{
"name": "Info",
"description": "BS System Info",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"js": ["script.js"],
"matches": ["http://*/*","https://*/*","<all_urls>"],
"css" : []
}],
"permissions": [
"storage",
"activeTab",
"system.cpu",
"system.memory",
"system.storage",
"system.display",
"tabs",
"scripting",
"http://*/*", "https://*/*", "chrome-devtools://*/*"
],
"action": {
"default_popup": "index.html",
"default_icon": {
"16": "/images/icon_16.png",
"32": "/images/icon_32.png",
"48": "/images/icon_48.png",
"128":"/images/icon_128.png"
}
}
}
Site/URL permissions in ManifestV3 use a separate key: host_permissions
"host_permissions": [
"*://*.example.org/"
],
"permissions": [
"storage"
],
More info in the official migration guide, make sure to study it.

Chrome Extension Manifest v3: Load content script only on click on extension icon (minimize permissions)

I am migrating a functioning browser extension to manifest v3. The problem: I want the content script to be loaded only upon clicking on the browser extension icon. However, the script is always loaded. When I try to upload to the chrome store, I get the following message, which is what I want to avoid:
Because of the host permission, your extension may require an in-depth
review that will delay publishing.
I suspect it has something to do with the "action", but I could not figure out on how to fix this. Here is the manifest:
{
"manifest_version": 3,
"name": "__MSG_extName__",
"description": "__MSG_extDescription__",
"key": "...",
"version": "1.0.0",
"icons": { ... },
"background": {
"service_worker": "/background.js"
},
"permissions": [
"storage"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"/content.js"
]
}
],
"web_accessible_resources": [
{
"resources": [
"/assets/*",
"/options.html"
],
"matches": [
"<all_urls>"
]
}
],
"options_page": "options.html",
"action": {}
}
One last note: I assume that I need activeTab permission. But again, my problem is that I want to minimize the required permissions.
Thanks in advance!
I eventually figured it out. Basically, I had to remove the "content_scripts" section. Instead, I need to inject the content script explicitly with the action handler. I was under the wrong assumption that I could constraint the content_scripts section with the right permissions.
For this to work, I had to set activeTab and scripting permissions, here the new permissions:
"permissions": [
"storage",
"activeTab",
"scripting"
],
I already had an action handler in my service worker (background.js), which now looks like this:
chrome.action.onClicked.addListener(async (tab) => {
await chrome.scripting.executeScript({
target: { tabId: tab.id, allFrames: true },
files: ["content.js"],
});
// Do other stuff...
});
I hope this answer will help someone, somewhere!
From my experience still you can use content_scripts but you should add the permission scripting.
See my manifest json below.
{
"manifest_version": 3,
"name": "*********",
"description": "This extension will repeat the media you are playing",
"version": "1.0",
"background": {
"service_worker": "background.js"
},
"content_scripts":[
{
"matches": ["https://www.google.com/*"],
"js": ["bot.js"]
}
],
"action": {
"default_icon": "icon.png",
"default_popup": "popup/popup.html"
},
"permissions": [
"activeTab",
"storage",
"tabs",
"scripting"
]
}

chrome extention chrome.webRequest no information was obtained [duplicate]

I am new to the extension development
I have tried with permissions url as
"<all_urls>
"*"
"http://*/*", "https://*/*"
None of the pattern is working
Full manifest:
{
"name": "Info",
"description": "BS System Info",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"js": ["script.js"],
"matches": ["http://*/*","https://*/*","<all_urls>"],
"css" : []
}],
"permissions": [
"storage",
"activeTab",
"system.cpu",
"system.memory",
"system.storage",
"system.display",
"tabs",
"scripting",
"http://*/*", "https://*/*", "chrome-devtools://*/*"
],
"action": {
"default_popup": "index.html",
"default_icon": {
"16": "/images/icon_16.png",
"32": "/images/icon_32.png",
"48": "/images/icon_48.png",
"128":"/images/icon_128.png"
}
}
}
Site/URL permissions in ManifestV3 use a separate key: host_permissions
"host_permissions": [
"*://*.example.org/"
],
"permissions": [
"storage"
],
More info in the official migration guide, make sure to study it.

Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "data:text/html,chromewebdata"

I'm getting this error:
extensions::lastError:133 Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "data:text/html,chromewebdata". Extension manifest must request permission to access this host.
I'm getting this error after disabling internet so that I can take action when the page load fails(due to heavy load) or internet down.
I've checked all similar questions and this almost similar but still unable to make it work. Another very similar one with comment that Chrome does not allow hijack of internal pages
My permissions looks like:
"permissions": [
"tabs","unlimitedStorage", "notifications", "history", "activeTab", "storage", "webRequest", "webRequestBlocking", "*://*/*", "http://*/*", "https://*/*"
],
I get the error when I run this code:
chrome.tabs.executeScript(null, {file: "showbacklink.js"});
or
chrome.tabs.executeScript(details.tabId, {file: "showbacklink.js"});
where details.tabId is the active tab.
What am I missing?
Edited manifest.json
{
"name": "",
"options_page": "options.html",
"description": "",
"version": "1.0",
"icons": {
"16": "icons/logo16.png",
"48": "icons/logo48.png",
"128": "icons/logo128.png"
},
"permissions": [
"tabs","unlimitedStorage", "notifications", "history", "activeTab", "storage", "webRequest", "webRequestBlocking", "http://*/*", "https://*/*"
],
"background": {
"scripts": [
"showbacklink.js",
"client_server_common.js",
"common.js",
"background.js"
],
"persistent": true
},
"content_security_policy": "script-src 'self'; object-src 'self'",
"manifest_version": 2,
"content_scripts": [
{
"run_at": "document_end",
"all_frames": true,
"matches": ["https://*/*"],
"css": [//REMOVED],
"js": [ //other files REMOVED
"myscript.js",
]
},
],
"web_accessible_resources": [ //REMOVED
]
}
Indeed, the "Offline" page, or any other error page shown is treated as a Chrome internal page instead of its "original" URL. As such, you can't inject into such pages to change them for security reasons. Imagine for a moment that an extension would be able to interact with SSL warning pages - you really, really don't want that.
If your goal is to provide some sort of alternative error page, you need to hook a listener for such navigation errors and redirect to your own page.
I would recommend looking at webNavigation and webRequest API.

Message passing Background.html -> Content Script

I've got the following in my background.html to sendRequest to contentscript...
chrome.tabs.getSelected(null,function(tab)
{
chrome.tabs.sendRequest(tab.id,{req:"func"});
});
However, it doesn't appear to be working. What am I doing wrong?
Portion of related manifest file...
"background_page": "background.html",
"browser_action":
{
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"notifications",
"contextMenus"
],
"options_page": "options.html",
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["contentScript.js","jquery.js"],
"all_frames": false
}
],
contentscript...
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse)
{
switch(request.req)
{
case "func":
func();
sendResponse({});
break;
default:
sendResponse({});
}
});
Make this in the background page :
chrome.tabs.getSelected(null,function(tab)
{
chrome.tabs.sendRequest(tab.id,{req:"func"}, function(response){
alert(response.req); // Get The response
});
});
And on the Content Script :
sendResponse({ req: 'Your Response' });
Also change your manifest to :
{
"background_page": "background.html",
"browser_action":
{
"default_icon": "icon.png",
"popup": "popup.html"
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"notifications",
"contextMenus"
],
"options_page": "options.html",
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["contentScript.js","jquery.js"],
"all_frames": false
}
],
"name" : "FirstExtension",
"version" : "1.0",
}
PS : Name and Version on the manifest are required...
Your code runs fine for me. Probably a content script is not loaded on that page when you are sending the request. Do you have some sort of condition before chrome.tabs.getSelected? You can't just run it right away, you need to be sure that a tab is loaded first.
PS. Also in your manifest you probably would want to load jquery before your content script.

Resources