ephemeral_future
ephemeral_future

Reputation: 3

Storing/Accessing a simple variable in Firefox Addon SDK

I hope that I haven't missed an answer here to this question, which seems far too simple but I can't find an answer between Mozilla or Stackoverflow.

I am trying to use the Mozilla Addon SDK simple storage to store one single number that contains the number of pages that have been visited since the addon was activated. In order to do this I am increasing the variable number once on every page load. See code below. The only thing I can't find information on is how to load the stored data from the console (or, if I am misunderstanding, is this data not stored locally in the browser?). Where the comment says "//load ss.storage.page_visits" is where my problem arises - I cannot understand how to load this data.

var ss = require("sdk/simple-storage");
var pageMod = require("sdk/page-mod");
//load ss.storage.page_visits
if (!ss.storage.page_visits){
    ss.storage.page_visits = 0;
}
var test=   '"<style>\
                h1{\
                    font-size: 5em;\
                    color: red;\
                    font-family: Helvetica;\
                }\
            </style>\
                <h1>test</h1><h2>' + ss.storage.page_visits + '</h2>"';

ss.storage.page_visits++;
if (ss.storage.page_visits/5 == 1){
    pageMod.PageMod({
      include: "*",
      contentScript: 'document.body.innerHTML = ' +
                     test
    });
    ss.storage.page_visits = 0;
}else{
    pageMod.PageMod({
      include: "*",
      contentScript: 'document.body.innerHTML = ' +
                     test
    });
}
console.log(ss.storage.page_visits);

Upvotes: 0

Views: 284

Answers (1)

willlma
willlma

Reputation: 7533

There's nothing wrong with simple-storage. It's just that

By default, cfx run and cfx test use a new profile each time they are executed. This means that any profile-specific data entered from one run of cfx will not, by default, be available in the next run.

See Using --profiledir to run on the same profile every time, or Extension Auto-Installer to run your extension without restarting the browser.

The way things are currently set up, your stored variable will increment once per install, not per page visit. If you want to increment it on page visit, you'll need to put ss.storage.page_visits++ in the PageMod's onAttach constructor function.

Also, why ss.storage.page_visits/5 == 1?

ss.storage.page_visits == 5

Upvotes: 1

Related Questions