0

How to control a variable in background script from page (popup) script?
To getting resource each 10 seconds, I am using setTimeout loop in the below code.
But below codes do not stop loop when I unchecked the checkbox in popup page (browser_action).

I expected that when I (check or uncheck) the checkbox in popup, the page_script send message to background script, if background receive stop_loop message, the background script change settimeout_loop_controller to false to stop settimeout loop.

But this codes do not react when I click checkbox.

How to communicate between two scripts?

let settimeout_loop_controller = true;

function fetching() {
    if (settimeout_loop_controller) { 
    // if 'settimeout_loop_controller become false, the loop will stop.
        fetch("http://url.com/example").then((getval) => { do_something })...  ;
        setTimeout(fetching, 10000);
    } else {
        return;
    }
}

fetching();

chrome.runtime.onMessage.addListener((message) => {
    if message.type === "stop_loop" {
        settimeout_loop_controller = false;
    }
    else if (message.type === "start_loop") {
        settimeout_loop_controller = true;
        fetching();
    }
});

popup script:

let checkbox = document.getElementById("checkbox");

checkbox.onchange((e) => {
    if (checkbox.checked) {
        chrome.runtime.sendMessage({"type": "start_loop"});
    }
    else {
        chrome.runtime.sendMessage({"type": "stop_loop"});
    }
});
KiYugadgeter
  • 3,796
  • 7
  • 34
  • 74
  • Please [edit] to be on-topic: include a **complete** [mcve] duplicating the problem. Usually, include a *manifest.json*, some of the background, content, and popup scripts and HTML. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Oct 07 '16 at 15:43
  • My answer to [Firefox WebExtension settings page](http://stackoverflow.com/a/39236352/3773011) provides a fully functional, tested in Firefox and Chrome, example of four (somewhat) different ways to communicate between an options/popup page and a background script. – Makyen Oct 07 '16 at 15:49
  • The reason that a [mcve] is required is that we want to help. It is **much** easier to help if we don't have to recreate all the code needed to duplicate the problem. This is code that you already have. So, please help us to help you and provide a *complete* [mcve] that duplicates the problem with such questions. Without a [mcve] the amount of effort required to even begin to help you is **much** higher which *significantly* reduces the number of people willing/able to help you. Even if we put out the extra effort, we have to *guess* at significant portions of what your problem might be. – Makyen Oct 07 '16 at 15:52

1 Answers1

2

The chrome.runtime.sendMessage API does not exist in page script, but it can be accessed in content script. A typical usage is page script post a message to content script using window.postMessage({"type": "stop_loop"},"*"), then forwarding the message inside content script.
For Firefox 49 and newer version, you can Sharing objects with page scripts. For example, you can define a function notify() and exportFunction(notify, window, {defineAs:'notify'}); in content script first. The page script will be able to call the exposed function in window object.

window.notify("Message from the page script!");

Beck Yang
  • 3,004
  • 2
  • 21
  • 26