Cannot read property 'onConnet' - google-chrome-extension

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.

Related

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"
]
}

Get network data in chrome extension

I have a chrome extenison but i can't figure out how to access the data in the devtools network tab and send to the popup. Any suggestion?
it's basically a bug reporting chrome extension where you can take screenshots, create issue, and I need the network log (and/or console)
manifest.json
{
"manifest_version": 2,
"name": "my-chrome-extension",
"description": "Chrome Extension for report bug",
"version": "1.0",
"background": {
"scripts": [
"js/background.js"
],
"persistent": false
},
"icons": {
"16": "./icon.png",
"36": "./icon.png",
"48": "./icon.png",
"120": "./icon.png"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/vendor.js", "js/content_script.js"
]
}
],
"web_accessible_resources": [
"inject-script.js",
"js/inject-script.js"
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "PerfWatch"
},
"permissions": [
"tabs",
"activeTab",
"storage"
]
}
You cant access it directly. Only via API like webRequest via bg script.
Example:
chrome.webRequest.onBeforeRequest.addListener((details) => {
//This identifies a redirect to another page
if (details.url.indexOf("Target") && details.method === "OPTIONS/..." && details.initiator.indexOf("Source")) {
chrome.tabs.sendMessage(details.tabId, {
message: "xyz"
});
}
})
Your manifest.json must include the webRequest permission in order to access the webRequests:
{
"manifest_version": 2,
"name": "my-chrome-extension",
"description": "Chrome Extension for report bug",
"version": "1.0",
"background": {
"scripts": [
"js/background.js"
],
"persistent": false
},
"icons": {
"16": "./icon.png",
"36": "./icon.png",
"48": "./icon.png",
"120": "./icon.png"
},
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"js/vendor.js", "js/content_script.js"
]
}
],
"web_accessible_resources": [
"inject-script.js",
"js/inject-script.js"
],
"browser_action": {
"default_popup": "popup.html",
"default_title": "PerfWatch"
},
"permissions": [
"tabs",
"activeTab",
"storage",
"webRequest"
]
}
Hint:
There are additional APIs like webNavigation and webRequestBlocking if you need more funtionallity.

chrome tabs event don't work on background js

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"
}

Jquery not running completely in chrome extension

I have written a chrome extension
The manifest file is below
{
"name": "MWE",
"version": "2.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["https://*/*"],
"js": ["jquery.js", "content_script.js"]
} ],
"browser_action": {
"default_icon": "icon.png",
"default_title": "Make this widget bigger"
},
"manifest_version": 2
}
The jquery file that i am using is given below
$(".dijitAccordionTitle.dijitAccordionTitle-selected").parent().siblings().css("display","none")
$('#containerDiv').show().parentsUntil('body').andSelf().siblings().hide();
$("#dijit_layout_AccordionContainer_0").height($(document).height())
I want the jquery to execute after page is loaded successfully.The first line is not executing while it runs successfully in developer tools console.
What could be the issue?
Try wrapping the code in:
document.addEventListener('DOMContentLoaded', function () {
// Your code here
});

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