我正在开发一个生成 Chrome 扩展程序的项目。在后台页面中,我有一个名为checkingProcess. 当打开新选项卡或更新选项卡时执行此功能。(我试图在这里捕捉 URL 的变化。)
chrome.tabs.onActivated.addListener((activeInfo) => {
checkingProcess(activeInfo.tabId)
})
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
checkingProcess(tab.id)
})
然后,在checkingProcess函数中,我有一些用于数据处理和 API 调用的函数。然后我尝试接收来自弹出窗口的消息。此消息表示弹出窗口已被用户打开。
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.msg === 'popup_opened') {
sendResponse({
matches_length: response['matches'].length,
hostname: host,
})
}
chrome.runtime.lastError
})
之后,它会向弹出窗口发送响应。在弹出窗口中,我听取消息并使用弹出窗口中的响应。
useEffect(() => {
chrome.runtime.sendMessage({ msg: 'popup_opened' }, (res) => {
setHostname(res['hostname'])
setMatchesLength(res['matches_length'])
console.log(res['hostname'], 'burası')
console.log(res['matches_length'], 'burası')
})
}, [])
但是,我意识到这个消息过程只执行一次,但我需要多次运行它才能同时访问后台的数据。我怎样才能做到这一点?