bookmarks.js 3.4 KB

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