1

I'm trying to send info from background.js to popup. I read a few questions and answers here, here and here, and decided to use chrome.extension.getViews to directly call function in popup. Before I start to code the actual info passing, I tried the getViews first, but I got an Error in the event handler: TypeError: chrome.extension.getViews is not a function right after loading the extension (load unpacked). I saw the word Foreground Only here, so I move the code to content-script.js but I still got the same error. Please help.

Here is my manifest.json

{
  "name": "Action API Demo",
  "version": "1.0",
  "manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "host_permissions": [
    "............."
  ],
"permissions": [
    "activeTab", "declarativeContent"
],

"action": {
    "default_title": "Default Title",
    "default_popup": "popups/popup.html",
    "default_icon": {
      ........
    }
},
"icons": {
    ........
},
"content_scripts": [
    { 
      "matches": [............],
      "js": ["cs.js"]
    }
]

}

Here is my background.js

chrome.runtime.onInstalled.addListener(() => {
    testViews();
});
function testViews() {
    var popWins = chrome.extension.getViews({type: 'popup'});
    for (win in popWins) {
        console.log(win.location.href);
    }
}

EDIT: If I change the manifest.json to MV2, the error disappeared but always return an empty array. This is not mentioned in chrome's developer site. Obviously I want to us MV3, so cannot help. Any idea?

senderj
  • 400
  • 1
  • 9

0 Answers0