I'm making a chrome extension that would:
Detect a POST request
Send a message to the content script that's running in the tab that made the request
Retrieve some data from the DOM and send it back with sendResponse
But most of the time I'm getting "undefined" as the message sent from sendResponse.
I've made a dummy extension to debug this further, and the odd part is that it only works sometimes. I wasn't able to determine the cause of this behavior after comparing the responseHeaders of the requests.
I've used this for testing, clicking on the "related searches" buttons.
https://www.gamiss.com/wholesale-cheap/buckle-leather-belt/
What am I missing here?
manifest.json
{
"manifest_version": 2,
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"all_frames": true,
"js": ["testContent.js"]
}
],
"permissions": [
"tabs",
"webRequest",
"<all_urls>"
],
"background": {
"scripts": ["testBackground.js"],
"persistent": true
}
}
testBackground.js
chrome.webRequest.onResponseStarted.addListener(function(e){
if(e.type === "main_frame"){
console.log(e);
chrome.tabs.query({
active:true,
currentWindow:true
},function (tabs){
chrome.tabs.sendMessage(tabs[0].id, {message: "Hi there..."}, function (response){
console.log(response);
});
});
}
},{
urls: ["<all_urls>"],
types: ["main_frame"]
},["responseHeaders"]
);
testContent.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.message == "Hi there..."){
console.log(request.message);
sendResponse({response: "bye bye"});
return true;
} else {
sendResponse({});
}
});
Related
I try to copy some cookies(in text format) to the clipboard. In content_script.js it's not a problem, but when I try to copy something into the clipboard in the background.js it doesn't work because it's not supported. So I decided to send the text from background.js to content_script.js and from there on I can copy it to the clipboard.
I use chrome.tabs.query() and chrome.tabs.sendMessage() to achieve this. Everytime I run my extension I get the following error inside the background console: Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
manifest.json:
{
"name": "Cookies to Desktop",
"manifest_version": 3,
"version": "0.0.1",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"content_script.js"
]
}
],
"background": {
"service_worker": "background.js"
},
"host_permissions": [
"*://*.google.com/*"
],
"permissions": [
"cookies",
"contextMenus",
"clipboardWrite",
"tabs"
]
}
background.js:
function foo() {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id,
{
message: "copyText",
textToCopy: "some text"
}, function (response) { })
})
}
foo()
content_script.js:
chrome.runtime.onMessage.addListener(
function (textToCopy) {
console.log('SUCCESS ' + textToCopy)
alert('SUCCESS')
}
)
How can I successfully send data from background to content
I'm trying to set up a background script fetching data from content periodically and write it to a file (kind of webpage scraping)
Unfortunately I got stuck at the very beginning:
Background sends a message to content and the callback is called but the parameter is not passed correctly, it's "undefined" whatever I try...
Manifest:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"content_scripts": [{
"matches" : ["<all_urls>"],
"js": ["content.js"]
}],
"browser_action": {
"default_title": "Test Extension"
},
"permissions": ["activeTab"]
}
content.js
chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
if (msg.text === 'report_back') {
sendResponse({data: "goodbye"});
}
});
background.js
chrome.browserAction.onClicked.addListener(function (tab) {
chrome.tabs.sendMessage(tab.id, {text: 'report_back'}, function(response) {
console.log(response.data);
});
});
If you are getting:
Error in event handler for (unknown): TypeError: Cannot read property 'data' of undefined at ...
I'm guessing you are clicking on the extension icon when you are in a tab like chrome://extensions
Try clicking on it from any other website and it should work.
that's my popUp.js.
window.onload = function() {
chrome.extension.sendMessage({
type: "login-check"
});
};
and that's my Background script.
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
switch(request.type){
case "login-check":
checkLogin();
break;
}
});
function checkLogin() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {type: "login"}, function(response){
console.log(response.farewell)
});
});
}
and that's my content-script.
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "login")
sendResponse({farewell: "goodbye"});
});
My message is sending from popUp.js to background.js but not sending from background to content script , showing me an error "Error in event handler for (unknown): TypeError: Cannot read property 'farewell' of undefined
" Please answer this question i am new to Chrome extension development and please don't send me link to others answers i have already implement all the possible answers in stack overflow.
Thanks in Advance.
and that's my Manifest.json
{
"manifest_version": 2,
"name": "Time Tracking Extension",
"description": "This Extension is for Time Tracking",
"version": "2.1",
"browser_action": {
"default_icon": "logo.png",
"default_popup": "popUp.html"
},
"permissions": ["storage", "tabs", "cookies"],
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["css/mystyles.css", "css/jquery-ui.css", "css/bootstrap.min.css"],
"js": ["js/jquery.min.js", "js/jquery-ui.js", "js/bootstrap.min.js", "js/content-script.js"]
}
]
}
I'm trying to capture the visible area of a page using chrome.tabs.captureVisibleTab. Here is the code that makes the call:
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.name == 'screenshot') {
chrome.tabs.captureVisibleTab(null, null, function(dataUrl) {
sendResponse({ screenshotUrl: dataUrl });
});
}
});
But when I try to capture the tab I get this error:
Unchecked runtime.lastError while running tabs.captureVisibleTab: The 'activeTab' permission is not in effect because this extension has not been in invoked.
Here is my manifest file:
{
"manifest_version": 2,
"name": "Empathy",
"version": "0.1",
"description": "Simulate accessibility issues for websites.",
"browser_action": {
"default_icon": "empathy19.png",
"default_title": "Empathy!"
},
"permissions": [
"activeTab",
"contextMenus",
"desktopCapture",
"tabCapture",
"tts" // Text-to-speech
],
"background": {
"scripts": [
"boot.js"
],
"persistent": false
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": [
"src/helpers.js",
"src/colorblindness.js",
"lib/colorvision.js",
"lib/html2canvas.js"
]
}
]
}
I have active tab permissions
The call is being made from a background script
I'm matching <all_urls>
Why do I get that error?
There are things that talk about <all_urls> as something to match, but what I was missing was the <all_urls> permission. After I added the permission, it worked.
im trying some stuff with chrome exstion API, and i want to send a data from the background.js to the contentscrip.js . everything i did fails, even tried the copy and paste the google example, and still no go. (doing this for 2 days lol)
here is my json file:
{
"name": "BlaExtension",
"version": "0.1",
"description": "bla the the exstion",
"permissions": [
"tabs","http://*/*","https://*/*"
],
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*/*","https://*/*"],
"js": ["content_script.js"]
}
],
"permissions": [
"tabs", "http://www.google.com/*"
],
"manifest_version": 2
}
background.js
chrome.tabs.getSelected(null, function (tab) {
chrome.tabs.sendMessage(tab.id, { greeting: "hello" }, function (response) {
alert(foo);
});
});
content_script.js
chrome.extension.onMessage.addListener(function (request, sender, sendResponse) {
alert(request.greeting);
});
i tried looking for help in old posts on messaging here on stackoverflow but nothing seem to help.
what im doing worng?
(sorry for my english)