/services/cloudsync/docs/example.rst

https://github.com/rillian/firefox · ReStructuredText · 132 lines · 117 code · 15 blank · 0 comment · 0 complexity · 367150def2e5f06ad210ede3e66f47e9 MD5 · raw file

  1. .. _cloudsync_example:
  2. =======
  3. Example
  4. =======
  5. .. code-block:: javascript
  6. Cu.import("resource://gre/modules/CloudSync.jsm");
  7. let HelloWorld = {
  8. onLoad: function() {
  9. let cloudSync = CloudSync();
  10. console.log("CLOUDSYNC -- hello world", cloudSync.local.id, cloudSync.local.name, cloudSync.adapters);
  11. cloudSync.adapters.register('helloworld', {});
  12. console.log("CLOUDSYNC -- " + JSON.stringify(cloudSync.adapters.getAdapterNames()));
  13. cloudSync.tabs.addEventListener("change", function() {
  14. console.log("tab change");
  15. cloudSync.tabs.getLocalTabs().then(
  16. function(records) {
  17. console.log(JSON.stringify(records));
  18. }
  19. );
  20. });
  21. cloudSync.tabs.getLocalTabs().then(
  22. function(records) {
  23. console.log(JSON.stringify(records));
  24. }
  25. );
  26. let remoteClient = {
  27. id: "001",
  28. name: "FakeClient",
  29. };
  30. let remoteTabs1 = [
  31. {url:"https://www.google.ca",title:"Google",icon:"https://www.google.ca/favicon.ico",lastUsed:Date.now()},
  32. ];
  33. let remoteTabs2 = [
  34. {url:"https://www.google.ca",title:"Google Canada",icon:"https://www.google.ca/favicon.ico",lastUsed:Date.now()},
  35. {url:"http://www.reddit.com",title:"Reddit",icon:"http://www.reddit.com/favicon.ico",lastUsed:Date.now()},
  36. ];
  37. cloudSync.tabs.mergeRemoteTabs(remoteClient, remoteTabs1).then(
  38. function() {
  39. return cloudSync.tabs.mergeRemoteTabs(remoteClient, remoteTabs2);
  40. }
  41. ).then(
  42. function() {
  43. return cloudSync.tabs.getRemoteTabs();
  44. }
  45. ).then(
  46. function(tabs) {
  47. console.log("remote tabs:", tabs);
  48. }
  49. );
  50. cloudSync.bookmarks.getRootFolder("Hello World").then(
  51. function(rootFolder) {
  52. console.log(rootFolder.name, rootFolder.id);
  53. rootFolder.addEventListener("add", function(guid) {
  54. console.log("CLOUDSYNC -- bookmark item added: " + guid);
  55. rootFolder.getLocalItemsById([guid]).then(
  56. function(items) {
  57. console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
  58. }
  59. );
  60. });
  61. rootFolder.addEventListener("remove", function(guid) {
  62. console.log("CLOUDSYNC -- bookmark item removed: " + guid);
  63. rootFolder.getLocalItemsById([guid]).then(
  64. function(items) {
  65. console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
  66. }
  67. );
  68. });
  69. rootFolder.addEventListener("change", function(guid) {
  70. console.log("CLOUDSYNC -- bookmark item changed: " + guid);
  71. rootFolder.getLocalItemsById([guid]).then(
  72. function(items) {
  73. console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
  74. }
  75. );
  76. });
  77. rootFolder.addEventListener("move", function(guid) {
  78. console.log("CLOUDSYNC -- bookmark item moved: " + guid);
  79. rootFolder.getLocalItemsById([guid]).then(
  80. function(items) {
  81. console.log("CLOUDSYNC -- items: " + JSON.stringify(items));
  82. }
  83. );
  84. });
  85. function logLocalItems() {
  86. return rootFolder.getLocalItems().then(
  87. function(items) {
  88. console.log("CLOUDSYNC -- local items: " + JSON.stringify(items));
  89. }
  90. );
  91. }
  92. let items = [
  93. {"id":"9fdoci2KOME6","type":rootFolder.FOLDER,"parent":rootFolder.id,"title":"My Bookmarks 1"},
  94. {"id":"1fdoci2KOME5","type":rootFolder.FOLDER,"parent":rootFolder.id,"title":"My Bookmarks 2"},
  95. {"id":"G_UL4ZhOyX8m","type":rootFolder.BOOKMARK,"parent":"1fdoci2KOME5","title":"reddit: the front page of the internet","uri":"http://www.reddit.com/"},
  96. ];
  97. function mergeSomeItems() {
  98. return rootFolder.mergeRemoteItems(items);
  99. }
  100. logLocalItems().then(
  101. mergeSomeItems
  102. ).then(
  103. function(processedItems) {
  104. console.log("!!!", processedItems);
  105. console.log("merge complete");
  106. },
  107. function(error) {
  108. console.log("merge failed:", error);
  109. }
  110. ).then(
  111. logLocalItems
  112. );
  113. }
  114. );
  115. },
  116. };
  117. window.addEventListener("load", function(e) { HelloWorld.onLoad(e); }, false);