ui.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_16.png";
  24. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_16_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.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. singlefile.config.get()
  78. .then(config => {
  79. if (config.contextMenuEnabled) {
  80. browser.contextMenus.create({
  81. id: MENU_ID_SAVE_PAGE,
  82. contexts: ["page"],
  83. title: "Save page"
  84. });
  85. browser.contextMenus.create({
  86. id: MENU_ID_SAVE_SELECTED,
  87. contexts: ["selection"],
  88. title: "Save selection"
  89. });
  90. } else {
  91. browser.contextMenus.removeAll();
  92. }
  93. });
  94. }
  95. function processTab(tab, options) {
  96. const tabId = tab.id;
  97. singlefile.core.processTab(tab, options)
  98. .then(() => {
  99. tabs[tabId] = {
  100. id: tabId,
  101. text: "...",
  102. color: DEFAULT_COLOR,
  103. title: "initializing...",
  104. path: DEFAULT_ICON_PATH,
  105. progress: -1,
  106. barProgress: -1
  107. };
  108. refreshBadge(tabId);
  109. })
  110. .catch(() => {
  111. tabs[tabId] = {
  112. id: tabId,
  113. text: "↻",
  114. color: [255, 141, 1, 255],
  115. title: "reload the page",
  116. path: DEFAULT_ICON_PATH,
  117. progress: -1,
  118. barProgress: -1
  119. };
  120. refreshBadge(tabId);
  121. });
  122. }
  123. function onTabError(tabId) {
  124. const tabData = tabs[tabId];
  125. tabData.text = "ERR";
  126. tabData.color = [229, 4, 12, 255];
  127. tabData.title = DEFAULT_TITLE;
  128. tabData.path = DEFAULT_ICON_PATH;
  129. tabData.progress = -1;
  130. tabData.barProgress = -1;
  131. refreshBadge(tabId);
  132. }
  133. function onTabEnd(tabId) {
  134. const tabData = tabs[tabId];
  135. tabData.text = "OK";
  136. tabData.color = [4, 229, 36, 255];
  137. tabData.title = DEFAULT_TITLE;
  138. tabData.path = DEFAULT_ICON_PATH;
  139. tabData.progress = -1;
  140. tabData.barProgress = -1;
  141. refreshBadge(tabId);
  142. }
  143. function onTabProgress(tabId, index, maxIndex) {
  144. const tabData = tabs[tabId];
  145. const progress = Math.max(Math.min(100, Math.floor((index / maxIndex) * 100)), 0);
  146. if (tabData.progress != progress) {
  147. tabData.progress = progress;
  148. tabData.text = "";
  149. tabData.title = "progress: " + Math.min(100, Math.floor((index / maxIndex) * 100)) + "%";
  150. tabData.color = [4, 229, 36, 255];
  151. const barProgress = Math.floor((index / maxIndex) * 15);
  152. if (tabData.barProgress != barProgress) {
  153. tabData.barProgress = barProgress;
  154. tabData.path = WAIT_ICON_PATH_PREFIX + barProgress + ".png";
  155. }
  156. refreshBadge(tabId);
  157. }
  158. }
  159. function onTabRemoved(tabId) {
  160. delete tabs[tabId];
  161. }
  162. function onTabActivated(tabId, isActive) {
  163. if (isActive) {
  164. browser.browserAction.enable(tabId);
  165. } else {
  166. browser.browserAction.disable(tabId);
  167. }
  168. }
  169. function isAllowedURL(url) {
  170. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  171. }
  172. async function refreshBadge(tabId) {
  173. await Promise.all(badgeRefreshPending);
  174. const promise = refreshBadgeAsync(tabId);
  175. badgeRefreshPending.push(promise);
  176. await promise;
  177. badgeRefreshPending = badgeRefreshPending.filter(pending => pending != promise);
  178. }
  179. async function refreshBadgeAsync(tabId) {
  180. for (let property of BADGE_PROPERTIES) {
  181. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod);
  182. }
  183. }
  184. async function refreshBadgeProperty(tabId, property, browserActionMethod) {
  185. const tabData = tabs[tabId];
  186. const value = tabData[property];
  187. if (!badgeTabs[tabId]) {
  188. badgeTabs[tabId] = {};
  189. }
  190. if (JSON.stringify(badgeTabs[tabId][property]) != JSON.stringify(value)) {
  191. const browserActionParameter = { tabId };
  192. badgeTabs[tabId][property] = browserActionParameter[property] = value;
  193. return new Promise(resolve => {
  194. if (RUNNING_IN_EDGE) {
  195. browser.browserAction[browserActionMethod](browserActionParameter);
  196. resolve();
  197. } else {
  198. browser.browserAction[browserActionMethod](browserActionParameter, resolve);
  199. }
  200. });
  201. }
  202. }
  203. })();