ui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2018 Gildas Lormeau
  3. * contact : gildas.lormeau <at> gmail.com
  4. *
  5. * This file is part of SingleFile.
  6. *
  7. * SingleFile is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * SingleFile 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
  15. * GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with SingleFile. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /* global singlefile, navigator */
  21. singlefile.ui = (() => {
  22. const browser = this.browser || this.chrome;
  23. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_19.png";
  24. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_19_wait";
  25. const DEFAULT_TITLE = "Process this page with SingleFile";
  26. const DEFAULT_COLOR = [2, 147, 20, 255];
  27. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  28. const RUNNING_IN_EDGE = navigator.userAgent.includes("Edge");
  29. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  30. const MENU_ID_SAVE_PAGE = "save-page";
  31. const MENU_ID_SAVE_SELECTED = "save-selected";
  32. const tabs = {};
  33. const badgeTabs = {};
  34. let badgeRefreshPending = [];
  35. browser.runtime.onInstalled.addListener(refreshContextMenu);
  36. browser.contextMenus.onClicked.addListener((event, tab) => {
  37. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  38. processTab(tab);
  39. }
  40. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  41. processTab(tab, { selected: true });
  42. }
  43. });
  44. browser.browserAction.onClicked.addListener(tab => {
  45. if (isAllowedURL(tab.url)) {
  46. browser.tabs.query({ currentWindow: true, highlighted: true }, tabs => {
  47. if (!tabs.length) {
  48. browser.tabs.query({ currentWindow: true, active: true }, tabs => tabs.forEach(processTab));
  49. } else {
  50. tabs.forEach(processTab);
  51. }
  52. });
  53. }
  54. });
  55. browser.tabs.onActivated.addListener(activeInfo => browser.tabs.get(activeInfo.tabId, tab => onActive(tab.id, isAllowedURL(tab.url))));
  56. browser.tabs.onCreated.addListener(tab => onActive(tab.id, isAllowedURL(tab.url)));
  57. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onActive(tab.id, isAllowedURL(tab.url)));
  58. browser.tabs.onRemoved.addListener(tabId => onRemoved(tabId));
  59. browser.runtime.onMessage.addListener((request, sender) => {
  60. if (request.processStart || request.processProgress) {
  61. onProgress(sender.tab.id, request.index, request.maxIndex);
  62. }
  63. if (request.processEnd) {
  64. onEnd(sender.tab.id);
  65. }
  66. if (request.processError) {
  67. onError(sender.tab.id);
  68. }
  69. return false;
  70. });
  71. return { update: refreshContextMenu };
  72. function refreshContextMenu() {
  73. const contextMenuEnabled = singlefile.config.get().contextMenuEnabled;
  74. if (contextMenuEnabled) {
  75. browser.contextMenus.create({
  76. id: MENU_ID_SAVE_PAGE,
  77. contexts: ["page"],
  78. title: "Save page with SingleFile"
  79. });
  80. browser.contextMenus.create({
  81. id: MENU_ID_SAVE_SELECTED,
  82. contexts: ["selection"],
  83. title: "Save selection with SingleFile"
  84. });
  85. } else {
  86. browser.contextMenus.removeAll();
  87. }
  88. }
  89. function processTab(tab) {
  90. const tabId = tab.id;
  91. singlefile.core.processTab(tab);
  92. tabs[tabId] = {
  93. id: tabId,
  94. text: "...",
  95. color: DEFAULT_COLOR,
  96. title: "initializing...",
  97. path: DEFAULT_ICON_PATH,
  98. progress: -1,
  99. barProgress: -1
  100. };
  101. refreshBadge(tabId);
  102. }
  103. function onError(tabId) {
  104. const tabData = tabs[tabId];
  105. tabData.text = "ERR";
  106. tabData.color = [229, 4, 12, 255];
  107. tabData.title = DEFAULT_TITLE;
  108. tabData.path = DEFAULT_ICON_PATH;
  109. tabData.progress = -1;
  110. tabData.barProgress = -1;
  111. refreshBadge(tabId);
  112. }
  113. function onEnd(tabId) {
  114. const tabData = tabs[tabId];
  115. tabData.text = "OK";
  116. tabData.color = [4, 229, 36, 255];
  117. tabData.title = DEFAULT_TITLE;
  118. tabData.path = DEFAULT_ICON_PATH;
  119. tabData.progress = -1;
  120. tabData.barProgress = -1;
  121. refreshBadge(tabId);
  122. }
  123. function onProgress(tabId, index, maxIndex) {
  124. const tabData = tabs[tabId];
  125. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  126. if (tabData.progress != progress) {
  127. tabData.progress = progress;
  128. tabData.text = "";
  129. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  130. tabData.color = [4, 229, 36, 255];
  131. const barProgress = Math.floor((index / maxIndex) * 15);
  132. if (tabData.barProgress != barProgress) {
  133. tabData.barProgress = barProgress;
  134. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  135. }
  136. refreshBadge(tabId);
  137. }
  138. }
  139. function onRemoved(tabId) {
  140. delete tabs[tabId];
  141. }
  142. function onActive(tabId, isActive) {
  143. if (isActive) {
  144. browser.browserAction.enable(tabId);
  145. } else {
  146. browser.browserAction.disable(tabId);
  147. }
  148. }
  149. function isAllowedURL(url) {
  150. return (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  151. }
  152. async function refreshBadge(tabId) {
  153. await Promise.all(badgeRefreshPending);
  154. const promise = refreshBadgeAsync(tabId);
  155. badgeRefreshPending.push(promise);
  156. await promise;
  157. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  158. }
  159. async function refreshBadgeAsync(tabId) {
  160. for (let property of BADGE_PROPERTIES) {
  161. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  162. }
  163. }
  164. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  165. const tabData = tabs[tabId];
  166. const value = tabData[property];
  167. if (!badgeTabs[tabId]) {
  168. badgeTabs[tabId] = {};
  169. }
  170. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  171. const browserActionParameter = { tabId };
  172. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  173. return new Promise(resolve => {
  174. if (RUNNING_IN_EDGE) {
  175. browser.browserAction[browserActionMethod](browserActionParameter);
  176. resolve();
  177. } else {
  178. browser.browserAction[browserActionMethod](browserActionParameter, resolve);
  179. }
  180. });
  181. }
  182. }
  183. })();