tabs-util.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. const pendingPrompts = new Map();
  25. export {
  26. onPromptValueResponse,
  27. queryTabs,
  28. promptValue,
  29. extractAuthCode,
  30. launchWebAuthFlow
  31. };
  32. async function onPromptValueResponse(message, sender) {
  33. const promptPromise = pendingPrompts.get(sender.tab.id);
  34. if (promptPromise) {
  35. promptPromise.resolve(message.value);
  36. pendingPrompts.delete(sender.tab.id);
  37. }
  38. }
  39. async function queryTabs(options) {
  40. const tabs = await browser.tabs.query(options);
  41. return tabs.sort((tab1, tab2) => tab1.index - tab2.index);
  42. }
  43. async function promptValue(promptMessage) {
  44. const tabs = await browser.tabs.query({ currentWindow: true, active: true });
  45. return new Promise((resolve, reject) => {
  46. const selectedTabId = tabs[0].id;
  47. browser.tabs.onRemoved.addListener(onTabRemoved);
  48. pendingPrompts.set(selectedTabId, { resolve, reject });
  49. browser.tabs.sendMessage(selectedTabId, { method: "common.promptValueRequest", promptMessage });
  50. function onTabRemoved(tabId) {
  51. if (tabId == selectedTabId) {
  52. pendingPrompts.delete(tabId);
  53. browser.tabs.onUpdated.removeListener(onTabRemoved);
  54. reject();
  55. }
  56. }
  57. });
  58. }
  59. function extractAuthCode(authURL) {
  60. return new Promise((resolve, reject) => {
  61. let authTabId;
  62. browser.tabs.onUpdated.addListener(onTabUpdated);
  63. browser.tabs.onRemoved.addListener(onTabRemoved);
  64. function onTabUpdated(tabId, changeInfo) {
  65. if (changeInfo && changeInfo.url == authURL) {
  66. authTabId = tabId;
  67. }
  68. if (authTabId == tabId && changeInfo && changeInfo.title && changeInfo.title.startsWith("Success code=")) {
  69. browser.tabs.onUpdated.removeListener(onTabUpdated);
  70. browser.tabs.onUpdated.removeListener(onTabRemoved);
  71. resolve(changeInfo.title.substring(13, changeInfo.title.length - 49));
  72. }
  73. }
  74. function onTabRemoved(tabId) {
  75. if (tabId == authTabId) {
  76. browser.tabs.onUpdated.removeListener(onTabUpdated);
  77. browser.tabs.onUpdated.removeListener(onTabRemoved);
  78. reject();
  79. }
  80. }
  81. });
  82. }
  83. async function launchWebAuthFlow(options) {
  84. const tab = await browser.tabs.create({ url: options.url, active: true });
  85. return new Promise((resolve, reject) => {
  86. browser.tabs.onRemoved.addListener(onTabRemoved);
  87. function onTabRemoved(tabId) {
  88. if (tabId == tab.id) {
  89. browser.tabs.onRemoved.removeListener(onTabRemoved);
  90. reject(new Error("code_required"));
  91. }
  92. }
  93. });
  94. }