Как отправлять данные в popup постоянно, без клика на расширение google chrome?
Есть расширение для Google Chrome, сделал вывод данных в popup, но там значение выводится только по клику, нужно чтобы вывод был постоянным. Как это можно реализовать?
Код background.js
chrome.runtime.onMessage.addListener((msg, sender) => {
// First, validate the message's structure.
if ((msg.from === 'content') && (msg.subject === 'showPageAction')) {
// Enable the page-action for the requesting tab.
chrome.pageAction.show(sender.tab.id);
}
});
Код popup.js
// Update the relevant fields with the new data.
const setDOMInfo = info => {
document.getElementById('total').textContent = info.total;
document.getElementById('inputs').textContent = info.inputs;
document.getElementById('buttons').textContent = info.buttons;
};
// Once the DOM is ready...
window.addEventListener('DOMContentLoaded', () => {
// ...query for the active tab...
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
// ...and send a request for the DOM info...
chrome.tabs.sendMessage(
tabs[0].id, {
from: 'popup',
subject: 'DOMInfo'
},
// ...also specifying a callback to be called
// from the receiving end (content script).
setDOMInfo);
});
});
Код content.js
// Inform the background page that
// this tab should have a page-action.
chrome.runtime.sendMessage({
from: 'content',
subject: 'showPageAction',
});
// Listen for messages from the popup.
chrome.runtime.onMessage.addListener((msg, sender, response) => {
// First, validate the message's structure.
if ((msg.from === 'popup') && (msg.subject === 'DOMInfo')) {
// Collect the necessary data.
// (For your specific requirements `document.querySelectorAll(...)`
// should be equivalent to jquery's `$(...)`.)
var searchThis = document.getElementsByClassName('buttonText-3X2QgaDd');
for(let i = 0; i< searchThis.length; i++){
// console.log(searchThis[1].textContent);
//localStorage.setItem("perem", searchThis[1].textContent); //запишем его в хранилище по ключу "Sentiment"
var tradeviewitem = searchThis[1].textContent;
}
console.log('Пришло - ' + tradeviewitem);
var domInfo = {
total: tradeviewitem,
inputs: document.querySelectorAll('input').length,
buttons: document.querySelectorAll('button').length,
};
// Directly respond to the sender (popup),
// through the specified callback.
response(domInfo);
}
});