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

157
Views
Firefox extension content script does not load during temporary add-on load

I'm developing an extension that involves both a background script and a content script. The content script gets the selected text from webpages, when the user clicks on the extension's relative menu entry and then sends it to the background script for further processing.

Here is the relative section of the manifest.json:

"background": {
    "scripts": ["background.js"]
  },
  
  "content_scripts": [
  {
    "matches": ["<all_urls>"],
    "js": ["content.js"]
  }
]

I load the extension in about:debugging > This Firefox > Load Temporary Add-on... in order to test it.

In the code I send a message from the background script to the content script but that throws an error:

Uncaught (in promise) Error: Could not establish connection. Receiving end does not exist

I checked by using the Debugger, in about:devtools-toolbox and I found out that only the background script loads.

Does anyone have an idea what makes the content script to fail to load and what would the solution be?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The background script should send a message to the content script, asking for the selected text, and then listen to the message it receives. Then it can call the function that does the Google search:

function onCreated() {
  if (browser.runtime.lastError) {
    console.log(`Error: ${browser.runtime.lastError}`);
  } else {
    console.log("Item created successfully");
  }
}

browser.menus.create({
  id: "context-entry",
  title: 'search',
  contexts: ['all'],
  onclick: getText
}, onCreated);

async function getText() {
  const tabInfo = await getCurrentTab();
  const [{ id: tabId }] = tabInfo;
  browser.tabs.sendMessage(tabId, { trigger: 'getText' });
}

browser.runtime.onMessage.addListener(function ({ txt }) {
  doSearch(txt);
});

function doSearch(txt) {
  var searchURL = `https://www.google.com/search?q=${txt}`;
  browser.tabs.create({url: searchURL});
}

And the content script should listen to the message from the background script, and send the selected text back.

browser.runtime.onMessage.addListener(({ trigger }) => {
  if (trigger === 'getText') {
    const selection = window.getSelection();
    const txt = selection.toString();
    browser.runtime.sendMessage({ trigger: 'foundText', txt });
  }
});
about 4 years ago · Juan Pablo Isaza Report
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!