问题描述
当我使用谷歌Chrome浏览器时,我得到了某些属性,但我发现Chrome的运行时属性不可用。
app:Object
csi:function(){native function GetCSI(); return GetCSI();}
loadTimes:function(){native function GetLoadTimes(); 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]}
构造函数: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.runti me.sendMessage for my extension
如何解决以上问题?
编辑:
我的代码是:
if(typeof(chrome)==='undefined' ){
result.isChromeBrowser = false;
return next(result);
} else {
result.isChromeBrowser = true;
}
console.log(chrome.runtime); //未定义
//检查chrome运行时是否可用...
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存储库安装扩展程序时,无法完成此操作,但是在从本地目录安装扩展程序时可以很好地工作。
externally_connectable:{
matches:[
*://local.mywebsite .com / *
}
我已将externally_connectable与匹配属性..现在,当我加载解压目录安装扩展,我的网页得到chrome.runtime ..但是当我安装扩展从铬商店,相同的浏览器上的网页不会得到chrome.runtime ..为什么所以??
到底我还是没有在页面上使用chrome.runtime ://local.mywebsite.com/ 。
帮助我。
我的问题可以通过从Chrome商店完全删除插件并重新上传并重新发布该插件。
问题是:
最初我没有'externally_connectable'属性,所以无法获得 chrome.runtime
定义。后来,当我包括,然后我更新铬插件。
主要原因可能是:'Chrome商店不会修改'manifest.json'(至少对于像'externally_connectable'这样的特定属性),只需通过上传更新插件即可。您可能需要删除并重新上传以获取manifest.json更新'(这是我可以得出的结论,因为我的经验,请纠正我,如果我错了一些有效的参考源。)
等'chrome.runtime'保持未定义。
稍后当我删除插件并重新上传时,一切正常。
When i console.log(chrome)
with google chrome browser i get certain properties but i find the 'runtime' property of chrome is not available.
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__()
so chrome.runtime is undefined.
and hence i am not able to use chrome.runtime.sendMessage for my extension
How to resolve the above??
EDIT :
my code is :
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;
}
EDIT 2 :
from here : https://developer.chrome.com/extensions/manifest/externally_connectable.html.I am sure(correct me if i am wrong after going through above link) that a web page can communicate with a chrome extension. But not able to make it up through when the extension is installed from chrome store, however working perfectly in case of extension installed from local directory.
i am providing externallyConnectable as :
"externally_connectable": {
"matches": [
"*://local.mywebsite.com/*"
]
}
I have included the externally_connectable with "matches" property.. Now when i load unpacked directory to install extension, my web page get chrome.runtime.. but when i install extension from chrome store, the same web page on same browser does not get chrome.runtime.. why so??in the end i still dont have chrome.runtime on the page ://local.mywebsite.com/.help me out.
My problem get to solved by removing the plugin completely from chrome store and re-upload and re-publish the plugin again.
The problem was : Initially i did not had 'externally_connectable' property, so wasn't able to get chrome.runtime
defined. Later when i included, then i was updating the chrome plugin.And the main cause may be : 'Chrome store does not modify the 'manifest.json' (at least for certain properties like 'externally_connectable') just by updating the plugin by uploading. You may have to remove and re-upload to get manifest.json updated' (This is what i can conclude because of my experience, Please correct me if i am wrong with some valid reference source.)
and so 'chrome.runtime' remains undefined.
Later when i removed the plugin and re-uploaded, everything worked fine.
这篇关于chrome.runtime在从Chrome商店安装扩展时未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!