Chrome API - chrome.runtime.onMessage.addListener не реагирует на вызовы
Написал подробно комментарии где выводит лог, а где нет. Предоставил лишь фрагменты кода. Непонятные функции могу объяснить. Честно сейчас для меня это магия какая-то тк у меня даже не работают копипаст примеры
chrome.runtime.onMessage.addListener в подключенном файле никак не реагирует и не вызывается при sendMessage
Background.js
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.status === "find_link") {
console.log(request.url);
chrome.tabs.create({ url: request.url })
}
}
)
function include(tabId) {
chrome.tabs.executeScript(tabId, { file: './js/query.js' }, () => console.log('Imported fetching videos'))
}
function startSearch() {
openWindow().then(async window => {
for (let i = 0; i < queries.length; i++) {
const query = queries[i];
let Link = getSearchLink(query)
let Tab = await openTab(window.id, i, Link)
// chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
// var activeTab = tabs[0]
// include(activeTab.id)
// console.log('выводит id: ', activeTab.id);
// chrome.tabs.sendMessage(activeTab.id, { status: "opened" }, function() { console.log('выводит req sended'); })
// })
var activeTab = Tab
include(activeTab.id)
console.log('output id: ', activeTab.id);
chrome.tabs.sendMessage(activeTab.id, { status: "opened" }, function(res) { console.log('should output', res.result); })
// res.result - res of undefined
}
})
}
Query.js
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log('doesnt output');
if (request.status === "opened") {
const links = getVideos()
chrome.extension.sendMessage({ status: "find_video", url: links[0] }, function(response) {
console.log('dont output - link was found');
});
}
sendResponse({ result: "success" })
}
)
console.log('But this log are output, script was included');
Manifest.js
{
"name": "YouTube [BOT]",
"description": "Do some youtube action in browser",
"version": "1.0",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["tabs", "storage", "activeTab", "http://*/", "https://*/"],
"update_url": "https://clients2.google.com/service/update2/crx"
}