Chrome extension permissions for localhost not working in manifest.js - google-chrome

I am trying to get localhost permission in my manifest.json so I can use chrome.tabs.executeScript but it is not working. I have tried nearly everything you can think of. Here is an example:
{
"manifest_version": 2,
"name": "extName",
"version": "1",
"description": "extDesc",
"content_scripts": [
{
"matches": [
"http://localhost:3000/*",
"http://127.0.0.1/*",
"http://*/*",
"https://*/*"
],
"css": [
"style.css"
],
"js": [
"contentScript.js"
]
}
],
"background": {
"scripts": [
"background.js"
]
},
"permissions": [
"activeTab",
"tabs"
]
}
But no matter what I try I keep getting
Error during tabs.executeScript: Cannot access contents of url ....
Extension manifest must request permission to access this host.
Actually, I think it's happening also not on localhost
Here is my background.js:
chrome.runtime.onMessage.addListener(
function(message, callback) {
if (message == "runContentScript"){
chrome.tabs.executeScript(null, {
code: 'console.log(window.angular)'
});
}
});

Related

Chrome Extension background.js not receiving message sent from content.js

I want to send data from my inject.js to background.js so that it calls for an external API to store the data into DB.
Here are my sample files
manifest.json
{
"name": "Income Tax Notice Reader",
"description": "Reads Income Tax notices once you login into the IT account",
"version": "1.0",
"manifest_version": 3,
"permissions": [
"alarms",
"notifications",
"activeTab",
"tabs",
"storage",
"scripting"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [{
"exclude_globs": [
"chrome://extensions/"
],
"matches": [
"<all_urls>"
],
"all_frames": true,
"js": ["jquery.min.js","content.js"]
}],
"web_accessible_resources": [{
"resources": ["inject.js","jquery.min.js"],
"matches": [ "<all_urls>" ]
}],
"action": {},
"icons": {
"16": "favicon.ico"
},
"commands":{
"_execute_browser_action": {
"suggested_key": {
"default": "Ctrl+Shift+F",
"mac": "MacCtrl+Shift+F"
}
}
}
}
My content.js file
var script = document.createElement('script');
script.src = chrome.runtime.getURL('jquery.min.js');
(document.head||document.documentElement).appendChild(script);
var script = document.createElement('script');
script.src = chrome.runtime.getURL('inject.js');
(document.head||document.documentElement).appendChild(script);
window.addEventListener("message",function(event){
alert(event.data.message);
chrome.runtime.sendMessage(event.data);
return true;
});
Background.js
chrome.tabs.onUpdated.addListener( function (tabId, changeInfo, tab) {
if (changeInfo.status == 'complete') {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['content.js']
});
}
})
chrome.runtime.onMessage.addListener(function(event){
console.log(event.data.message);
return true;
});
inject.js
window.postMessage({message:"send message to background worker"});
The message is reaching till content.js, but it is not going from content.js to background.js, not sure what I am doing wrong here, tried multiple ways but still failing to understand the issue.

Chrome extension manifest permissions migrating from V2 to V3

Am looking to migrate a manifest V2 extension to V3, but concerned extension might be disabled due to change in manifest permissions. My original plan was to create a new beta extension with existing V2 code and perms, install it from the chrome store locally, then update to manifest V3 perms and see what happens, but chrome store no longer accepts new V2 extensions.
Either the official documentation is purposely vague on the matter of permissions, or I missed something. Other SO questions I see are very specific. Hoping someone with more experience sees this and might be able to spot right away if anything may trigger with the manifest upgrade below. Ideally it's a smooth process given were already requesting a broad match on one of the content script blocks, and the permission list is also reduced.
Current manifest V2 (non pertinent values redacted)
{
"manifest_version": 2,
"name": "...",
"short_name": "...",
"description": "...",
"version": "1.8.0",
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["*://*.example.com/*",
"*://example2.co/*",
"*://*.example2.com/*",
"http://www.example3.com/*", "https://www.example3.com/*"],
"css": [],
"js": ["js/content_script1.min.js"],
"run_at":"document_end"
},
{
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": [
"*://example2.co/*",
"*://*.example2.com/*",
"*://*.example3.com/*"
],
"css": [],
"js": ["js/content_script2.min.js"]
}
],
"browser_action": {
"default_icon": "images/icon.png",
"default_popup": "popup.html",
"default_title": "..."
},
"permissions": [
"activeTab",
"background",
"storage",
"tabs",
"notifications",
"http://*/",
"https://*/"
],
"content_security_policy": "script-src 'self' https://ssl.google-analytics.com https://static.example.com https://api.example.com https://plugin.example.com https://example.com https://s3.us-west-2.amazonaws.com; object-src 'self'",
"externally_connectable": {
"matches": ["*://example.com/*"]
},
"icons": { ... },
"web_accessible_resources": ["/*.png", "/*.svg"]
}
New manifest V3
{
"manifest_version": 3,
"name": "...",
"version": "2.0.0",
"description": "...",
"minimum_chrome_version": "93",
"permissions": [
"storage",
"alarms"
],
"host_permissions": [
"*://*/*"
],
"web_accessible_resources": [
{
"resources": [
"/assets/*"
],
"matches": [
"*://*/*"
]
},
{
"resources": [
"chunks/*-*.js",
"ui/content-script/index.js"
],
"matches": [
"https://*/*",
"http://*/*",
"*://*/*"
]
}
],
"action": {
"default_title": "...",
"default_popup": "ui/popup/index.html",
"default_icon": { ... }
},
"icons": { ... },
"background": {
"service_worker": "service-worker.js",
"type": "module"
},
"content_scripts": [
{
"js": [
"ui/content-script/import-index.js"
],
"matches": [
"https://*/*",
"http://*/*"
],
"run_at": "document_end"
}
],
"externally_connectable": {
"matches": [
"*://example.com/*"
]
}
}

"Cannot access contents of url" Chrome extension error in background.js

I am trying to invoke a current_script from a background script, but I receive the following error:
Unchecked runtime.lastError while running tabs.executeScript: Cannot access contents of url "chrome-devtools://devtools/bundled/inspector.html?
I understand I need to send also tab.id in executeScript function, I also tried that but with no luck.
UPDATE: I changed background.js to the following but content_script.js is still not working:
chrome.commands.onCommand.addListener(function(command) {
if (command === "toggle-feature") {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
for(var i = 0; i<tabs.length;i++) {
alert(tabs.length);
chrome.tabs.executeScript(tabs[i].id, {"file": "content_script.js"});
alert("h");
}
});
}
});
Here is all my code:
Manifest.nmf
{ "manifest_version": 2,
"name": "Extractor",
"version": "1",
"description": "Extract from 144",
"icons": {
"16": "logo16.png",
"48": "logo48.png",
"128": "logo128.png"
},
"page_action": {
"default_icon": {
"16": "logo16.png",
"48": "logo48.png",
"128": "logo128.png"
},
"default_title": "Extractor"
},
"background": {
"scripts": [ "background.js" ],
"persistent": true
},
"content_scripts": [ {
"matches": [ "https://www.msn.com/*" ],
"js": [ "content_script.js" ]
} ],
"permissions": [
"tabs",
"https://www.msn.com/*",
"activeTab",
"*://*/*"
],
"commands": {
"toggle-feature": {
"suggested_key": {
"default": "Ctrl+Shift+1",
"windows": "Ctrl+Shift+2"
},
"description": "Extract now"
}
}
}
Background.js
chrome.commands.onCommand.addListener(function(command) {
if (command === "toggle-feature") {
chrome.tabs.executeScript(null, {file: "content_script.js"} );
}
});
content_script.js
alert("success");
As you see I have also tried to add all links combinations in permissions but with no luck!
How can I solve this problem and get current_script working when pressing a hot key in a tab?

background.js is being triggered every refresh

It seems as if my background.js is triggered every refresh [planted an alert, and i see it every refresh, also my context menu is duplicating its inner self].
this is my manifest file:
"manifest_version": 2,
"name": "test",
"description": "test",
"version": "1.0",
"permissions": [
"activeTab",
"storage",
"tabs",
"contextMenus",
"https://*/*",
"http://*/*",
"https://www.google.com/_/chrome/newtab*"
],
"browser_action": {
"default_icon": "raj_robot.png",
"default_title" : "MemoMi"
},
"chrome_url_overrides" : {
"newtab" : "mypage.html"
},
"background": {
"scripts" : ["background.js"]
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js" : ["selection.js"],
"run_at": "document_end"
}
],
And this is my background script:
function handle_click() {
alert("hi there!");
}
chrome.contextMenus.create({
title: "menu title",
contexts:["selection"],
onclick: handle_click
});
what am I doing wrong here?
Thanks!
Gura
Ok, I found it. I was actually implicitly triggering background.js by using methods in the content scripts, that should have been in the background.js. Just comment out your code and and see if it improves.

Chrome extension access webrequest headers

I'm trying to write simple extension that would act on request headers.
In documentation there is something about chrome.WebRequest, but I have no idea how to make it work....
When I put listener to content page, chrome.WebRequest is undefined, when I put it to background section, totally nothing happens...
manifest
{
"manifest_version": 2,
"name": "RequestSpy",
"version": "0.1",
"description": "HTTP/S Request Analizer",
"background": [
{
"scripts": ["scripts.js"]
}
],
"icons":{
"128":"spy.png"
},
"permissions": [
"*://*/*","webRequest"
]
}
script.js
alert('haha');
chrome.webRequest.onHeadersReceived.addListener(function(details){
console.log(details);
alert('huhu');
});
Any help?
manifest.json
{
"name": "OnRequest",
"version": "1.0",
"description": "I can't has cheezburger!",
"permissions": ["webRequest",
"webRequestBlocking",
"http://*/*",
"https://*/*"],
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2
}
background.js
chrome.webRequest.onHeadersReceived.addListener(function(e){
alert("onHeadersReceived");},{urls: ["http://*/*", "https://*/*"]}, ["blocking"]
);