tabs.js 6.3 KB

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