ui.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 browser, singlefile */
  21. singlefile.ui = (() => {
  22. const DEFAULT_ICON_PATH = "/extension/ui/resources/icon_16.png";
  23. const WAIT_ICON_PATH_PREFIX = "/extension/ui/resources/icon_16_wait";
  24. const DEFAULT_TITLE = "Save page with SingleFile";
  25. const DEFAULT_COLOR = [2, 147, 20, 255];
  26. const BADGE_PROPERTIES = [{ name: "text", browserActionMethod: "setBadgeText" }, { name: "color", browserActionMethod: "setBadgeBackgroundColor" }, { name: "title", browserActionMethod: "setTitle" }, { name: "path", browserActionMethod: "setIcon" }];
  27. const STORE_URLS = ["https://chrome.google.com", "https://addons.mozilla.org"];
  28. const MENU_ID_SAVE_PAGE = "save-page";
  29. const MENU_ID_SAVE_SELECTED = "save-selected";
  30. const MENU_ID_SAVE_FRAME = "save-frame";
  31. const badgeTabs = {};
  32. const badgeRefreshPending = {};
  33. browser.runtime.onInstalled.addListener(refreshContextMenu);
  34. if (browser.menus && browser.menus.onClicked) {
  35. browser.menus.onClicked.addListener((event, tab) => {
  36. if (event.menuItemId == MENU_ID_SAVE_PAGE) {
  37. processTab(tab);
  38. }
  39. if (event.menuItemId == MENU_ID_SAVE_SELECTED) {
  40. processTab(tab, { selected: true });
  41. }
  42. if (event.menuItemId == MENU_ID_SAVE_FRAME) {
  43. processTab(tab, { frameId: event.frameId });
  44. }
  45. });
  46. }
  47. browser.browserAction.onClicked.addListener(async tab => {
  48. if (isAllowedURL(tab.url)) {
  49. const tabs = await browser.tabs.query({ currentWindow: true, highlighted: true });
  50. if (!tabs.length) {
  51. processTab(tab);
  52. } else {
  53. tabs.forEach(processTab);
  54. }
  55. }
  56. });
  57. browser.tabs.onActivated.addListener(async activeInfo => {
  58. const tab = browser.tabs.get(activeInfo.tabId);
  59. onTabActivated(tab.id, isAllowedURL(tab.url));
  60. });
  61. browser.tabs.onCreated.addListener(tab => onTabActivated(tab.id, isAllowedURL(tab.url)));
  62. browser.tabs.onUpdated.addListener((tabId, changeInfo, tab) => onTabActivated(tab.id, isAllowedURL(tab.url)));
  63. browser.tabs.onRemoved.addListener(tabId => onTabRemoved(tabId));
  64. browser.runtime.onMessage.addListener((request, sender) => {
  65. if (request.processProgress && request.maxIndex) {
  66. onTabProgress(sender.tab.id, request.index, request.maxIndex);
  67. }
  68. if (request.processEnd) {
  69. onTabEnd(sender.tab.id);
  70. }
  71. if (request.processError) {
  72. if (request.error) {
  73. console.error("Initialization error", request.error); // eslint-disable-line no-console
  74. }
  75. onTabError(sender.tab.id);
  76. }
  77. });
  78. return { update: refreshContextMenu };
  79. async function refreshContextMenu() {
  80. const config = await singlefile.config.get();
  81. if (browser.menus && browser.menus.removeAll && browser.menus.create) {
  82. if (config.contextMenuEnabled) {
  83. await browser.menus.removeAll();
  84. browser.menus.create({
  85. id: MENU_ID_SAVE_PAGE,
  86. contexts: ["page"],
  87. title: DEFAULT_TITLE
  88. });
  89. browser.menus.create({
  90. id: MENU_ID_SAVE_SELECTED,
  91. contexts: ["selection"],
  92. title: "Save selection with SingleFile"
  93. });
  94. browser.menus.create({
  95. id: MENU_ID_SAVE_FRAME,
  96. contexts: ["frame"],
  97. title: "Save frame with SingleFile"
  98. });
  99. } else {
  100. await browser.menus.removeAll();
  101. }
  102. }
  103. }
  104. async function processTab(tab, options) {
  105. const tabId = tab.id;
  106. try {
  107. await singlefile.core.processTab(tab, options);
  108. refreshBadge(tabId, {
  109. id: tabId,
  110. text: "...",
  111. color: DEFAULT_COLOR,
  112. title: "Initializing...",
  113. path: DEFAULT_ICON_PATH,
  114. progress: -1,
  115. barProgress: -1
  116. });
  117. } catch (error) {
  118. if (error) {
  119. onTabError(tabId);
  120. } else {
  121. refreshBadge(tabId, {
  122. id: tabId,
  123. text: "↻",
  124. color: [255, 141, 1, 255],
  125. title: "Reload the page",
  126. path: DEFAULT_ICON_PATH,
  127. progress: -1,
  128. barProgress: -1
  129. });
  130. }
  131. }
  132. }
  133. function onTabError(tabId) {
  134. refreshBadge(tabId, {
  135. text: "ERR",
  136. color: [229, 4, 12, 255],
  137. title: DEFAULT_TITLE,
  138. path: DEFAULT_ICON_PATH,
  139. progress: -1,
  140. barProgress: -1
  141. });
  142. }
  143. function onTabEnd(tabId) {
  144. refreshBadge(tabId, {
  145. text: "OK",
  146. color: [4, 229, 36, 255],
  147. title: DEFAULT_TITLE,
  148. path: DEFAULT_ICON_PATH,
  149. progress: -1,
  150. barProgress: -1
  151. });
  152. }
  153. function onTabProgress(tabId, index, maxIndex) {
  154. const progress = Math.max(Math.min(20, Math.floor((index / maxIndex) * 20)), 0);
  155. const barProgress = Math.floor((index / maxIndex) * 8);
  156. refreshBadge(tabId, {
  157. progress,
  158. text: "",
  159. title: "Save progress: " + (progress * 5) + "%",
  160. color: [4, 229, 36, 255],
  161. barProgress,
  162. path: WAIT_ICON_PATH_PREFIX + barProgress + ".png"
  163. });
  164. }
  165. function onTabRemoved(tabId) {
  166. delete badgeTabs[tabId];
  167. }
  168. function onTabActivated(tabId, isActive) {
  169. if (browser.browserAction && browser.browserAction.enable && browser.browserAction.disable) {
  170. if (isActive) {
  171. browser.browserAction.enable(tabId);
  172. if (browser.runtime.lastError) {
  173. /* ignored */
  174. }
  175. } else {
  176. browser.browserAction.disable(tabId);
  177. if (browser.runtime.lastError) {
  178. /* ignored */
  179. }
  180. }
  181. }
  182. }
  183. function isAllowedURL(url) {
  184. return url && (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file://")) && !STORE_URLS.find(storeUrl => url.startsWith(storeUrl));
  185. }
  186. async function refreshBadge(tabId, tabData) {
  187. if (!badgeRefreshPending[tabId]) {
  188. badgeRefreshPending[tabId] = [];
  189. }
  190. let promise;
  191. for (promise of badgeRefreshPending[tabId]) {
  192. await promise;
  193. }
  194. promise = refreshBadgeAsync(tabId, tabData);
  195. badgeRefreshPending[tabId].push(promise);
  196. await promise;
  197. badgeRefreshPending[tabId].filter(pending => pending != promise);
  198. }
  199. async function refreshBadgeAsync(tabId, tabData, lastTabData) {
  200. for (let property of BADGE_PROPERTIES) {
  201. await refreshBadgeProperty(tabId, property.name, property.browserActionMethod, tabData, lastTabData);
  202. }
  203. }
  204. async function refreshBadgeProperty(tabId, property, browserActionMethod, tabData) {
  205. const value = tabData[property];
  206. const browserActionParameter = { tabId };
  207. if (browser.browserAction && browser.browserAction[browserActionMethod]) {
  208. browserActionParameter[property] = value;
  209. if (!badgeTabs[tabId]) {
  210. badgeTabs[tabId] = {};
  211. }
  212. if (JSON.stringify(badgeTabs[tabId][browserActionMethod]) != JSON.stringify(value)) {
  213. badgeTabs[tabId][browserActionMethod] = value;
  214. await browser.browserAction[browserActionMethod](browserActionParameter);
  215. }
  216. }
  217. }
  218. })();