Cannot read properties of undefined (reading 'addListener')

2,814 views
Skip to first unread message

YS LIN

unread,
Dec 7, 2022, 4:38:05 AM12/7/22
to Chromium Extensions
Hi there,
My extensions in development normal operation, But there will be a prompt after the Google extensions Store is launched "Uncaught TypeError: Cannot read properties of undefined (reading 'addListener')" And the function cannot be used normally.my chrome extensions version is V3.
The specific failure functions are: chrome.contextMenus.onClicked.addListener() Because other functions defined are normal, only it fails.This problem has troubled me for a long time. Does anyone have a similar problem?


This error will be reported when I right click on the menu(“查找相似图书资源”)。

YS LIN

unread,
Dec 7, 2022, 4:41:49 AM12/7/22
to Chromium Extensions, YS LIN
// remove all contentMenus
chrome.contextMenus.removeAll(function () {
  // create contentMenus
  chrome.contextMenus.create(contextMenu)
})

// handle function
function contextMenusListener(info, tab) {
  chrome.tabs.sendMessage(tab.id, {
   // xxx
  })
}

// listen click do something       
chrome.contextMenus.onClicked.addListener(contextMenusListener)       // error

Jackie Han

unread,
Dec 7, 2022, 5:06:28 AM12/7/22
to YS LIN, Chromium Extensions
You should add "chrome.contextMenus.onClicked.addListener" at top level in service worker, not in contextMenus.removeAll()'s callback.

If it works in dev mode, but doesn't work when published to CWS, check your bundled/minified code.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/7c5af44b-0582-4394-be7b-20aff3432d1fn%40chromium.org.

Stefan Van Damme

unread,
Dec 7, 2022, 5:17:13 AM12/7/22
to Chromium Extensions, lys...@gmail.com
Hi Lys,

See this sample on how to use the contextMenus (also for Manifest V3):
https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/mv2-archive/api/contextMenus/basic

Thanks,
Reply all
Reply to author
Forward
0 new messages