2016-04-25 20 views
0

私の拡張機能で使用しているカスタムオブジェクトを作成しました。グループ(オブジェクトタイプ)タイプのオブジェクトを保存した後、それらのオブジェクトをストレージから取り出すと、プロトタイプメソッドが存在しなくなったように見えます。オブジェクトはオブジェクトリテラル{}にシリアル化され、オブジェクトのメソッドを保持する方法を理解できていないようです。私は以下のグループクラスのコードを提供しました。ストレージから取得したオブジェクトの下のファイルからメソッドの1つを試してみると、その関数が存在しないというエラーが出ます。 forループを使用してすべてのプロパティをループし、オブジェクトにはnameプロパティとurlsプロパティがあります。どんな助けでも大歓迎です!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; 
}; 

EDIT:

ここ

が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のみプロトタイプ/機能を含まないキーと値の項目を、保存するために使用されると信じています。しかし、私はこれに関する公式文書を見つけられませんでした。

回避策の1つはGroup.prototype.containsUrl.call(currentGroup, sender.url)です。containsUrlを呼び出して、"this"のコンテキストを指定することができます。

+0

ありがとうございました!これにより、関数を呼び出すことができ、これまでのところ動作しているように見えます。私はこれを理解しようと数時間を費やし、非常に感謝しています! – SamG

+0

@SamG、嬉しいことですが、 'chrome.storage'を使ってオブジェクトを保存することができますが、キー値だけがサポートされているようです。私たちは誰かがより良い説明をすることができるかどうかを待つことができます。 –

+0

今のところ私はあなたの答えを正しいものとして残しておきます。なぜなら、これまでの私にとっては唯一の解決策だからです。もう一度ありがとう、私はそのバグを過ぎているので、進歩を遂げることができました – SamG

関連する問題