summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/extensions/bookmarks/bookmark_api.html
blob: ba8084e6e53912516d7e43396b273005de415efc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<html>
<link rel="stylesheet" type="text/css" href="extensions_toolstrip.css">
<script>

var dump = function(obj, indent) {
  if (indent === undefined)
    indent = "";
  if (typeof obj == "object") {
    var ret = "{<br/>";
    var child_indent = indent + "  ";
    for (var item in obj) {
      var child = null;
      try {
        child = obj[item];
      } catch (e) {
        child = '<error>';
      }
      ret += child_indent + item + ": " + dump(child, indent + "  ");
    }
    return ret + indent + "}" + "<br/>";
  } else {
    return obj.toString() + "<br/>";
  }
}

var testMoveBookmarks = function(event) {
  if (event.shiftKey) {
    // TODO - it would be nice to have a mechanism to do this built-in to a
    // context menu.
    window.location.reload();
    return;
  }
  console.log("testMoveBookmarks");
  chromium.bookmarks.get([], function(root) {
    chromium.bookmarks.get(root[0].childrenIds, function(root_children) {
      var bookmark_bar = root_children[0];  // bookmarks bar is always first
      chromium.bookmarks.get(bookmark_bar.childrenIds,
                                 function(bar_children) {
        var folder_search = [];
        bar_children.forEach(function(child) {
          if (child.title == "folder" && child.url == undefined) {
            folder_search.push(child);
          }
        });
        if (folder_search.length == 1) {
          console.log('moving children out of "folder"');
          var folder = folder_search[0];
          folder.childrenIds.forEach(function(folder_child_id) {
            chromium.bookmarks.move({'id': folder_child_id,
                                     'parentId': bookmark_bar.id});
          });
          chromium.bookmarks.remove({'id': folder.id});
        } else if (folder_search.length == 0) {
          console.log('creating "folder" and moving children into it');
          chromium.bookmarks.create({'parentId': bookmark_bar.id, 
                                     'title': 'folder'},
                                      function(folder) {
            chromium.bookmarks.search("oog", function(oog_search) {
              oog_search.forEach(function(oog_match) {
                chromium.bookmarks.move({'id': oog_match.id,
                                         'parentId': folder.id})
              });
            });
          });
        } else {
          console.log("my puny code wasn't written to handle this");
        }
      });
    });
  });
};

var dumpBookmarks = function(event) {
  window.open("bookmark_view.html");
  /*
    console.dir(results);
    var win = window.open();
    win.document.write("<html><body><pre>");
    win.document.write(dump(results));
    win.document.write("</pre></body></html>");
    win.document.title = "Bookmarks";
  });
  */
};
</script>
<body>
<div class="toolstrip-button" onclick="dumpBookmarks(window.event);">
<span>Dump Bookmarks</span>
</div>
<div class="toolstrip-button" onclick="testMoveBookmarks(window.event);">
<span>Test Move</span>
</div>
</body>
</html>