ui.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. tabs = tabs.filter(tab => tab.highlighted);
  48. if (!tabs.length) {
  49. processTab(tab);
  50. } else {
  51. tabs.forEach(processTab);
  52. }
  53. });
  54. }
  55. });
  56. browser.tabs.onActivated.addListener(activeInfo => browser.tabs.get(activeInfo.tabId, tab => onTabActivated(tab.id, isAllowedURL(tab.url))));
  57. browser.tabs.onCreated.addListener(tab => onTabActivated(tab.id, isAllowedURL(tab.url)));
  58. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab.id, isAllowedURL(tab.url)));
  59. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  60. browser.runtime.onMessage.addListener((request, sender) => {
  61. if (request.processStart || request.processProgress) {
  62. onTabProgress(sender.tab.id, request.index, request.maxIndex);
  63. }
  64. if (request.processEnd) {
  65. onTabEnd(sender.tab.id);
  66. }
  67. if (request.processError) {
  68. if (request.error) {
  69. console.error("Initialization error", request.error); // eslint-disable-line no-console
  70. }
  71. onTabError(sender.tab.id);
  72. }
  73. return false;
  74. });
  75. return { update: refreshContextMenu };
  76. function refreshContextMenu() {
  77. const contextMenuEnabled = singlefile.config.get().contextMenuEnabled;
  78. if (contextMenuEnabled) {
  79. browser.contextMenus.create({
  80. id: MENU_ID_SAVE_PAGE,
  81. contexts: ["page"],
  82. title: "Save page with SingleFile"
  83. });
  84. browser.contextMenus.create({
  85. id: MENU_ID_SAVE_SELECTED,
  86. contexts: ["selection"],
  87. title: "Save selection with SingleFile"
  88. });
  89. } else {
  90. browser.contextMenus.removeAll();
  91. }
  92. }
  93. function processTab(tab) {
  94. const tabId = tab.id;
  95. singlefile.core.processTab(tab)
  96. .then(() => {
  97. tabs[tabId] = {
  98. id: tabId,
  99. text: "...",
  100. color: DEFAULT_COLOR,
  101. title: "initializing...",
  102. path: DEFAULT_ICON_PATH,
  103. progress: -1,
  104. barProgress: -1
  105. };
  106. refreshBadge(tabId);
  107. })
  108. .catch(() => {
  109. tabs[tabId] = {
  110. id: tabId,
  111. text: "↻",
  112. color: [255, 141, 1, 255],
  113. title: "reload the page",
  114. path: DEFAULT_ICON_PATH,
  115. progress: -1,
  116. barProgress: -1
  117. };
  118. refreshBadge(tabId);
  119. });
  120. }
  121. function onTabError(tabId) {
  122. const tabData = tabs[tabId];
  123. tabData.text = "ERR";
  124. tabData.color = [229, 4, 12, 255];
  125. tabData.title = DEFAULT_TITLE;
  126. tabData.path = DEFAULT_ICON_PATH;
  127. tabData.progress = -1;
  128. tabData.barProgress = -1;
  129. refreshBadge(tabId);
  130. }
  131. function onTabEnd(tabId) {
  132. const tabData = tabs[tabId];
  133. tabData.text = "OK";
  134. tabData.color = [4, 229, 36, 255];
  135. tabData.title = DEFAULT_TITLE;
  136. tabData.path = DEFAULT_ICON_PATH;
  137. tabData.progress = -1;
  138. tabData.barProgress = -1;
  139. refreshBadge(tabId);
  140. }
  141. function onTabProgress(tabId, index, maxIndex) {
  142. const tabData = tabs[tabId];
  143. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  144. if (tabData.progress != progress) {
  145. tabData.progress = progress;
  146. tabData.text = "";
  147. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  148. tabData.color = [4, 229, 36, 255];
  149. const barProgress = Math.floor((index / maxIndex) * 15);
  150. if (tabData.barProgress != barProgress) {
  151. tabData.barProgress = barProgress;
  152. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  153. }
  154. refreshBadge(tabId);
  155. }
  156. }
  157. function onTabRemoved(tabId) {
  158. delete tabs[tabId];
  159. }
  160. function onTabActivated(tabId, isActive) {
  161. if (isActive) {
  162. browser.browserAction.enable(tabId);
  163. } else {
  164. browser.browserAction.disable(tabId);
  165. }
  166. }
  167. function isAllowedURL(url) {
  168. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  169. }
  170. async function refreshBadge(tabId) {
  171. await Promise.all(badgeRefreshPending);
  172. const promise = refreshBadgeAsync(tabId);
  173. badgeRefreshPending.push(promise);
  174. await promise;
  175. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  176. }
  177. async function refreshBadgeAsync(tabId) {
  178. for (let property of BADGE_PROPERTIES) {
  179. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  180. }
  181. }
  182. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  183. const tabData = tabs[tabId];
  184. const value = tabData[property];
  185. if (!badgeTabs[tabId]) {
  186. badgeTabs[tabId] = {};
  187. }
  188. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  189. const browserActionParameter = { tabId };
  190. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  191. return new Promise(resolve => {
  192. if (RUNNING_IN_EDGE) {
  193. browser.browserAction[browserActionMethod](browserActionParameter);
  194. resolve();
  195. } else {
  196. browser.browserAction[browserActionMethod](browserActionParameter, resolve);
  197. }
  198. });
  199. }
  200. }
  201. })();