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
Related
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 am making a chrome extension it will get me to certain page by autoclicking. Since it goes from page to page. I am not able to get past after the first page. I am not able to do when the second page loads after first click
manifest.json
{
"manifest_version": 2,
"name": "AutoClick",
"description": "This autoclicks",
"version": "1.1",
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"content_scripts": [{
"matches": ["*://*.blogger.com/*"],
"js": ["content.js"],
"run_at": "document_end",
"all_frames": false
}],
"permissions": [
"activeTab",
"tabs",
"*://*.blogger.com/*"
]
}
background.js
chrome.tabs.create
chrome.browserAction.onClicked.addListener(function (tab) {
var newURL = "https://draft.blogger.com/blog/posts/3778735982367012067";
chrome.tabs.create({ url: newURL });
chrome.tabs.executeScript({ file: "content.js" });
});
content.js
function createpost(){
document.querySelector('.CwaK9').click()
}
function video1(){
document.querySelector('.vde74d').click()
}
if (window.location.href == "https://draft.blogger.com/blog/posts/3778735982367012067") {
createpost()
newFunction()
}
function newFunction() {
if (window.location.href.match('https://draft.blogger.com/blog/posts/3778735982367012067/')) {
video1();
}
}
using match because it add random ID to end of the URL after first click.
It works fine during first click but when the next page loads it don't.
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({});
}
});
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)