从chrome商店安装扩展时,chrome.runtime未定义

时间:2014-02-12 06:46:01

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

当我使用谷歌浏览器浏览器console.log(chrome)时,我获得了某些属性,但我发现chrome的“运行时”属性不可用。

app: Object
csi: function () { native function GetCSI(); return GetCSI();}
loadTimes: function () { native function GetLoadTimes(); return GetLoadTimes();}
webstore: Object
__proto__: Object
__defineGetter__: function __defineGetter__() { [native code] }
__defineSetter__: function __defineSetter__() { [native code] }
__lookupGetter__: function __lookupGetter__() { [native code] }
__lookupSetter__: function __lookupSetter__() { [native code] }
constructor: function Object() { [native code] }
hasOwnProperty: function hasOwnProperty() { [native code] }
isPrototypeOf: function isPrototypeOf() { [native code] }
propertyIsEnumerable: function propertyIsEnumerable() { [native code] }
toLocaleString: function toLocaleString() { [native code] }
toString: function toString() { [native code] }
valueOf: function valueOf() { [native code] }
get __proto__: function __proto__() { [native code] }
set __proto__: function __proto__()

所以chrome.runtime未定义。

因此我无法将chrome.runtime.sendMessage用于我的扩展程序

如何解决上述问题?

编辑:

我的代码是:

    if(typeof(chrome) === 'undefined'){
                result.isChromeBrowser = false;
                return next(result);
              } else {
                result.isChromeBrowser = true;
              }

console.log(chrome.runtime);  // undefined

              //check whether the chrome runtime is available or not ...
              if(!chrome.runtime){
                result.isChromeRuntimeAvailable = false;
                console.log(result);
              } else {
                result.isChromeRuntimeAvailable = true;
              }

编辑2:

从这里开始:https://developer.chrome.com/extensions/manifest/externally_connectable.html。 我确信(通过上面的链接后,如果我错了,请更正我)网页可以与Chrome扩展程序进行通信。但是从chrome商店安装扩展程序时无法完成,但是如果从本地目录安装了扩展程序,则无法正常工作。

我提供externalConnectable as:

"externally_connectable": {
        "matches": [
            "*://local.mywebsite.com/*"
        ]
    }

我已将external_connectable包含在“matches”属性中。现在,当我加载解压缩的目录以安装扩展程序时,我的网页获得chrome.runtime ..但是当我从chrome商店安装扩展程序时,同一个浏览器上的同一个网页不会得到chrome.runtime ..为什么呢? 最后,我仍然没有在页面://local.mywebsite.com/ 上使用chrome.runtime。 帮助我。

2 个答案:

答案 0 :(得分:2)

我的问题通过从chrome商店中完全删除插件并重新上传并重新发布插件来解决。

问题是:  最初我没有'external_connectable'属性,因此无法定义chrome.runtime。后来当我加入时,我正在更新chrome插件。 主要原因可能是:'Chrome商店不会通过上传更新插件来修改'manifest.json'(至少对于某些属性,如'external_connectable')。您可能必须删除并重新上传以更新manifest.json'(这是我可以得出的结论,因为我的经验,如果我错误地使用了一些有效的参考资料,请纠正我。)

所以'chrome.runtime'仍未定义。

后来当我删除插件并重新上传时,一切正常。

答案 1 :(得分:1)

您可能已经解决了以前已经解决过的问题:
chrome.runtime.sendMessage not working as expected

尝试检查sendMessage的可用性。如果没有,那么chrome版本真的很旧:
Chrome Extension: Port error: Could not establish connection. Receiving end does not exist.

希望我帮助,欢呼!