【问题标题】:Uncaught TypeError: Cannot read property 'get' of undefined when detecting chrome extension未捕获的类型错误:检测 chrome 扩展时无法读取未定义的属性“get”
【发布时间】:2016-03-26 12:59:15
【问题描述】:

我们编写了一个 chrome 扩展,它通过本地消息从外部网页(部署在本地服务器 tomcat 中)调用。我们希望外部页面首先检测到用户安装了 chrome 扩展,然后与扩展进行通信。 这是扩展清单文件:

"permissions":
[
"tabs",
"http://*/*",
"nativeMessaging",
"management"
],
"externally_connectable":
{
"matches": ["http://localhost:8080/test.jsp"]}

]

外部页面将扩展称为:

chrome.management.get(extension_id,function(a){alert("hi");});

但我在 chrome 控制台中遇到错误:

未捕获的类型错误:无法读取未定义的属性“get”。

任何人都可以帮助解决问题以检测已安装的扩展。

谢谢。

【问题讨论】:

  • 亲爱的@wOxxOm 我在部署在本地网络服务器中的外部网页中使用它。
  • 正如您在the documentation 中看到的,唯一可用的API 是chrome.runtime.connectchrome.runtime.sendMessage
  • 亲爱的@ wOxxOm,您的意思是无法从外部网页检测到安装了特定扩展程序?
  • 是的,我用谷歌搜索过,但他们都使用 chrome.management.get,但调用它时出现错误。

标签: javascript jquery google-chrome google-chrome-extension


【解决方案1】:

你试图解决错误的问题。

如果您的分机是externally_connectable,您应该尝试连接。否则你会得到一个有意义的错误。

请注意,只有在至少有一个 externally_connectable 分机准备好收听时,chrome.runtime.sendMessage 才会显示在页面上。所以你也需要检查一下:

if(chrome && chrome.runtime && chrome.runtime.sendMessage) {
  chrome.runtime.sendMessage(
    extension_id,
    message,
    onMessageCallback
  );
}

function onMessageCallback(response) {
  if(chrome.runtime.lastError) {
    // Something went wrong, probably the right extension is not installed
  }
}

只需确保您的扩展程序中确实有代码在监听chrome.runtime.onMessageExternal

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 2017-10-28
    • 1970-01-01
    • 2013-02-13
    • 2018-10-20
    • 2016-01-03
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多