tabs.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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, setTimeout */
  24. import * as config from "./config.js";
  25. import * as autosave from "./autosave.js";
  26. import * as business from "./business.js";
  27. import * as editor from "./editor.js";
  28. import * as tabsData from "./tabs-data.js";
  29. import * as ui from "./../../ui/bg/index.js";
  30. const DELAY_MAYBE_INIT = 1500;
  31. const pendingPrompts = new Map();
  32. browser.tabs.onCreated.addListener(tab => onTabCreated(tab));
  33. browser.tabs.onActivated.addListener(activeInfo => onTabActivated(activeInfo));
  34. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  35. browser.tabs.onUpdated.addListener((tabId, changeInfo) => onTabUpdated(tabId, changeInfo));
  36. browser.tabs.onReplaced.addListener((addedTabId, removedTabId) => onTabReplaced(addedTabId, removedTabId));
  37. export {
  38. onMessage,
  39. get,
  40. create,
  41. createAndWait,
  42. sendMessage,
  43. update,
  44. remove,
  45. promptValue,
  46. extractAuthCode,
  47. launchWebAuthFlow
  48. };
  49. async function onMessage(message, sender) {
  50. if (message.method.endsWith(".init")) {
  51. await onInit(sender.tab, message);
  52. ui.onInit(sender.tab);
  53. business.onInit(sender.tab);
  54. autosave.onInit(sender.tab);
  55. }
  56. if (message.method.endsWith(".promptValueResponse")) {
  57. const promptPromise = pendingPrompts.get(sender.tab.id);
  58. if (promptPromise) {
  59. promptPromise.resolve(message.value);
  60. pendingPrompts.delete(sender.tab.id);
  61. }
  62. }
  63. if (message.method.endsWith(".getOptions")) {
  64. return config.getOptions(message.url);
  65. }
  66. if (message.method.endsWith(".activate")) {
  67. await browser.tabs.update(message.tabId, { active: true });
  68. }
  69. }
  70. function sendMessage(tabId, message, options) {
  71. return browser.tabs.sendMessage(tabId, message, options);
  72. }
  73. function update(tabId, updateProperties) {
  74. return browser.tabs.update(tabId, updateProperties);
  75. }
  76. function remove(tabId) {
  77. return browser.tabs.remove(tabId);
  78. }
  79. function create(createProperties) {
  80. return browser.tabs.create(createProperties);
  81. }
  82. async function createAndWait(createProperties) {
  83. const tab = await browser.tabs.create(createProperties);
  84. return new Promise((resolve, reject) => {
  85. browser.tabs.onUpdated.addListener(onTabUpdated);
  86. browser.tabs.onRemoved.addListener(onTabRemoved);
  87. function onTabUpdated(tabId, changeInfo) {
  88. if (tabId == tab.id && changeInfo.status == "complete") {
  89. resolve(tab);
  90. browser.tabs.onUpdated.removeListener(onTabUpdated);
  91. browser.tabs.onRemoved.removeListener(onTabRemoved);
  92. }
  93. }
  94. function onTabRemoved(tabId) {
  95. if (tabId == tab.id) {
  96. reject(tabId);
  97. browser.tabs.onRemoved.removeListener(onTabRemoved);
  98. }
  99. }
  100. });
  101. }
  102. async function get(options) {
  103. if (options.id) {
  104. return browser.tabs.get(options.id);
  105. } else {
  106. const tabs = await browser.tabs.query(options);
  107. return tabs.sort((tab1, tab2) => tab1.index - tab2.index);
  108. }
  109. }
  110. async function promptValue(promptMessage) {
  111. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  112. return new Promise((resolve, reject) => {
  113. const selectedTabId = tabs[0].id;
  114. browser.tabs.onRemoved.addListener(onTabRemoved);
  115. pendingPrompts.set(selectedTabId, { resolve, reject });
  116. browser.tabs.sendMessage(selectedTabId, { method: "common.promptValueRequest", promptMessage });
  117. function onTabRemoved(tabId) {
  118. if (tabId == selectedTabId) {
  119. pendingPrompts.delete(tabId);
  120. browser.tabs.onUpdated.removeListener(onTabRemoved);
  121. reject();
  122. }
  123. }
  124. });
  125. }
  126. function extractAuthCode(authURL) {
  127. return new Promise((resolve, reject) => {
  128. let authTabId;
  129. browser.tabs.onUpdated.addListener(onTabUpdated);
  130. browser.tabs.onRemoved.addListener(onTabRemoved);
  131. function onTabUpdated(tabId, changeInfo) {
  132. if (changeInfo && changeInfo.url == authURL) {
  133. authTabId = tabId;
  134. }
  135. if (authTabId == tabId && changeInfo && changeInfo.title && changeInfo.title.startsWith("Success code=")) {
  136. browser.tabs.onUpdated.removeListener(onTabUpdated);
  137. browser.tabs.onUpdated.removeListener(onTabRemoved);
  138. resolve(changeInfo.title.substring(13, changeInfo.title.length - 49));
  139. }
  140. }
  141. function onTabRemoved(tabId) {
  142. if (tabId == authTabId) {
  143. browser.tabs.onUpdated.removeListener(onTabUpdated);
  144. browser.tabs.onUpdated.removeListener(onTabRemoved);
  145. reject();
  146. }
  147. }
  148. });
  149. }
  150. async function launchWebAuthFlow(options) {
  151. const tab = await browser.tabs.create({ url: options.url, active: true });
  152. return new Promise((resolve, reject) => {
  153. browser.tabs.onRemoved.addListener(onTabRemoved);
  154. function onTabRemoved(tabId) {
  155. if (tabId == tab.id) {
  156. browser.tabs.onRemoved.removeListener(onTabRemoved);
  157. reject(new Error("code_required"));
  158. }
  159. }
  160. });
  161. }
  162. async function onInit(tab, options) {
  163. await tabsData.remove(tab.id);
  164. const allTabsData = await tabsData.get(tab.id);
  165. allTabsData[tab.id].savedPageDetected = options.savedPageDetected;
  166. await tabsData.set(allTabsData);
  167. }
  168. async function onTabUpdated(tabId, changeInfo) {
  169. if (changeInfo.status == "complete") {
  170. setTimeout(async () => {
  171. try {
  172. await browser.tabs.sendMessage(tabId, { method: "content.maybeInit" });
  173. }
  174. catch (error) {
  175. // ignored
  176. }
  177. }, DELAY_MAYBE_INIT);
  178. autosave.onTabUpdated(tabId);
  179. const tab = await browser.tabs.get(tabId);
  180. if (editor.isEditor(tab)) {
  181. const allTabsData = await tabsData.get(tab.id);
  182. allTabsData[tab.id].editorDetected = true;
  183. await tabsData.set(allTabsData);
  184. ui.onTabActivated(tab);
  185. }
  186. }
  187. if (changeInfo.discarded) {
  188. autosave.onTabDiscarded(tabId);
  189. }
  190. }
  191. function onTabReplaced(addedTabId, removedTabId) {
  192. autosave.onTabReplaced(addedTabId, removedTabId);
  193. }
  194. function onTabCreated(tab) {
  195. ui.onTabCreated(tab);
  196. }
  197. async function onTabActivated(activeInfo) {
  198. const tab = await browser.tabs.get(activeInfo.tabId);
  199. ui.onTabActivated(tab);
  200. }
  201. function onTabRemoved(tabId) {
  202. tabsData.remove(tabId);
  203. editor.onTabRemoved(tabId);
  204. business.onTabRemoved(tabId);
  205. autosave.onTabRemoved(tabId);
  206. }