尝试在我的内容和后台脚本之间进行通信时,我收到以下错误:
Port error: Could not establish connection. Receiving end does not exist.
Error in event handler for 'undefined': Cannot read property 'message' of undefined
TypeError: Cannot read property 'message' of undefined
Run Code Online (Sandbox Code Playgroud)
background.js
function onRequest(request, sender, callbackFunction) {
console.log("Me (BS) became this Message:" + request.message);
sendResponse({message: request.message})
};
chrome.extension.onRequest.addListener(onRequest);
Run Code Online (Sandbox Code Playgroud)
streamcloud.js
function contactBackground(nachricht){
chrome.extension.sendMessage({message: nachricht}, function(response) {
console.log("The Background Script got the following Message: " + response.message);
});
}
Run Code Online (Sandbox Code Playgroud)
和我的manifest.json
{
"name": "InstantWatch - Dev",
"manifest_version": 2,
"version": "0.7",
"permissions": ["tabs", …Run Code Online (Sandbox Code Playgroud) 当我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() { …Run Code Online (Sandbox Code Playgroud) 我对我的第一个“将所有内容放在一起”的 Chrome 扩展程序感到困惑,我将描述我正在尝试做的事情,然后我是如何通过一些脚本摘录来解决这个问题的:
Run Code Online (Sandbox Code Playgroud)function load_options() { var repl_adurl = localStorage["repl_adurl"]; default_img.src = repl_adurl; tf_default_ad.value = repl_adurl; } function save_options() { var tf_ad = document.getElementById("tf_default_ad"); localStorage["repl_adurl"] = tf_ad.value; } document.addEventListener('DOMContentLoaded', function () { document.querySelector('button').addEventListener('click', save_options); }); document.addEventListener('DOMContentLoaded', load_options );
Run Code Online (Sandbox Code Playgroud)var s = document.createElement('script'); s.src = chrome.extension.getURL("myscript.js"); console.log( s.src ); (document.head||document.documentElement).appendChild(s); s.parentNode.removeChild(s);
我从 html 源中抓取图像没有任何问题,但我似乎无法访问 localStorage 数据。我意识到它必须与具有不同环境的两个脚本有关,但我不确定如何克服这个问题——据我所知,我需要从 contentscript.js 注入 myscript.js,因为 contentscript.js 没有可以访问 html 源代码。 …