bookmarks.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2010-2020 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * The code in this file is free software: you can redistribute it and/or
  8. * modify it under the terms of the GNU Affero General Public License
  9. * (GNU AGPL) as published by the Free Software Foundation, either version 3
  10. * of the License, or (at your option) any later version.
  11. *
  12. * The code in this file is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
  15. * General Public License for more details.
  16. *
  17. * As additional permission under GNU AGPL version 3 section 7, you may
  18. * distribute UNMODIFIED VERSIONS OF THIS file without the copy of the GNU
  19. * AGPL normally required by section 4, provided you include this license
  20. * notice and a URL through which recipients can access the Corresponding
  21. * Source.
  22. */
  23. /* global extension, browser */
  24. extension.core.bg.bookmarks = (() => {
  25. enable();
  26. return {
  27. onMessage,
  28. saveCreatedBookmarks: enable,
  29. disable,
  30. update
  31. };
  32. async function onMessage(message) {
  33. if (message.method.endsWith(".saveCreatedBookmarks")) {
  34. enable();
  35. return {};
  36. }
  37. if (message.method.endsWith(".disable")) {
  38. disable();
  39. return {};
  40. }
  41. }
  42. async function enable() {
  43. try {
  44. browser.bookmarks.onCreated.removeListener(onCreated);
  45. } catch (error) {
  46. // ignored
  47. }
  48. let enabled;
  49. const profiles = await extension.core.bg.config.getProfiles();
  50. Object.keys(profiles).forEach(profileName => {
  51. if (profiles[profileName].saveCreatedBookmarks) {
  52. enabled = true;
  53. }
  54. });
  55. if (enabled) {
  56. browser.bookmarks.onCreated.addListener(onCreated);
  57. }
  58. }
  59. async function disable() {
  60. let disabled;
  61. const profiles = await extension.core.bg.config.getProfiles();
  62. Object.keys(profiles).forEach(profileName => disabled = disabled || !profiles[profileName].saveCreatedBookmarks);
  63. if (disabled) {
  64. browser.bookmarks.onCreated.removeListener(onCreated);
  65. }
  66. }
  67. async function update(id, changes) {
  68. try {
  69. await browser.bookmarks.update(id, changes);
  70. } catch (error) {
  71. // ignored
  72. }
  73. }
  74. async function onCreated(bookmarkId, bookmarkInfo) {
  75. const tabs = await extension.core.bg.tabs.get({ lastFocusedWindow: true, active: true });
  76. const options = await extension.core.bg.config.getOptions(bookmarkInfo.url);
  77. if (options.saveCreatedBookmarks) {
  78. const bookmarkFolders = await getParentFolders(bookmarkInfo.parentId);
  79. const ignoredBookmark = bookmarkFolders.find(folder => options.ignoredBookmarkFolders.includes(folder));
  80. if (!ignoredBookmark) {
  81. if (tabs.length && tabs[0].url == bookmarkInfo.url) {
  82. extension.core.bg.business.saveTabs(tabs, { bookmarkId, bookmarkFolders });
  83. } else {
  84. const tabs = await extension.core.bg.tabs.get({});
  85. if (tabs.length) {
  86. const tab = tabs.find(tab => tab.url == bookmarkInfo.url);
  87. if (tab) {
  88. extension.core.bg.business.saveTabs([tab], { bookmarkId, bookmarkFolders });
  89. } else {
  90. if (bookmarkInfo.url) {
  91. if (bookmarkInfo.url == "about:blank") {
  92. browser.bookmarks.onChanged.addListener(onChanged);
  93. } else {
  94. saveUrl(bookmarkInfo.url);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. async function getParentFolders(id, folderNames = []) {
  103. if (id) {
  104. const bookmarkNode = (await browser.bookmarks.get(id))[0];
  105. if (bookmarkNode && bookmarkNode.title) {
  106. folderNames.unshift(bookmarkNode.title);
  107. await getParentFolders(bookmarkNode.parentId, folderNames);
  108. }
  109. }
  110. return folderNames;
  111. }
  112. function onChanged(id, changeInfo) {
  113. if (id == bookmarkId && changeInfo.url) {
  114. browser.bookmarks.onChanged.removeListener(onChanged);
  115. saveUrl(changeInfo.url);
  116. }
  117. }
  118. function saveUrl(url) {
  119. extension.core.bg.business.saveUrls([url], { bookmarkId });
  120. }
  121. }
  122. })();