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)
Related
I'm trying to send a request from background.js to content.js but it gives me an error saying:
Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist.
I have even tried to use the google chrome example in their documentation and that did not work. Probably I'm missing something somewhere. I would appreciate your support!
Manifest.js
{
"name": "test",
"description": "test",
"version": "1.0.0",
"manifest_version": 3,
"author": "test",
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["scripts/content.js"]
}
],
"action": {
},
"background": {
"service_worker": "scripts/background.js"
},
"permissions": [
"tabs"
],
"host_permissions": [
"*://*/*"
]
}
content.js
chrome.runtime.onMessage.addListener( (request, sender, sendResponse) => {
console.log(request);
})
chrome.action.onClicked.addListener(
(tab) => {
chrome.tabs.sendMessage(tab.id, {response: "Hello World"})
}
)
The tree of folder is:
manifest.json
scripts/background.js
scripts/content.js
I found the mistake that I've been doing. The example in the documentation was fine but I was printing console.log(request) and checking the console of the extension itself and I should've checked the console of the current page since it was in content.js and not in the background.js
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
Trying to send a message from background.js to content.js returns Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
I've tried:
background.js
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
chrome.tabs.sendMessage(tabId, "some-random-message")
})
content.js
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
alert(message)
sendResponse("Successfully received message.")
})
Is this happening because I'm trying to send a message to my content script after that tab has been closed? If so, is there any way to still access the document in the content script after or while the tab is closing?
To add more info, I also did try using chrome.tabs.onCreated... instead of onRemoved but with no avail.
EDIT: Adding my manifest.json file.
{
"manifest_version": 3,
"name": "Some Extension Name",
"version": "0.0.1",
"action": {
"default_popup": "popup.html",
"default_title": "Some Extension Name"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["./content.js"]
}
],
"background": {
"service_worker": "./background.js"
},
"permissions": ["cookies", "tabs", "storage"]
}
I have a very simple chrome extension, where I'm trying to pass a message from the background script to the content script. But chrome.runtime is undefined.
Here's literally all the code, as you can see there's almost nothing to it. In the content script, runtime is undefined.
Background Script:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.runtime.sendMessage({action: 'someAction'},
function(response) {
console.log('Response: ', response);
});
});
Content Script:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
sendResponse({
user: 'user'
});
});
manifest.json
{
"manifest_version": 2,
"name": "My Extension",
"version": "1.0",
"description": "Some stupid extension",
"browser_action": {
"default_icon": "icons/MyExtensionIcon.png"
},
"icons": {
"48": "icons/MyExtensionIcon.png"
},
"permissions": [
"tabs",
"storage",
"https://*/",
"http://*/"
],
"content_scripts": [
{
"matches": ["*://*.twitter.com/*", "https://twitter.com/*"],
"js": ["js/content.js"]
}
],
"background": {
"scripts": ["js/background.js"],
"persistent": true
},
"web_accessible_resources": [
"js/*",
"css/*"
]
}
Other Info:
Chrome Version 58.0.3029.110 (64-bit)
Installing my extension as an
"Unpacked extension" with developer mode
Ok I figured it out. It's absolutely stupid, but it appears this is simply a Heisenbug. By adding a breakpoint, or debugger statement, it causes that value to be undefined. Maybe a chrome bug?
I swear, every day Chrome feels more, and more like Internet Explorer.
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.