0

I'm trying to create a browser extension, specifically for Chrome. I copied the example almost exactly from here (https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/lastError) for a content and background script.

My content.js file

function send(e){
const sending = chrome.runtime.sendMessage({content:"message"})
console.log("THIS SHOULD BE A PROMISE",sending)
}
window.addEventListener("click",send)

My background.js file

function handleMessage(request, sender, sendResponse) {
  console.log(`content received is: ${request.content}`);
  return Promise.resolve({response: "response from background script"});
}

chrome.runtime.onMessage.addListener(handleMessage);

When I have the background page open, and I click, the background page prints content script sent a message: message but my browswer's console prints THIS SHOULD BE A PROMISE undefined.

My manifest.json file is minimal to replicate this.

{
  "manifest_version": 2,
  "name": "My First Extension",
  "version": "0.1",
  "browser_action": {
       "default_icon": {                    
         "16": "icon.png",        
         "24": "icon.png",          
         "32": "icon.png"     
       },
    "default_title": "test"      
  },
  "permissions": [
    "activeTab",
    "storage"
  ],
  "background": {
    "scripts": ["js/background.js"],
    "persistent": false
  },
  "content_scripts": [
    {
      "matches": [ "http://*/*", "https://*/*", "file://*/*" ],
      "js": [ "js/content.js" ]
    }
  ],

Anyone have any idea what's going on? I checked multiple other threads on this topic to no avail.

Azrael
  • 21
  • 1
  • 2
    You should use the chrome developer docs to develop chrome extensions, not the mozilla docs. https://developer.chrome.com/apps/runtime#method-sendMessage – tkausl Oct 01 '20 at 23:24
  • If you want the code to work like mozilla browser extension, I think https://pastebin.com/JJ67yR9n will do what you want – Jaromanda X Oct 01 '20 at 23:41
  • by the way, there's no `.runtime.sendMessage` in that link - so, not sure how your code is even close to the code in that link, let alone an almost exact copy! – Jaromanda X Oct 01 '20 at 23:51

0 Answers0