gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 05:29:56 26 4
gpt4 key购买 nike

当我使用 google chrome 浏览器 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/docs/extensions/mv3/manifest/externally_connectable .我确定(如果我通过上面的链接错了,请纠正我)网页可以与 chrome 扩展程序通信。但是当从 chrome 商店安装扩展时无法弥补它,但是在从本地目录安装扩展的情况下工作完美。

我正在提供 externallyConnectable 作为:

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

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

最佳答案

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

问题是: 最初我没有“externally_connectable”属性,所以无法定义 chrome.runtime。后来当我包含时,我正在更新 chrome 插件。主要原因可能是:“Chrome 商店不会通过上传更新插件来修改‘manifest.json’(至少对于某些属性,如‘externally_connectable’)。您可能需要删除并重新上传才能更新 manifest.json'(根据我的经验,这是我可以得出的结论,如果我对某些有效引用源有误,请纠正我。)

因此 'chrome.runtime' 仍未定义。

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

关于javascript - 从 chrome 商店安装扩展时,chrome.runtime 未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21720602/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com