bookmarks.js 4.1 KB

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