2016-04-25 76 views
0

我已经创建了我在扩展中使用的自定义对象。当我保存Group类型的对象(我的对象类型),然后将这些对象从存储中取出时,看起来原型方法不再存在。现在我在文档中读到对象序列化为对象文字{},我似乎无法弄清楚如何保持对象的方法。我已经提供了下面的组类的代码。当我尝试从存储中检索到的对象上使用下面文件中的某个方法时,出现该函数不存在的错误。我使用了for循环来遍历所有的属性,并且对象具有名称和url属性。任何帮助将不胜感激!Chrome扩展存储自定义对象类型带原型方法

Group.js:

// Create the Group class 
var Group = function (name, urls) { 
    this.name = name; 
    this.urls = urls; 
}; 

// Clears all urls from the group 
Group.prototype.clearUrls = function() { 
    this.urls = []; 
}; 

// Adds the specified url to the group 
Group.prototype.addUrl = function (url) { 
    this.urls.append(url); 
}; 

// Removes the specified url from the group 
Group.prototype.removeUrl = function (url) { 
    this.urls = this.urls.filter(function(_url){ 
    return url !== _url; 
    }); 
}; 

// Renames the group 
Group.prototype.rename = function (name) { 
    this.name = name; 
}; 

// Checks whether or not the group contains the specified url 
// Returns either true or false 
Group.prototype.containsUrl = function (url) { 
    var contains = false; 
    for (var i = 0; i < this.urls.length; i++) { 
    if (this.urls[i] === url) { 
     contains = true; 
     break; 
    } 
    } 
    return contains; 
}; 

编辑:

这里是background.js脚本,它显示了对象是如何检索,然后它是如何在后面的脚本调用。它在收到addUrl消息并尝试在currentGroup上调用containsUrl()时失败。

// Global Variables 
var currentGroup; 
var groups = []; 
var options = []; 

// Short hand function to save the current data to storage 
var saveData = function() { 
    // Save default options, currrentGroup, and 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 for saved data to make sure data is current 
chrome.storage.sync.get(function(items) { 
    // Check if there are groups 
    if (items['groups']) { // Set the groups 
    groups = items['groups']; 
    } else { // Create default group and add to list of groups 
    currentGroup = new Group('default', []); 
    groups = [currentGroup]; 
    } 

    // Check for current group, if none set to first available group 
    if (items['currentGroup']) { 
    currentGroup = items['currentGroup']; 
    console.log(Object.getOwnPropertyNames(currentGroup)); 
    } else { 
    currentGroup = groups[0]; 
    } 

    // Check for the options 
    if (items['options']) { 
    options = items['options']; 
    } else { 
    // No options, set the default options and save them 
    options['overrideHomepages'] = true; 
    } 

    saveData(); 

    // After data has been fetched bring up the tabs 
    chrome.tabs.query({'currentWindow': true}, function(tabs) { 
    for (var i = 0; i < 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 was sent 
    if (request.message === 'addUrl') { 
    console.log('Recieved message: ' + request.message); 
    // Check if the group contains the url already 
    if (currentGroup.containsUrl(sender.url) === false) { 
     currentGroup.addUrl(sender.url); 
     saveData(); 
     sendResponse({'message': 'Saved ' + sender.url}); 
    } 
    } 

    // If remove url was sent 
    if (request.message === 'removeUrl') { 
    // Check if the group contains the url 
    if (currentGroup.containsUrl(sender.url)) { 
     currentGroup.removeUrl(sender.url); 
     saveData(); 
     sendResponse({'message': 'Removed ' + sender.url}) 
    } 
    } 
}); 
+0

对你的问题不太清楚,你能否提供更多的代码,比如你如何使用Group.js? –

+0

我编辑了这篇文章,并添加了background.js脚本以提供更多上下文 – SamG

+0

什么是错误信息?当你调用'currentGroup.containsUrl'时,'currentGroup'是否已经初始化?消息何时会发送到后台页面? –

回答

1

我相信目前chrome.storage仅用于保存键值项,不包括原型/功能。但是,我没有找到任何关于此的官方文档。

一种解决方法是使用Group.prototype.containsUrl.call(currentGroup, sender.url),它允许您调用containsUrl并指定"this"的上下文。

+0

谢谢!这允许调用该函数,并且它似乎正在工作到目前为止。我花了几个小时试图弄清楚这一点,非常感谢! – SamG

+0

@SamG,很高兴帮助,但是,我不确定这一点,因为'chrome.storage'可以用来保存对象,但似乎只支持key-value。我们可以拭目以待,看看别人能否给出更好的解释。 –

+0

现在我会留下您的答案作为正确的答案,因为它是迄今为止唯一对我有用的解决方案,如果有更好的解决方案出现,我会更新!再次感谢,现在我已经能够取得进展,我已经超过了这个错误 – SamG