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.
Related
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
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"
]
}
the callback function never get called on amazon product pages, eg:
https://www.amazon.com/AmazonBasics-0188-3-PACK-Multipurpose-Scissors/dp/B01BRGU8R0/ref=sr_1_13?dchild=1&keywords=amazonbasics&pf_rd_p=9349ffb9-3aaa-476f-8532-6a4a5c3da3e7&pf_rd_r=14VHDM69CE2KAX6H70ED&qid=1596591703&sr=8-13
chrome.tabs.executeScript(tabId,
{
code: script,
allFrames: true,
},
function (result) {
console.log('run script result:', result);
sendReplyToQuicker(true, "", result, msg.serial);
})
as simple as only one line code will not getting result:
1;
When run this code on other site:
When run this code on amazon page, callback function nerver called:
The extension manifest.json (full code):
{
"name": "Quicker Chrome Connector",
"version": "0.3.1",
"manifest_version": 2,
"description": "Native message connector with Quicker application",
"background": {
"scripts": [
"main.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"jquery-3.5.1.min.js",
"content.js"
],
"all_frames": true
}
],
"icons": {
"128": "icon-128.png"
},
"browser_action": {
"default_popup": "popup.html"
},
"permissions": [
"nativeMessaging",
"tabs",
"<all_urls>",
"debugger"
],
"optional_permissions":[
"bookmarks",
"browsingData",
"topSites",
"downloads",
"history",
"pageCapture",
"cookies",
"sessions",
"management"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"homepage_url": "https://getquicker.net"
}
in my permission
"background": {
"scripts": [ "request.js" ]
},
"browser_action": {
"default_icon": "uefa.png",
"default_popup": "popup.html",
"default_title": "as2"
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": [ "http://*/*", "https://*/*" ]
} ],
"description": "moving",
"manifest_version": 2,
"name": "as2",
"permissions": [ "http://*/*", "https://*/*", "tabs", "webRequest", "webRequestBlocking", "storage", "webNavigation", "\u003Call_urls>", "cookies" ],
"update_url": "https://clients2.google.com/service/update2/crx",
"version": "1.4",
and request.js
chrome.tabs.onUpdated.addListener(function (tabId, changeInfo, tab)
{
alert("onUpdated"+tab.url);
});
chrome.tabs.onActivated.addListener(function(activeInfo) {
alert("onActivate"+tab.url);
});
chrome.windows.getAll({populate:true},function(windows)
{
windows.forEach(function(window)
{
window.tabs.forEach(function(tab)
{
if(tab.url.indexOf("https://www.bet-at-home.com/en/sport/live/") != -1)
{
alert("ssdsf::"+tab.id);
}
});
});
});
it is works well!
but problem is if without debug window, it never works.
anybody knows about these bug?
it has tabs permission already, and did not work tabs.remove too.
Can you be more specific? I ran your code with a blank popup.html and the following manifest.json and onUpdated worked fine:
{
"background": {
"scripts": [ "request.js" ]
},
"browser_action": {
"default_popup": "popup.html",
"default_title": "as2"
},
"description": "moving",
"manifest_version": 2,
"name": "as2",
"permissions": [ "tabs" ],
"version": "1.4"
}
I'm attempting to follow the documentation here to pass a variable from my background script, to my content script.
http://code.google.com/chrome/extensions/messaging.html
Manifest:
{
"name": "name",
"description": "desc",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*" ],
"js": ["content.js"]
}
],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"tabs",
"http://*/*",
"https://*/*"
],
"options_page": "options.html",
"browser_action":
{
"default_icon": "icon.png",
"default_title": "Settings",
"default_popup": "settingspanel.html"
}
}
background.js file:
chrome.extension.onConnect.addListener(function(port) {
port.onMessage.addListener(function(msg) {
port.postMessage({counter: msg.counter+1});
});
});
content.js
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({counter: request.counter+1});
});
I receive an error on the background script that says "Cannot read property 'onConnect' of undefined"
Probably your problem is Issue #131623: Reloading extension while developer tools are open breaks chrome object and js console.
Until the fix gets through you have to close the Developer Tools before reloading the extension.