Sunday, 21 October 2018

Access user options in background.js in Chrome Extension

I'm trying to access chrome.storage.sync where I store some user options in my background.js but the asynchronous nature of chrome.storage.sync.get is causing me issues.

If I try and use chrome.storage.sync.get within my chrome.webRequest.onBeforeRequest.addListener the callback isn't quick enough for the function to use it.

I have tried adding the user options as a global variable within background.js but it appears to me that that value doesn't persist.

Anyone else using user options in background.js?

function getoption(){
  chrome.storage.sync.get({
    radarpref: 'nothing',
  }, function(items) {
  console.log(items.radarpref);
  return items.radarpref;
    
  });

}
var hold = getoption();
console.log (hold) //this returns hold value

chrome.webRequest.onBeforeRequest.addListener(
  function(info) {

    //this doesn't work - yet
   console.log('i dont see the hold');
        console.log(hold) //hold not returned when callback ran
  ...
  


from Access user options in background.js in Chrome Extension

No comments:

Post a Comment