bookmarks.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 singlefile, browser */
  24. singlefile.extension.core.bg.bookmarks = (() => {
  25. enable();
  26. return {
  27. onMessage,
  28. saveCreatedBookmarks: enable,
  29. disable,
  30. update: async (id, changes) => {
  31. try {
  32. await browser.bookmarks.update(id, changes);
  33. } catch (error) {
  34. // ignored
  35. }
  36. }
  37. };
  38. function onMessage(message) {
  39. if (message.method.endsWith(".saveCreatedBookmarks")) {
  40. enable();
  41. return {};
  42. }
  43. if (message.method.endsWith(".disable")) {
  44. disable();
  45. return {};
  46. }
  47. }
  48. async function enable() {
  49. try {
  50. browser.bookmarks.onCreated.removeListener(onCreated);
  51. } catch (error) {
  52. // ignored
  53. }
  54. let enabled;
  55. const profiles = await singlefile.extension.core.bg.config.getProfiles();
  56. Object.keys(profiles).forEach(profileName => {
  57. if (profiles[profileName].saveCreatedBookmarks) {
  58. enabled = true;
  59. }
  60. });
  61. if (enabled) {
  62. browser.bookmarks.onCreated.addListener(onCreated);
  63. }
  64. }
  65. async function disable() {
  66. let disabled;
  67. const profiles = await singlefile.extension.core.bg.config.getProfiles();
  68. Object.keys(profiles).forEach(profileName => disabled = disabled || !profiles[profileName].saveCreatedBookmarks);
  69. if (disabled) {
  70. browser.bookmarks.onCreated.removeListener(onCreated);
  71. }
  72. }
  73. async function onCreated(bookmarkId, bookmarkInfo) {
  74. const tabs = await singlefile.extension.core.bg.tabs.get({ lastFocusedWindow: true, active: true });
  75. const options = await singlefile.extension.core.bg.config.getOptions(bookmarkInfo.url);
  76. if (options.saveCreatedBookmarks) {
  77. const bookmarkFolders = await getParentFolders(bookmarkInfo.parentId);
  78. const ignoredBookmark = bookmarkFolders.find(folder => options.ignoredBookmarkFolders.includes(folder));
  79. if (!ignoredBookmark) {
  80. if (tabs.length && tabs[0].url == bookmarkInfo.url) {
  81. singlefile.extension.core.bg.business.saveTabs(tabs, { bookmarkId });
  82. } else {
  83. const tabs = await singlefile.extension.core.bg.tabs.get({});
  84. if (tabs.length) {
  85. const tab = tabs.find(tab => tab.url == bookmarkInfo.url);
  86. if (tab) {
  87. singlefile.extension.core.bg.business.saveTabs([tab], { bookmarkId });
  88. } else {
  89. if (bookmarkInfo.url) {
  90. if (bookmarkInfo.url == "about:blank") {
  91. browser.bookmarks.onChanged.addListener(onChanged);
  92. } else {
  93. saveUrl(bookmarkInfo.url);
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }
  101. async function getParentFolders(id, folderNames = []) {
  102. if (id) {
  103. const bookmarkNode = (await browser.bookmarks.get(id))[0];
  104. if (bookmarkNode && bookmarkNode.title) {
  105. folderNames.unshift(bookmarkNode.title);
  106. await getParentFolders(bookmarkNode.parentId, folderNames);
  107. }
  108. }
  109. return folderNames;
  110. }
  111. function onChanged(id, changeInfo) {
  112. if (id == bookmarkId && changeInfo.url) {
  113. browser.bookmarks.onChanged.removeListener(onChanged);
  114. saveUrl(changeInfo.url);
  115. }
  116. }
  117. function saveUrl(url) {
  118. singlefile.extension.core.bg.business.saveUrls([url], { bookmarkId });
  119. }
  120. }
  121. })();