Chrome セッション全体で保持する必要があるブックマークのタグ付けシステムの情報を含むオブジェクトを使用しているため、それをローカル ストレージに保存し、新しいブックマークが作成されるたびに更新しようとしています。
新しいブックマークを作成するとき、関数を起動して、新しいブックマークと同じタグを持つ他のブックマークがあるかどうかを確認します。これにより、動的フォルダーのような機能を果たす「タグ グループ」にブックマークが整理されます。
ストレージ オブジェクトを設定すると、格納されているオブジェクトには期待するすべてのデータが含まれています。しかし、同じオブジェクトをストレージから取り出すとすぐに、ネストされたオブジェクトの 1 つが不思議なことに に変わりますnull
。コンソール出力を参照してください: top オブジェクトは、 function の set 呼び出しの直前updateStorage
です。一番下は、ストレージからそのオブジェクトを「取得」したときに返されるものです。tagGroups ブックマークが null になっていることに注意してください。ブックマーク自体はまだそこにあり、消えるのはタグ グループ オブジェクトだけです。私はこれを機能させるために、昼夜を問わずこれをいじりました。
モデルコードはこちら。コンテキストのためにすべてを含めましたが、最も関連性の高い部分は、createNewBookmark、updatePrimaryTreeWithTagGroups、および updateStorage メソッドです。
UPDATE : コードを編集して、ストレージから何かを設定/取得する前に、ブックマーク ツリーにすべての変更を加えてから、結果のオブジェクトでストレージを更新するための最後の呼び出しを行いました。私は文字通り、あるものを一度保存し、取得しようとするたびに別のものを取得しています。
function PrimaryBookmarksTree(){
chrome.storage.sync.get(null, this.findOrCreate.bind(this));
}
PrimaryBookmarksTree.prototype.findOrCreate = function(result){
if (result.bookmarksTree != undefined){
this.bookmarks = result.bookmarksTree.bookmarks;
this.title = result.bookmarksTree.title;
this.tagGroups = result.bookmarksTree.tagGroups;
console.log(this);
} else {
this.bookmarks = [];
this.title = "Marinade Bookmarks";
this.tagGroups = [];
chrome.storage.sync.set({"bookmarksTree": this}, function(){console.log("New tree created!")});
console.log(this);
}
}
function Bookmark(name, tags, url){
this.name = name;
this.tags = tags;
this.url = url;
this.dateCreated = this.date();
}
function TagGroup(tag){
this.bookmarks = [];
this.tag = tag;
}
//called by controller when user tags a new bookmark via the extension
PrimaryBookmarksTree.prototype.createNewBookmark = function(name, tags, url){
var newBookmark = new Bookmark(name, tags, url);
this.bookmarks.push(newBookmark);
this.tagGroups = this.updatePrimaryTreeWithTagGroups();
this.updateStorage(this);
}
PrimaryBookmarksTree.prototype.updatePrimaryTreeWithTagGroups = function(){
var tagsForGrouping = this.getTagsWithMultipleBookmarks(this.bookmarks);
for(j=0;j<tagsForGrouping.length;j++){
this.tagGroups.push(this.buildTagGroup(tagsForGrouping[j]));
}
return this.tagGroups;
}
PrimaryBookmarksTree.prototype.getTagsWithMultipleBookmarks = function(bookmarks){
var tagsToCheck = this.pluck(bookmarks, "tags");
var tagCounts = tagsToCheck.reduce(function (obj, curr){
if (typeof obj[curr] == 'undefined') {
obj[curr] = 1;
} else {
obj[curr] += 1;
}
return obj;
}, {});
var tagGroups = this.filter(tagCounts, function(x){return x > 1});
return tagGroups;
}
PrimaryBookmarksTree.prototype.buildTagGroup = function(tag){
tagGroup = new TagGroup(tag);
for(i=0;i<this.bookmarks.length;i++){
if(this.bookmarks[i].tags[0] == tag){
tagGroup.bookmarks.push(this.bookmarks[i]);
}
}
if (tagGroup.bookmarks.length != 0){
return tagGroup;
}
}
PrimaryBookmarksTree.prototype.updateStorage = function(updatedTree){
console.log(JSON.stringify(updatedTree));
chrome.storage.sync.set({"bookmarksTree": updatedTree}, function(){console.log("final storage complete")});
}