Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

137
Views
Chrome extension Can't send data from background to content

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

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!