Chrome Extension background page state
<p>I am implementing a notification to be appended to a page if a user is not logged in on the extension but I only want the extension to do this once per browser session so that they don't get an annoying notification for every page they visit if they're not logged in.</p>

<p>They way I do this is by initializing a boolean in my background script to false, it represents whether or not a notification has been appended before.</p>

<p>Whenever a user is not logged in the background script will receive a message and based on the boolean mentioned above it will notify the user that they are not logged in.</p>

<p>background.js</p>

<pre><code>var has_been_notified = false;
chrome.extension.onMessage.addListener(function(msg, sender) {
    if(!has_been_notified) {
        chrome.tabs.sendMessage(sender.tab.id, "login_notification");
        has_been_notified = true;
    }
})
</code></pre>

<p>The conditional should run once and never run again once the boolean is changed to true. This is under the assumption that the state of the background script is preserved throughout a browser session and never deployed again.</p>

<p>While I test the extension some random pages will get the notification even after the initial notification has fired. The pages that get it are about 1 in 10 but completely arbitrary of the site I visit.</p>