javascript - Chrome Extension Storing Custom Object Type Strips Prototype Methods -


i have created custom object using in extension. when save objects of type group (my object type) , later pull objects out of storage, appears prototype methods no longer present. read in documentation objects serialize down object literals {} , can't seem figure out how keep methods objects. have provided code of group class below. when try , use 1 of methods file below on object retrieved storage, error function not exist. used in loop loop through of properties , object has name , urls property. appreciated!

group.js:

// create group class var group = function (name, urls) {   this.name = name;   this.urls = urls; };  // clears urls group group.prototype.clearurls = function () {   this.urls = []; };  // adds specified url group group.prototype.addurl = function (url) {   this.urls.append(url); };  // removes specified url group group.prototype.removeurl = function (url) {   this.urls = this.urls.filter(function(_url){     return url !== _url;   }); };  // renames group group.prototype.rename = function (name) {   this.name = name; };  // checks whether or not group contains specified url // returns either true or false group.prototype.containsurl = function (url) {   var contains = false;   (var = 0; < this.urls.length; i++) {     if (this.urls[i] === url) {       contains = true;       break;     }   }   return contains; }; 

edit:

here background.js script, shows how object retrieved , how called later in script. fails when receives addurl message , attempts call containsurl() on currentgroup.

// global variables var currentgroup; var groups = []; var options = [];  // short hand function save current data storage var savedata = function () {   // save default options, currrentgroup, , groups   chrome.storage.sync.set({'options': options, 'currentgroup': currentgroup, 'groups': groups}, function() {     if (chrome.runtime.lasterror) {       console.error("could not save because: " + chrome.runtime.lasterror);     }   }); }  // on start query saved data make sure data current chrome.storage.sync.get(function(items) {   // check if there groups   if (items['groups']) { // set groups     groups = items['groups'];   } else { // create default group , add list of groups     currentgroup = new group('default', []);     groups = [currentgroup];   }    // check current group, if none set first available group   if (items['currentgroup']) {     currentgroup = items['currentgroup'];     console.log(object.getownpropertynames(currentgroup));   } else {     currentgroup = groups[0];   }    // check options   if (items['options']) {     options = items['options'];   } else {     // no options, set default options , save them     options['overridehomepages'] = true;   }    savedata();    // after data has been fetched bring tabs   chrome.tabs.query({'currentwindow': true}, function(tabs) {     (var = 0; < currentgroup.urls.length; i++) {       if (options['overridehomepages']) {         if (tabs[i].url.length > 0) {           chrome.tabs.update(tabs[0].id, {'url': currentgroup.urls[i]});         } else {           chrome.tabs.create({'url': currentgroup.urls[i]});         }       } else { // don't override homepages or open tabs         chrome.tabs.create({'url': currentgroup.urls[i]});       }       currentgroup.urls[i]     }   }); // end tabs.query  }); // end storage.sync.get  // add message listener chrome.runtime.onmessage.addlistener(function(request, sender, sendresponse) {    // if add url sent   if (request.message === 'addurl') {     console.log('recieved message: ' + request.message);     // check if group contains url     if (currentgroup.containsurl(sender.url) === false) {       currentgroup.addurl(sender.url);       savedata();       sendresponse({'message': 'saved ' + sender.url});     }   }    // if remove url sent   if (request.message === 'removeurl') {     // check if group contains url     if (currentgroup.containsurl(sender.url)) {       currentgroup.removeurl(sender.url);       savedata();       sendresponse({'message': 'removed ' + sender.url})     }   } }); 

i believe chrome.storage used save key-value items, not including prototype/functions. however, didn't find official docs this.

one workaround using group.prototype.containsurl.call(currentgroup, sender.url), allows invoke containsurl specifying context "this".


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -