D
D
dimonfreeman2021-10-29 17:19:09
Google Chrome
dimonfreeman, 2021-10-29 17:19:09

How to pass value to chrome.alarms in Chrome extension?

How to pull chrome.alarms from script inside DOM.
wanted through the localStorage variable, but chrome api chtoli does not have access there.

The bottom line is that if there is an element A on the page,
after a while, run chrome.alarms

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2021-10-29
@dimonfreeman

dimonfreeman , I see, well, you stupidly asked a question, so it looks like I gave you the wrong answer.
If you need to trigger an alert on a background page, then the algorithm changes a bit. The problem here is that only the content script (scripts.js) can send a message to the background process (background.js), so you need to use it as an intermediary.
So, for starters, in the background.js file, put a message listener like this:

chrome.extension.onMessage.addListener(function(message) {
  
  console.log('Из контент-скрипта получено следующее сообщение: ' + message);
  
  if (message == 'вызвать алерт') {
    
    // Здесь впишите ваш код для вызова алерта
    
  }
  
});

This listener will wait for messages from the content script (script.js) and when it receives a message with the text "trigger alert" it will execute the code that you write.
Further, inside the content script, you also need your own listener:
window.addEventListener('message', function(event) {
  
  // Извлекаем текст сообщения:
  var dom_message = event.data;
  
  console.log('Из DOM получено сообщение: ' + dom_message);
  
  // Пересылаем полученное сообщение
  // в фоновый процесс background.js:
  
  chrome.extension.sendMessage(event.data);
  
});

This listener will wait for messages from the DOM and forward them to the background.js background process
. Now, inside the DOM, you can safely call: code that you need - not necessarily an alert, anything is possible.
window.postMessage('вызвать алерт', '*');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question