【发布时间】:2022-07-19 20:54:29
【问题描述】:
我正在设计我的 Google Chrome 扩展程序来存储在完全不同的网站中创建的变量,这意味着我需要传递该变量。
这是网站script.js中的代码:
var editorExtensionId = "extension";
'use strict';
function x(){
chrome.runtime.sendMessage({n:1});
}
chrome.runtime.onMessage.addListener(function(loginKey){
console.log(loginKey.n);
});
chrome.action.onClicked.addListener(function(tab){
chrome.scripting.executeScript({
target: {tabId: tab.id},
function: x
});
});
然而,在控制台日志中,它给了我错误“Uncaught TypeError: Cannot read properties of undefined (reading 'addListener') 在 script.js:68:26"
我想知道如何解决这个问题。
【问题讨论】:
-
“这是网站 script.js 中的代码”
chrome.runtime.onMessage&chrome.action.onClicked只能在您的扩展程序中使用。您将无法在网站上使用这些! -
是的,我也是这么想的。那么我应该如何将变量发送到扩展程序呢?
-
抱歉,对 Chrome 扩展不太熟悉。
标签: javascript html google-chrome-extension